[UPDATE: I've made a 3D version of this simulation using the Khan Academy programming environment.]
I was searching the net for physics simulations made in Pygame (scouting out the competition) when I found a gravity simulation. The simulation is very straightforward (and hence very good): it starts with some randomly distributed particles, which coalesce due to the force of gravity, often leading to some particles orbiting others. It is basically a simulation of how a cloud of dust can become a sun, or solar system.
It looked like a simple idea and I was slightly disappointed with myself for not having thought of it, especially given that I have already coded most aspects of the simulation. I was inspired further to make my own version after watching the excellent, Wonders of the Solar System.
To my particle simulation I added:
- Mass to the particle object (and a way to convert mass to size)
- An attract() function that exerts a force on two particles based on their masses and the distance between them
- A way to deal with collisions
I then experimented with different parameters such as the size of the simulation, the number of particles and the range of starting masses. The biggest problem with the simulation seemed to be that the scale is wrong. By this I mean, the size of the particles is too big relative to the distance between the particles. This is hard to avoid because increasing the distance between particles will result in only one or two particles being on the screen at any one time while decreasing the particle size will make them invisible.
I have tried to deal with this problem by displaying the particles as larger than they "really" are. So even if a particle appears be a two pixel square, other particles can come within two particles and still not collide. I have also coloured particles, so the whiter they appear, the more dense they are. Due to the way, I set things up, I found that only one particle ever had a size greater than 2 units, which I displayed as a circle. I decided to colour this particle yellow to represent the initiation of thermonuclear fusion, i.e. the formation of a star.
Simulation run through
I have analysed several runs through the simulation here, but below are some screenshots of a representative run. The simulation appears to start very slowly. This is partly because it is slow - the beginning of the simulation has the most number of particles, so requires the most computations - and partly because the particles begin stationary and it takes time for a particle's speed to build up.
The simulation begins with 170 randomly placed particles, each with a mass between 1-4 units.
The cloud of particles begins to collapse under the force of gravity.
Eventually the cloud collapses and one particle accumulates sufficient mass (>125 units) to become a star. Due to the total mass in system, it is very unlikely that two stars will form. Occasionally they do, but then collapse in on each other. Only once did I see a binary star system spinning off into the sunset, looking relatively stable.
The system eventually settles down with a few "planets" in stable-looking orbits, often quite elliptical, and the odd "comet" with a very large and erratic orbit. There are also normally several small particles that are flung into space, never to return again. Even with a small number of planets and comets it is hard (impossible, I think) to predict whether the system is stable. It may look quite stable until a comet collides into a planet, or passes very close by and is flung into space.
It would be nice to see more astronomical phenomena, such as planets with moons or swirling galaxies, but I think this would require a lot more particles and a lot more space. It may also require a third spatial dimension (which will reduce the chance of a collision) and keeping track of rotation or angular velocity, but that is beyond my knowledge of mechanics.
The program, as it stands, can be found here. You will need Python and Pygame to run it. To write your own, see tutorial 12 in my Pygame physics simulation tutorial (you will need to look at the preceding 11 tutorials too).