Pygame physics simulation (Tutorial)
http://www.petercollingridge.co.uk/pygame-physics-simulation/
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.
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.
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).
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.
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 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).
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).
Attachment | Size |
---|---|
particle_tutorial_13.txt | 2.96 KB |
PyParticles3.txt | 6.87 KB |
No hay comentarios:
Publicar un comentario