Web Analytics
Air resistance in p5.js – a projectile slowed by air

Air resistance in p5.js – a projectile slowed by the air

The third lesson of the guide Program a physics simulation. We continue projectile motion, but now a tennis ball flies through air. The program throws two balls at the same time: a grey one without air resistance and an orange one with air resistance (click the canvas to throw them again). Here you can see the real power of simulation: for this motion there is no simple formula, but the program calculates it easily.

Physics: projectile with air resistance – the drag force F = ½ · ρ · C · A · v², opposite to the velocity.

Programming: objects (a body with its own variables), functions that work with an object, one function for two bodies.

You should know: lesson 2 (projectile motion).

Step 1: drag force and acceleration

The air resistance (drag) force is F = K · v², where K = ½ · ρ · C · A depends on the air density and the shape and size of the body. The force is opposite to the velocity, so the acceleration it causes (a = F / m) has components opposite to the velocity components:

ax = −(K / m) · v · vx        ay = −g − (K / m) · v · vy

The difference from projectile motion: now the horizontal velocity changes too, more and more slowly as the ball slows down. For a tennis ball the constants are:

const m = 0.057;         // mass, kg
const d = 0.067;         // diameter, m
const C = 0.5;           // drag coefficient
const RHO = 1.2;         // air density, kg/m³
const K = 0.5 * RHO * C * Math.PI * d * d / 4;  // drag force = K · v² (Math.PI: p5's PI does not exist yet when the file loads)

Step 2: two bodies – objects

We want two balls with the same rules but different K. Instead of duplicating all the variables, we describe each ball with one object – a “box” with its own position, velocity and trail. The function newBody(k) creates such an object:

function newBody(k) {
  const a = radians(angle);
  return { x: 0, y: 0, vx: v0 * cos(a), vy: v0 * sin(a), k: k, flying: true, trail: [] };
}

function start() {
  ideal = newBody(0);          // without drag: k = 0
  withDrag = newBody(K);
}

We access the properties of an object with a dot: body.x, body.vy…

Step 3: one step for any body

The function step(body) performs an Euler step for the body we give it. For the ideal body k = 0, so the drag disappears and we get ordinary projectile motion – the same code for both cases.

const v = sqrt(body.vx * body.vx + body.vy * body.vy);
const ax = -body.k / m * v * body.vx;        // drag slows the horizontal motion
const ay = -g - body.k / m * v * body.vy;    // and the vertical motion
body.vx = body.vx + ax * dt;
body.vy = body.vy + ay * dt;
body.x = body.x + body.vx * dt;
body.y = body.y + body.vy * dt;

Step 4: drawing and the result

Drawing is also one function, drawBody(body, colour), which we call twice in draw(). The result: without drag the ball flies 91.6 m (formula 91.7 m), but with drag only 42.7 m – less than half. The Python program on the page projectile with air resistance gives 43.1 m because it uses a much smaller step (0.001 s instead of 1/60 s); with a step of 1/600 s this program also gives 43.0 m.

Whole program

Copy it into the p5.js editor and delete the line canvas.parent('lesson-canvas');.

// Projectile with air resistance – program from the lesson “Program a physics simulation” (simulacije.svetprogramiranja.com)
const g = 9.81;          // acceleration due to gravity, m/s²
const v0 = 30;           // initial speed, m/s
const angle = 45;        // launch angle, in degrees
const SCALE = 3.2;       // how many pixels make one metre
const dt = 1 / 60;       // time step, s

// tennis ball
const m = 0.057;         // mass, kg
const d = 0.067;         // diameter, m
const C = 0.5;           // drag coefficient
const RHO = 1.2;         // air density, kg/m³
const K = 0.5 * RHO * C * Math.PI * d * d / 4;  // drag force = K · v² (Math.PI: p5's PI does not exist yet when the file loads)

let ideal, withDrag;     // two bodies: without and with air resistance

function setup() {
  const canvas = createCanvas(320, 220);
  canvas.parent('lesson-canvas');
  start();
}

function newBody(k) {
  const a = radians(angle);
  return { x: 0, y: 0, vx: v0 * cos(a), vy: v0 * sin(a), k: k, flying: true, trail: [] };
}

function start() {
  ideal = newBody(0);
  withDrag = newBody(K);
}

function step(body) {
  if (!body.flying) {
    return;
  }
  const v = sqrt(body.vx * body.vx + body.vy * body.vy);
  const ax = -body.k / m * v * body.vx;        // drag slows the horizontal motion
  const ay = -g - body.k / m * v * body.vy;    // and the vertical motion
  body.vx = body.vx + ax * dt;
  body.vy = body.vy + ay * dt;
  body.x = body.x + body.vx * dt;
  body.y = body.y + body.vy * dt;
  if (body.y <= 0) {
    body.y = 0;
    body.flying = false;
  }
  body.trail.push([body.x, body.y]);
}

function drawBody(body, colour) {
  const groundY = height - 25;
  const x0 = 12;
  noFill();
  stroke(colour);
  strokeWeight(2);
  beginShape();
  for (const point of body.trail) {
    vertex(x0 + point[0] * SCALE, groundY - point[1] * SCALE);
  }
  endShape();
  noStroke();
  fill(colour);
  circle(x0 + body.x * SCALE, groundY - body.y * SCALE, 12);
}

function draw() {
  background(248, 250, 252);
  step(ideal);
  step(withDrag);

  stroke(51, 65, 85);
  strokeWeight(3);
  line(0, height - 25, width, height - 25);

  drawBody(ideal, color(148, 163, 184));
  drawBody(withDrag, color(234, 88, 12));

  noStroke();
  fill(11, 19, 36);
  textSize(14);
  text('without drag: x = ' + ideal.x.toFixed(1) + ' m', 10, 22);
  text('with drag: x = ' + withDrag.x.toFixed(1) + ' m', 10, 42);
}

function mousePressed() {
  if (mouseX >= 0 && mouseX <= width && mouseY >= 0 && mouseY <= height) {
    start();
  }
}

Tasks for you

  1. Change angle to 30°, 35°, 40° and 45°. At which angle does the ball with drag fly farthest?
    Solution

    The ranges are 41.5 m, 42.9 m, 43.3 m and 42.7 m – farthest at about 40°, not at 45° as without air.

  2. Replace the tennis ball with a football (m = 0.43 kg, d = 0.22 m, C = 0.25), and then with a table tennis ball (m = 0.0027 kg, d = 0.04 m, C = 0.5).
    Solution

    The football flies 49.6 m, and the table tennis ball only 11.7 m. What matters is the ratio of drag to mass: a ball that is light for its size feels the air the most.

  3. Print the terminal velocity of the ball: the speed at which the drag equals the weight, K · v² = m · g.
    Solution

    const vt = sqrt(m * g / K); – about 23.0 m/s for a tennis ball.

  4. Add a third ball (e.g. a football). What would the program look like with an array of bodies and a for loop?
    Solution

    Create an array bodies = [newBody(0), newBody(K1), newBody(K2)], and in draw() use for (const body of bodies) { step(body); drawBody(body, …); }. Since each body has a different mass, the mass should also be stored in the object (body.m).

Next