Pygame physics simulation (Tutorial)

This series of tutorials is intended to show you how to create a physical simulation using Python and Pygame. The simulation will build up and eventually consist of multiple particle interacting with one another and can be used in several ways. I have written this sort of program several times, in several languages for several different projects, which I describe below.

Requirements

  • Python, which can be downloaded here.
  • A basic knowledge of Python, if you are complete beginner you should probably read through some other tutorials first (this is a good one). At the very least, you’ll need to know how to run a Python script.
  • Pygame, which can be downloaded here (you'll also find a lot more detail about how Pygame works and what it can do there).

You won’t, however, require any prior knowledge of Pygame; this tutorial will show you how to use Pygame to create a graphical display, update it, and make it to respond to user inputs. For these tutorial, it would be advantageous if you had a grounding in basic mathematics, specifically trigonometry. Many children wonder when they’ll ever use the trigonometry they learn in school, well creating simulations is one of those (few) times. In these tutorials I’ll show you some simple mathematical tricks that can be surprisingly powerful. These tricks took me a while to work out and I'll do my best to explain how how they work. If you want, you can blindly copy the code, but if you want to change the behaviour of the simulation at a later date, it would be a good idea to understand what the mathematics is for.

Uses for the simulation

As I mentioned above, I have used used this type of program for several, surprisingly diverse projects. For example:

  • The simulation started as molecular dynamics simulation, which came out of an idea for a lava lamp simulation.
  • I later adapted it to display a network built as by a Go AI.
  • I made a similar version to help someone in my old lab draw an interactome map (although he didn’t use the dynamic part in the end).
  • I then used it to display other networks, such as this word network.
  • I adapted this to create a simulated cell with a cytoskeleton.
  • I've considered using the code in a related project to show the metabolic pathways in virtual microbes that players evolve to fight one another.
  • I've also considered using the code to draw a network of all my projects to show how they interact (for example, this code for drawing networks interacts with nearly all my projects).

All of these different projects used the same ideas, so should have resulted in a lot of code reuse, but instead, I seem to have spent a lot of time rewriting the code. This was partially because it was initially written in Java and later in Python, and partially because I’ve come up with better ways of doing things in each iteration. However, each time I rewrote the code, I spent a lot time frustrated, struggling with getting the maths right (witnessing all sort of odd behaviours from the particles in the meantime). So this time, I’m determined will be the last. I've tried to write the code in such a way that I can import the module and create networks easily. I've also decided to write this tutorial explaining how to create this similar sort of simulation in case anyone else wants to use it. I would like to put the program online for people to play with, but I’m not sure I can without redoing everything (again) in Java.