Simple Particle Collision Gravity System | Jason Zhu’s Personal Website
Simple Particle Collision Gravity System
This simple particle collsion and Gravity system is completed in 2021 summer.
The idea started from my motivation to build a physics engine, but since that I cannot reach the level of building an engine and limited time, I just made the minimal and core program. Also, this only supports circle collisions.
Here is the final effect. Also, by modifying coefficients of the code, you can alter the size, position, and amount of the obstacle, particles, and also adding some cool effects, and here is an illustration:
How does the code work
The core system is the collision system, and I use the vector calculation to model the motion of particles.
Each particle has attribute for speed in 2D, and represented in vector form
When one particle detects collision with another particle or an obstacle (distance is smaller than the radius added together), it transform the speed vector from the normal coordinate to the coordinate systemt that use the connecting line of two objects as unit vector for “x”.
Then the program calculate the transformed vector (where I matrix calculation in linear algebra in the code).
By physics property, we know that the perpendicular sub-vector would not be affected and we only need to calculate the sum of two sub-vectors pointing towards each other (or to the same direction) on the connecting line.
Finally, we transformed the resulting new speed vector back to the normal coordinate system, and the outcomes are the speed after collision.
Yet, there are many details not mentioned here due to the limitations of computer calculation. I will provide the code and you may check all the details.
Aside to text explaination, here is the corresponding code for algorithm (written in Processing based on Java):
Other parts
Check Collision Algorithm:
Obstacle Collision Algorithm:
When I wrote this program in 2021 summer, I was still a novice about Java coding, so I think my codes can be greatly optimized and extended if there’s more time. I might finish that if I had more time.
Also, the code for this post is way more longer than any other, so you can follow this link to check the full details on github!