This simulation shows two blocks moving along a track and colliding with each other and the walls. One spring is attached to the wall with a spring. Try changing the mass of the blocks to see if the collisions happen correctly.
You can change parameters such as mass, spring stiffness, and friction (damping). You can drag either block with your mouse to change the starting positions.
(If you don't see the simulation try
instructions for enabling Java.)
Collisions and Conservation of Momentum
The spring and block on the left use the same model as the single spring simulation. For a collision with the wall, we simply reverse the velocity. For collisions between moving blocks we use the law of conservation of momentum to determine the new velocities.
Define the following variables:
 v_{1i}, v_{2i} = initial (precollision) velocity of blocks
 v_{1f}, v_{2f} = final (postcollision) velocity of blocks
 v_{cm} = velocity of center of mass
 v_{1cm}, v_{2cm} = velocity of blocks in center of mass frame
 m_{1}, m_{2} = mass of blocks
 p_{i}, p_{f} = initial and final momentum of system of blocks
We find the velocity of the center of mass of the 2block system.
v_{cm} =

m_{1} v_{1i} + m_{2} v_{2i}

m_{1} + m_{2}

Next we find the velocity of each block in the coordinate system (frame) that is moving along with the center of mass.
v_{1cm} = v_{1i} − v_{cm}
v_{2cm} = v_{2i} − v_{cm}
Next we reflect (reverse) each velocity in this center of mass frame, and translate back to the stationary coordinate system.
v_{1f} = −(v_{1i} − v_{cm}) + v_{cm}
v_{2f} = −(v_{2i} − v_{cm}) + v_{cm}
If you fully expand the above you get

v_{1f} = −v_{1i} +

2(m_{1} v_{1i} + m_{2} v_{2i})

m_{1} + m_{2}

 (1) 

v_{2f} = −v_{2i} +

2(m_{1} v_{1i} + m_{2} v_{2i})

m_{1} + m_{2}

 (2) 
As a check, we can calculate the pre and post collision momentum, which should be the same.
p_{i} = m_{1} v_{1i} + m_{2} v_{2i}
p_{f} = m_{1} v_{1f} + m_{2} v_{2f}
If you expand p_{f} using equations (1) and (2) and simplify you will see that p_{f} = p_{i} as expected.
Handling Collisions in Software
simulation of two balls colliding
Simulations on the computer are discrete in the sense that time advances in "chunks", not smoothly. In the diagram, we calculated the state of the world at time
t = 10.0 and then at time
t = 10.1. But the collision happened sometime inbetween. So by the time we detect a collision, the objects are overlapping each other  a physically impossible state.
The simulations on this website handle collisions as follows:
 Detect that a collision occurred
 Rerun the simulation to very close to (but just before) the time of collision. The time of collision is found by a binary search process.
 Handle the collision by modifying the state of the simulation, such as fixing the velocities as described above.
 Continue to run the simulation up to the current 'now' time. If additional collisions happen they are handled in the same way.
If you are not running the simulation in real time, you can take as long as you want to get as much accuracy as desired. But for a real time simulation, you may need to accept lower accuracy or use some fancier programming. For example, instead of using trial and error to find the time of collision, you could try to predict the time of collision based on the state of the system.
Other examples of collision handling are the simulations
Roller Coaster with Flight and
Rigid Body Collisions.
This web page was first published April 2001.