Make a realtime model of a Solar System (Earth’s or any other you can imagine), using Newtonian physics, SDL, and Python.
After hearing Alan Kay talk about using programming to explore Physics, I decided to explore the physics of our solar system by making a realtime video model. Here’s what I did:
1. Review the Newtonian mechanics behind it – specifically, F=ma, the law of universal gravitation, and the conservation of momentum. Wikipedia has great articles about these, and any Physics textbook should supply the details.
2. I decided to use SDL to do the video programming. There’s a great Python interface to it, called PyGame.
3. I also decided to use NumPy to do the vector arithmetic.
The most important mathematical operation here is Integration: Integrating velocity over time to get position, and integrating acceleration over time to get velocity. This is normally easy enough, but when two bodies get very close to each other, the acceleration gets very high and changes very fast to make things tricky. Initially, weird things would happen when two bodies got close to each other. I worked out some ways of improving the Integration in these cases, including adding a “jerk” factor to model the fact that the rate of acceleration is always changing.
I also added an error meter, which you can see pop up in the video on the left side. The simulator tracks the total error (by tracking total momentum), and pops up a green bar when it gets above a threshold. The bar turns red when it gets very high, which thankfully doesn’t happen too often. The error is also logged to a console window, which you can see in the video.
The simulator can be controlled via a command window. But my favorite way is to simply add lines into the source file. I add a line for each Solar Body to appear – I can specify the mass and initial conditions, or let the simulator choose randomly.
What I really enjoy about watching it is how you see Kepler’s laws play out, even though they were never programmed explicitly. In the video, for instance, you can see nice Keplerian elliptical motion, and you can also see the velocities change to keep equal areas swept in equal periods of time,. all as Kepler predicted.
Enjoy!
To see the video, click on the box labelled “sim_star_thumb”, which appears on the bottom of this page, after the two pictures.

(complete instructions)