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.
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).
![](diagram1.jpg)
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
![](diagram2.jpg)
The Math
Step 1Calculate 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).