Pygame physics simulation (Tutorial)
This series of tutorials demonstrates how to create a physical simulation using Python and Pygame. The tutorials start with the very basics and build up to a final simulation of a classical physics problem: the trajectory of a cannonball. Something like this:
If you're not interested in the mechanics of the program, you can skip ahead to tutorial 10 and download the PyParticles module and particle_tutorial_10.txt, which demonstrates how to use it from the bottom of the page.
- 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).
It would also be advantageous if you had a grounding in basic mathematics, specifically trigonometry. The tutorial includes 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 later want to change the behaviour of the simulation, it would be a good idea to understand how the mathematics works. If you're interested in learning more about the mathematics or the physics then I can recommend the Khan Academy, which has an amazing collection of educational videos.
You won’t however, require any prior knowledge of Pygame; this tutorial will start from the very basics, demonstrating how to use Pygame to create a graphical display, update it, and make it to respond to user inputs.
Uses for the simulation
Although this simulation is very simple, with a few adaptations, I have expanded 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 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 adapted this to create a simulated cell with a cytoskeleton.
- By swapping repulsive forces for attraction, I made a simple solar system simulation.
- I'm planning to use 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).
Below is an example of a network of Chinese characters built using code from the tutorial (See here for more details).
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 (or Canvas or SVG).