First we want to look at a very simple one dimensional collision. This will be a head-on collision between a moving ball (1) and a stationary ball (2). As ball 2 is stationary, its energy and momentum are both zero before the collision. In this setup there are two unknown values – the final velocities of each ball which we will call x and y. The initial velocity of ball 1 is v and the mass of each ball is m and n respectively.
Let’s do the algebra
Create the functions
So, we succeeded in deriving formulas for x and y. Formulas (2) and (3) above. Using these we can write a new function.
function elastic_collision(m, n, v) {
var x = v * (m - n) / (m + n);
var y = v + x;
return [x, y];
}
So now, armed with this function, we can modify the draw() function of our sketch to handle the collision between the balls properly, instead of just changing their colour and allowing them to pass through each other. So we write something like this instead…
function draw() {
...
for (let index = 0; index < balls.length; index++) {
balls[index].update();
}
for (let i = 0; i < balls.length; i++) {
for (let j = i+1; j < balls.length; j++) {
if (areColliding(balls[i], balls[j])) {
handleCollision(balls[i], balls[j]);
}
}
}
...
}
You will see that we need a new function called handleCollision() to handle the collision between two balls. At the moment this function is simple, but we will be adding more to it later. Here is the initial version…
function handleCollision(ball1, ball2) {
let [v1f, v2f] = elastic_collision(ball1.mass, ball2.mass, ball1.vel.x);
ball1.vel.x = v1f;
ball2.vel.x = v2f;
}
A working sketch
With these few changes, our sketch is now ready to support a one dimensional collision between two balls, where one of the balls is not moving.
I have turned off the wrap-around feature of the sketch, leaving the balls to disappear off the edges of the screen. This is because it will only work for one collision! Why? If the balls wrapped around back onto the screen and collided again our elastic_collision() function would not work as it expects one of the balls to be stationary. After the collision both balls are moving.
Before the collision
Before the collision ball A is moving and ball B is stationary. So ball A contains all the energy and momentum of the system. It’s easy to see for the energy. For the momentum, notice that the p (total) vector is identical to the A (top) vector. The B (middle) vector with just a small circle and an arrowhead represents a zero length vector.
After the collision
After the collision, you can see that the A vector now points in the opposite direction and is shorter. B is now moving, so has a momentum vector. Notice that if you were to put the tail of the B vector on the head of the A vector, then it finishes at the same point as the p (total) vector. That is, the overall momentum has been conserved.
You will see that the energy of ball A and ball B still add up to the same total energy (we almost, it is out by 0.01, but this just a rounding error). So the overall energy has also been conserved!
Next, we need to handle the situation where both balls are moving. On to the next chapter…