Particle Life Simulation
Explore artificial life with simple rules of attraction and repulsion that produce complex self-organizing patterns

Particle Life Simulation
Overview
Particle Life is a fascinating simulation of primitive artificial life using simple rules of attraction or repulsion among atom-like particles. Despite the simplicity of the underlying algorithm, it produces incredibly complex, life-like patterns that emerge spontaneously.
The core algorithm is less than a page of code, yet it generates behaviors reminiscent of biological systems, cellular automata, and self-organizing phenomena found in nature.
What Makes It Special
This project demonstrates how complexity can arise from simplicity. Unlike many other artificial life simulations, Particle Life:
- ✅ No collision detection - This made simulating thousands of particles possible in real-time
- ✅ Real-time GUI controls - Change parameters in real-time for easy fine-tuning and exploration
- ✅ Extremely simple code - Core algorithm is under 100 lines
- ✅ Educational focus - Designed as learning material for non-programmers
- ✅ New patterns discovered - The real-time tuning approach revealed never-seen-before patterns
Try It Online
The quickest way to experience Particle Life is through the online demos:
- 2D Version: https://hunar4321.github.io/particle-life/particle_life.html
- 3D Version: https://hunar4321.github.io/particle-life/particle_life_3d.html
Just open these links in your browser and watch the particles interact!
How It Works
The Core Algorithm
Each particle has a position (x, y) and velocity (vx, vy). The simulation applies forces between particles based on their types and distances.
The Rule Function: For each pair of particle types, we define a "gravity" parameter (g):
- Positive g = Attraction
- Negative g = Repulsion
The force between two particles is calculated as:
F = (g × 1) / d
where d is the distance between particles (limited to a maximum range).
The JavaScript Code
Here's the complete simulation in just a few lines of JavaScript:
<canvas id="life" width="500" height="500"></canvas>
<script>
// Hunar Ahmad @ brainxyz
m = document.getElementById("life").getContext("2d");
draw = (x, y, c, s) => {
m.fillStyle = c;
m.fillRect(x, y, s, s);
};
atoms = [];
atom = (x, y, c) => {
return { x: x, y: y, vx: 0, vy: 0, color: c };
};
random = () => {
return Math.random() * 400 + 50;
};
create = (number, color) => {
group = [];
for (let i = 0; i < number; i++) {
group.push(atom(random(), random(), color));
atoms.push(group[i]);
}
return group;
};
rule = (atoms1, atoms2, g) => {
for (let i = 0; i < atoms1.length; i++) {
fx = 0;
fy = 0;
for (let j = 0; j < atoms2.length; j++) {
a = atoms1[i];
b = atoms2[j];
dx = a.x - b.x;
dy = a.y - b.y;
d = Math.sqrt(dx * dx + dy * dy);
if (d > 0 && d < 80) {
F = (g * 1) / d;
fx += F * dx;
fy += F * dy;
}
}
a.vx = (a.vx + fx) * 0.5;
a.vy = (a.vy + fy) * 0.5;
a.x += a.vx;
a.y += a.vy;
if (a.x <= 0 || a.x >= 500) { a.vx *= -1; }
if (a.y <= 0 || a.y >= 500) { a.vy *= -1; }
}
};
yellow = create(200, "yellow");
red = create(200, "red");
green = create(200, "green");
update = () => {
rule(green, green, -0.32);
rule(green, red, -0.17);
rule(green, yellow, 0.34);
rule(red, red, -0.1);
rule(red, green, -0.34);
rule(yellow, yellow, 0.15);
rule(yellow, green, -0.2);
m.clearRect(0, 0, 500, 500);
draw(0, 0, "black", 500);
for (i = 0; i < atoms.length; i++) {
draw(atoms[i].x, atoms[i].y, atoms[i].color, 5);
}
requestAnimationFrame(update);
};
update();
</script>
Creating Interesting Patterns
The key to finding interesting patterns is exploration and fine-tuning:
- Start with random parameters - Use random explorations to find base patterns
- Fine-tune gradually - Once you find something interesting, make small adjustments
- Make occasional big jumps - Avoid getting stuck in local maxima by trying radically different parameters
- Experiment with particle counts - More particles can create more complex interactions
Example Patterns
The default parameters in the code above create specific behaviors:
- Green particles repel each other (-0.32)
- Red and green particles repel each other (-0.17 and -0.34)
- Yellow attracts green (0.34) but repels red and itself
These simple rules create complex, evolving patterns that change over time.
Installation and Usage
C++ Version (with GUI)
The C++ version provides a full graphical interface for real-time parameter adjustment.
Download and Run:
# Clone the repository
git clone http://192.168.1.237:3000/archie/particle-life.git
cd particle-life
# Go to the bin folder and run the executable
cd particle_life/bin
./particle_life
Building from Source: The C++ version uses openFrameworks. To build it yourself:
- Download openFrameworks
- Use openFramework's projectGenerator and import the
/particle_life/folder - Alternatively, generate a new openFramework project and add ofxGui
- Replace the
/src/folder with the one provided in this repository
The core algorithm is in the first 100 lines of /particle_life/src/ofApp.cpp. The rest are GUI components and rendering controls provided by openFrameworks.
Python Version
A Python implementation is also available:
cd particle-life
python particle_life.py
Web Version
The JavaScript version is ready to run in any browser:
- Open
particle_life.htmlfor the 2D version - Open
particle_life_3d.htmlfor the 3D version
Other Ports and Implementations
The Particle Life concept has been implemented in many languages and frameworks:
- Godot: game-of-leif
- Rust: smarticles
- Go: go-artificial-life, particles-rules-of-attraction, Particle-Life-Go
- Python: pyrticleslife
- Lua: love-life
- QB64-PE: Particle-Life
- WebGL: webgl-particles
- Java: ParticleSimulation
- C# Winforms: ParticleLifeSimulation
- FreeBasic: FreeBasic implementation
- Julia: ParticleLife.jl
- Flutter: particles
Related Topics
Particle Life is related to several fascinating areas of study:
- Cellular Automata - Like Conway's Game of Life
- Primordial Soup Evolution - Early life simulation theories
- Self-Organizing Patterns - How order emerges from chaos
- Artificial Life - Simulating life-like behavior
- Emergent Behavior - Complex behavior from simple rules
Inspiration and History
This project was inspired by Jeffery Ventrella's Clusters. While I don't have access to Ventrella's original code, this implementation takes a different approach:
Key Differences:
- No collision detection (enables real-time simulation of thousands of particles)
- Added GUI controls for real-time parameter adjustment
- Focus on educational accessibility for non-programmers
The simplicity of the code (compared to other artificial life projects) was intentional - designed to prove that complexity can arise from extreme simplicity.
Future Improvements
There are several exciting directions for extending Particle Life:
- Save and Load Parameters - Allow users to share interesting models they discover
- Dynamic Particle Types - Currently fixed to four types, could be made configurable
- Performance Optimization - The O(n²) complexity is a bottleneck; could use spatial partitioning or GPU acceleration
- GPU Computing - Pairwise distance calculations are embarrassingly parallel and perfect for GPU
- Better Boundary Handling - Fast particles can escape screen bounds; need improved collision detection
- Improved UI - More intuitive controls for finer parameter adjustments
- Randomization Features - A randomize button or meta-rule for continuous parameter mutation
- Evolutionary Algorithms - Use a fitness function to automatically evolve interesting patterns (though defining "interesting" is challenging!)
Educational Value
Particle Life serves as an excellent educational tool because it:
- Demonstrates emergence - Shows how complex patterns arise from simple rules
- Accessible code - Easy to understand and modify
- Interactive learning - Real-time feedback encourages experimentation
- Cross-disciplinary - Touches on physics, biology, computer science, and mathematics
- Visual intuition - Patterns emerge that can be observed and analyzed
Conclusion
Particle Life is a beautiful example of how simplicity can give rise to complexity. With just a few dozen lines of code, we can simulate systems that exhibit life-like behaviors, self-organization, and emergent properties.
Whether you're a programmer wanting to understand emergent systems, a student exploring artificial life, or just someone who enjoys watching mesmerizing patterns evolve, Particle Life offers something for everyone. The real-time parameter adjustment makes it a playground for discovery - you never know what fascinating pattern you might stumble upon next.
References
- Video Tutorial: https://youtu.be/0Kx4Y9TVMGg
- Online Demo: 2D | 3D
- Original Repository: http://192.168.1.237:3000/archie/particle-life
- Inspiration: Jeffery Ventrella's Clusters
- Related: Conway's Game of Life, Cellular Automata, Artificial Life
Acknowledgments
This project was created by Hunar Ahmad as educational material to demonstrate how complexity can emerge from simplicity. The code is intentionally kept simple to be accessible to non-programmers while still producing fascinating results.
Special thanks to the community for creating ports in various languages and frameworks, making Particle Life accessible to even more developers and researchers.

