Inspired, as usual, by Leonid’s recent post, I decided to first write a script that would mimic his. After that, since I had all the numbers worked out, I wrote two more MATLAB programs: one that mimicked elastic collisions in 2-dimensions, and one that mimics them in 3.

In theory, you can specify the number of particles and their radius, as well as the mass, position, and initial velocity for each (I didn’t vectorize radius for some reason, so I cannot model balls of different sizes bouncing around). However, in practice I just generate random vectors for each of these numbers. The final aspect is that the domain I put the balls in was a pool table of 9 x 4.5 units, or 9 x 4.5 x 4.5 for the 3D version. This was just to make calculating the reflecting angle easier when a ball hit the wall.

As with Leonid’s code, mine works by checking whether the next step will cause any collisions, then adjusting the velocity vector so that the collision didn’t happen (using conservation of momentum and kinetic energy). This algorithm is not “smart” in the sense that by avoiding one collision, it might get pushed into a second collision which it does *not* detect, and if a particle gets going fast enough, it can reflect off a wall from a large distance (my time step is just 0.01). You can spot this in some of the figures below.

Anyways, here are some of the outputs. I did not go through the trouble of turning these into .gifs, but they play fairly smoothly. What happens is I simulate N particles of varying masses and velocities bouncing around in a 2- or 3- dimensional box for T seconds, then plot the path of one of the particles. The end position of all the particles, plus this path, is in each picture below (with the “tracked” particle colored in red).

The case of n particles in a d-dimensional box can be reduced to a single particle in an (nd)-dimensional polygonal cone, same way as it works for n=2 and d=1. However, the geometry of unfolding the trajectory by reflections becomes quite more intricate.

By the way, a commenter on my post pointed out a youtube video with essentially the same observation, but in which (1) only collisions between two balls are counted; (2) collisions are counted only until the heavy ball turns around; (3) the mass ratio is taken to be to compensate for (1) and (2). The person in the video says that the ratio of 16 gives 3 such collisions. I’m getting 4. Could you check this with your matlab code?

Never mind; the description of the calculations in the video were internally inconsistent (p vs p+1), which explains the discrepancy.