Author: Barbu Alexandru Daniel
After seeing this video on youtube, one project stood out to me - a collision simulation.
Since I was recently learning pygames and I am pretty good at physics, I decided to give it a try.
All I had to do was calculate the new velocity at each step of the while loop and move the objects in the scene.
I hope that I will tackle increasingly more complex physics simulations with the goal of one day having a custom physics engine made in c++ for a tech demo.
- How to simulate physics in a virtual environment
Important
This one is a little complicated to explain without some context.
Computers are good at doing calculations, undoubtedly, and physics is all about finding x at time y, but when you combine those two things you would get an object that moves very fast in the scene. For example, you might move it 10, 12, 50 units each millisecond.
This is where you need a limiter for the simulation. The way I did it was to lock the sim at 30 frames. Before this I used to think this step was complicated, but was easier than it appears.
- importance of consistent geometry collision
Important
You will see that if you fiddle with the speed of the objects, there is a high possibility of them passing through one another or blocking one inside the other. For this small scale simulation this is normal. If I move a square 50 pixels to the right and it happens to land inside a bigger square, and if the new velocity is smaller than the 50 pixels per second, object 1 will remain inside the other one.
If you were to move 100 pixels per second, there is a high possibility that in the span of 100 pixels you just skip the other object
Important
This behaviour can be fixed though (see: Penalty Method, Constraint-Based Method or Projection Method), but I will delve into these solutions with other projects.
- applying drag and energy loss to a physics simulation
- handling processes and automating testing in python
Caution
The code was developed on wsl 2. As a result I recommend running the code on a Linux-based system.
# Step 1.1 - create a virtual env
python -m venv env # venv called `env`# Step 1.2 - activate virtual env
source env/bin/activate# Step 2 - intall dependencies
pip install -r requirements.txt# Step 3.1 - run the code - elastic collision
# Script needs:
# [simulation time] - this can be -1 for infinite simulation time, or some other positive value that represents the seconds the sim will run for
# [mass 1]
# [velocity 1]
# [mass 2]
# [velocity 2]
python3 elastic_collision.py -1 2 2 2 0# Step 3.2 - run the code - plastic collision
# Script needs:
# [simulation time] - this can be -1 for infinite simulation time, or some other positive value that represents the seconds the sim will run for
# [mass 1]
# [velocity 1]
# [mass 2]
# [velocity 2]
python3 plastic_collision.py -1 5 10 2 0Tip
I have created a test script that launches 6 tests in total, with different outcomes. Please feel free to study and modify this to your liking. Below I will show you the commands to run it.
# Step 4 - run tests
python3 run_demos.pyTip
Notice how in the terminal you can inspect the simulation parameters and even receive a hint about what should happen.
