In this simulation you can drag either block. Try changing the mass of the blocks to see if the collisions happen correctly.

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 initial (pre-collision) velocity of blocks_{1i}, v_{2i}=v final (post-collision) velocity of blocks_{1f}, v_{2f}=v velocity of center of mass_{cm}=v velocity of blocks in center of mass frame_{1cm}, v_{2cm}=m mass of blocks_{1}, m_{2}=p initial and final momentum of system of blocks_{i}, p_{f}=

We find the velocity of the center of mass of the 2-block 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

_{1f} = -v_{1i} + 2(m_{1} v_{1i} + m_{2} v_{2i})/(m_{1} + m_{2}) (eqn 1)

v_{2f} = -v_{2i} + 2(m_{1} v_{1i} + m_{2} v_{2i})/(m_{1} + m_{2}) (eqn 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 _{f}_{f} = p_{i}

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

The task then becomes to determine the time in the past that the objects actually first contacted each other. In the simulation on this page, I find this moment by extrapolating backwards in time, based on the velocities of the blocks. This then gives a (presumably) more accurate time and place where the collision occurred. I then calculate the new velocities (as described above) and project them forward (outside of the normal Runge-Kutta algorithm) to the current time.

A more accurate method would be to back up the simulation to before the collision and re-run it forward to the predicted time of collision. You can do this iteratively to get as close as you want to the moment of collision. 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 probably need to cut some corners.

An additional complication comes when there are potentially several collisions that can take place. The problem is that after a collision takes place, the course of the simulation changes substantially and there could even be another collision taking place as a result within the chunk of time you are considering. In this case the right thing is to backup and run the simulation forward to the time of the first detected collision in the interval, handle the collision, and then run the simulation forward from there.

The simulations Roller Coaster with Flight and Collisions implement and describe this more sophisticated collision handling.