Balls Colliding in Two-Dimensions

This is a demo I created to continue with my experiments in physics and collisions between objects. Use the slider and number boxes below to change the mass, speed or direction of each ball. You can also switch back and forth between editing different balls by clicking on any ball. Click on "Go" to watch them collide.

Special thanks to Isaac Newton for making this demo possible

Some Notes About This Demo

Before trying to tackle an elastic collision in 2D it helps to first understand the physics and math involved in calculating a 1D collision.

The best way I can think of explaining a 2D collision is by comparing it to a 1D collision. For example if two balls collided at a 180o angle directly along the x-axis or y-axis, this would be exactly the same as a 1D collision. In fact, when you think about it, my previous demo is a 2D collision where all of the movement just happens to be along the x-axis (while the y-velocity remains zero).

What happens if the collision angle is less than 180o? To keep things simple, let's assume the two balls are going to collide somewhere along the y-axis, exactly parallel to one another. As we can see in the diagram on the right, the collision can still be calculated as a 1D collision since all of the force being exchanged is along the x-axis or x-velocities of the balls. This means we can still use the same equation for a 2D collision as we did for a 1D collision:

v1 = u1(m1 - m2) + 2m2u2
m1 + m2

where:

m1 = mass of object 1
u1 = initial velocity of object 1
v1 = final velocity of object 1
m2 = mass of object 2
u2 = initial velocity of object 2
v2 = final velocity of object 2


The only thing left to consider are collisions that occur which are not parallel to the y-axis. The easiest way to solve this problem is to rotate everything so the y-axis is once again perpendicular to the collision point (see diagram on the right). After the rotation is complete, we once again have a created a scenario where the collision can be calculated as if it took place in one dimension.

The Math

Step 1

Calculate the collision angle (A) by using atan2 applied to the difference of the coordinates of each object (x, y):

A = atan2(y1 - y2, x1 - x2)

Step 2

Use the collision angle (A), the ball's initial velocity (u) and ball's initial direction (D) to derive it's x/y velocity in the new rotated coordinate system:

v1x = u1 X cos(D1 - A)
v1y = u1 X sin(D1 - A)
v2x = u2 X cos(D2 - A)
v2y = u2 X sin(D2 - A)

Step 3

Now that we have the collision aligned along the x-axis, all we have to do is apply the 1D collision equation to vx. We will call the final x-velocities for each ball f1x and f2x:

f1x = v1x(m1 - m2) + 2m2v2x
m1 + m2

f2x = v2x(m1 - m2) + 2m2v1x
m1 + m2

Step 4

Now that we we have the final x and y velocities in the rotated coordinate system, we must convert everything back to a normal Cartesian coordinate system, as follows:

v1 = (f1x2 X f1x2 + v1y X v1y2)½ (Note ½ means square root)
v2 = (f2x2 X f2x2 + v2y X v2y2)½

for final velocity and:

D1 = atan2(v1y, f1x) + A
D2 = atan2(v2y, f2x) + A
for final direction.

Also check out Emanuele Feronato's website for an example on how to solve the above using inititial and final x/y velocities (as opposed to starting off with an initial velocity (magnitude) and angle and solving for a final velocity and angle).