Particle life
Winter 2021 ~ @TitouanCh
Particle Life - Species-Simulator.com
Inspired by Clusters from Jeffrey Ventrella, we can observe in this simulation emergent behaviors reminding us of micro-organisms...
Performance (BETA)--
What is particle life?
Particle life is a simulation that depicts life-like behavior arising from particles following a set of simple rules. Particle life reminds me a lot of John Conway’s game of life in that sense. However, the execution and mechanics of these two models differ significantly.
What are the rules of particle life?
Particle life is made up of many particles. You can see them flying around above. Each particle has the ability to either attract or repel other particles.
How particles interact, if they repel or attract each other, is dependent on their color. At the start of the simulation, particles are randomly assigned a color, which represents their species. Next, each possible pair of colors (order matters) is assigned a random number. This random number will determine the nature of the interaction between the two colors: a negative number indicates an attractive force, while a positive number represents a repulsive force.
Blue | Green | Red | |
Blue | 1.8 | -5.2 | |
Green | -4.3 | 0.7 | |
Red | -3.2 | 7.3 |
Interactions don’t have to be symmetrical. For example, in the table above: blue attracts green but green repels blue, this can result in an interesting glider effect.
The strength of particle interactions diminishes based on the distance between two particles. This distance is determined using a selected norm. Additionally, if two particles get too close, a strong repelling force is applied to prevent them from passing through each other.
All these interactions can lead to endless energy creation in our simulation. We counteract this by adding a strong friction force to all particles, which prevents them from continuously accelerating.
That's all there really is to it, still, even with those simple rules we can get some fun results.
How is it made?
Particle life is built using JavaScript entirely.
However, due to the O(n^2) complexity of each step in the simulation, as calculating the next position of each particle requires determining the resulting force from each other particle acting on it, we need to employ two methods to optimize the simulation.
- The first method is space partitioning, where we divide the simulation space into partitions. For each particle, we only need to calculate the interaction with particles from its partition and neighboring partitions.
- The second method is multithreading which is built on top of space partitioning. We can assign different partitions to different threads. The performance benefit of this method varies because serializing the data and sending it to the threads each frame proves to be quite expensive.