Web Analytics
Collisions in p5.js – momentum, energy and restitution

Collisions in p5.js – how a program detects a collision

The sixth lesson of the guide Program a physics simulation. Two carts collide on a track. Until now bodies moved smoothly under a force; a collision is different – the velocities change almost instantly. So the program must (1) detect that a collision has happened and (2) calculate the velocities after it from conservation laws. With the keys 1, 5 and 0 you choose an elastic, a partly elastic or an inelastic collision (click the canvas first).

Physics: momentum and collisions – conservation of momentum, elastic and inelastic collisions, coefficient of restitution.

Programming: a condition for an event (collision), calculating the new state “in a jump”, letter and number keys (key).

You should know: lessons 1–3.

Step 1: motion without forces

On a frictionless track the carts move at constant velocity, so the step is the simplest so far – only the position changes:

x1 = x1 + v1 * dt;
x2 = x2 + v2 * dt;

At the start the first cart (2 kg) moves at 3 m/s, and the second (1 kg) is at rest. The momentum is p = 2 · 3 = 6 kg · m/s, and the kinetic energy is 2 · 3² / 2 = 9 J.

Step 2: when has a collision happened?

We track the centres of the carts. The carts touch when the distance between the centres equals the length of a cart. But that is not enough: after the collision the carts are still close, so the program would “collide” them again. So we add the condition that the carts are moving towards each other (the first is faster than the second):

if (x2 - x1 <= LENGTH && v1 > v2) {
  // collision
}

Step 3: velocities after the collision

In a collision momentum is conserved: m₁v₁ + m₂v₂ = m₁u₁ + m₂u₂. The second condition comes from the coefficient of restitution e – how much the carts “bounce”: the speed of separation after the collision is e times the speed of approach before it, u₂ − u₁ = e · (v₁ − v₂). Solving these two equations gives:

const p = m1 * v1 + m2 * v2;
const u1 = (p - m2 * e * (v1 - v2)) / (m1 + m2);
const u2 = (p + m1 * e * (v1 - v2)) / (m1 + m2);
v1 = u1;
v2 = u2;

For e = 1 the collision is elastic, and for e = 0 the carts move on together (they stick).

Step 4: checking – what is conserved?

The program prints the momentum and the energy now and before the collision:

collisionv₁ afterv₂ aftermomentumEk
e = 1 (elastic)1.00 m/s4.00 m/s6.009.00 J
e = 0.51.50 m/s3.00 m/s6.006.75 J
e = 0 (stick together)2.00 m/s2.00 m/s6.006.00 J

Momentum is always conserved, kinetic energy only in an elastic collision. In an inelastic one the “missing” energy has turned into heat and deformation.

Whole program

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

// Collisions – program from the lesson “Program a physics simulation” (simulacije.svetprogramiranja.com)
const m1 = 2;            // mass of cart 1, kg
const m2 = 1;            // mass of cart 2, kg
const LENGTH = 0.6;      // length of a cart, m
const SCALE = 30;        // how many pixels make one metre
const dt = 1 / 60;       // time step, s

let e = 1;               // coefficient of restitution: 1 – elastic, 0 – the carts stick together
let x1, v1, x2, v2, p0, E0;

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

function start() {
  x1 = 2;                // position of the centre of the cart, m
  v1 = 3;                // velocity, m/s (positive to the right)
  x2 = 6;
  v2 = 0;
  p0 = m1 * v1 + m2 * v2;                          // momentum before the collision
  E0 = 0.5 * m1 * v1 * v1 + 0.5 * m2 * v2 * v2;    // kinetic energy before the collision
}

function draw() {
  background(248, 250, 252);

  x1 = x1 + v1 * dt;
  x2 = x2 + v2 * dt;

  // collision: the carts touch and are moving towards each other
  if (x2 - x1 <= LENGTH && v1 > v2) {
    const p = m1 * v1 + m2 * v2;
    const u1 = (p - m2 * e * (v1 - v2)) / (m1 + m2);
    const u2 = (p + m1 * e * (v1 - v2)) / (m1 + m2);
    v1 = u1;
    v2 = u2;
  }

  // track and carts
  const trackY = 150;
  stroke(51, 65, 85);
  strokeWeight(3);
  line(0, trackY, width, trackY);
  noStroke();
  fill(59, 130, 246);
  rect(x1 * SCALE - LENGTH * SCALE / 2, trackY - 30, LENGTH * SCALE, 26, 4);
  fill(249, 115, 22);
  rect(x2 * SCALE - LENGTH * SCALE / 2, trackY - 24, LENGTH * SCALE, 20, 4);

  // numbers: momentum and energy now and before the collision
  const p = m1 * v1 + m2 * v2;
  const E = 0.5 * m1 * v1 * v1 + 0.5 * m2 * v2 * v2;
  fill(11, 19, 36);
  textSize(13);
  text('e = ' + e + '   (keys 1, 5, 0)', 10, 20);
  text('v1 = ' + v1.toFixed(2) + ' m/s,  v2 = ' + v2.toFixed(2) + ' m/s', 10, 40);
  text('p = ' + p.toFixed(2) + ' kg·m/s  (before: ' + p0.toFixed(2) + ')', 10, 60);
  text('Ek = ' + E.toFixed(2) + ' J  (before: ' + E0.toFixed(2) + ')', 10, 80);
  text('click to restart', 10, height - 12);
}

function keyPressed() {
  if (key === '1') { e = 1; }
  if (key === '5') { e = 0.5; }
  if (key === '0') { e = 0; }
  start();
}

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

Tasks for you

  1. Swap the masses: let a light cart (1 kg) hit a heavy one (2 kg) at rest. What happens in an elastic collision?
    Solution

    m1 = 1, m2 = 2: the light cart bounces back at 1 m/s (u₁ = −1.00 m/s), and the heavy one moves off at 2.00 m/s. The momentum is still 3 kg · m/s, and the energy 4.5 J.

  2. Make the masses equal (m1 = m2 = 1). What happens in an elastic collision?
    Solution

    The carts swap velocities: the first stops (0.00 m/s), and the second moves on at 3.00 m/s – like billiard balls or Newton's cradle.

  3. Add a wall at the right end of the track (at 10 m), from which the second cart bounces elastically. What happens next?
    Solution

    After moving the carts add if (x2 > 10 - LENGTH / 2 && v2 > 0) { v2 = -v2; }. The carts collide several times. With e = 1 the total energy stays 9 J (the wall does not take energy away), but the momentum of the carts changes at each bounce off the wall – the wall is an external body.

  4. Also print the kinetic energy “lost” in the collision (E0 − Ek). What percentage is it for e = 0?
    Solution

    text('lost: ' + (E0 - E).toFixed(2) + ' J', 10, 100); – for e = 0, 3.00 J is lost, i.e. one third of the initial energy.

Next