A p5.js implementation of Craig Reynold's Boids artificial life simulation.
Live preview available here: https://ishanmanchanda.github.io/Boids/ (Laptop/desktop recommended)
The simulation demonstrates the flocking behavior of bird-like objects called Boids. It serves as an example of emergent behavior, where complexity is generated by a group of individual agents interact according to simple rules.
The 3 rules of the original simulation are:
- Separation: Each Boid has a concept of personal space, and moves in a way to prevent crowding. More specifically, each Boid is repelled by all its neighbors within a certain radius.
- Alignment: Each Boid wants to move aligned with the group, and so changes its velocity to be in line with the average velocity of its neighbors.
- Cohesion: Each Boid tries to move towards the center of its local group.
These are the rules that govern Boid-Boid interactions; the original simulation additionally allowed for other rules such as avoiding obstacles and seeking targets.
I have implemented the above listed rules as well as some others to try and create an ecosystem. The ecosystem contains both Boids and Predators, with the Predators seeking Boids while the Boids flee from them. The Predators have a look-ahead feature which allows them to take into account the current velocity of Boids in additiona to their position. An earlier version of this ecosystem, which I developed on Khan Academy, had even more interactions in the form of static obstacles as well as a water body.
Each Predator has a hunger value which is reset when it eats a Boid. This is done by getting within a certain distance of a Boid, at which point the Boid is eaten. If a Predator's hunger value reaches a certain limit, it dies of starvation.
Additionally, both Predators as well as Boids have certain sight ranges, beyond which they cannot perceive other agents. The group behavior + the wrap-around effect of the edges of the simulation area generally ensure that agents eventually come close enough to interact, rather than continue on linear paths that never intersect.
Alignment, Cohesion, and Separation can be toggled on/off by pressing A, C, and S on the keyboard. Boids and Predators can be added by holding down B or P and clicking inside the simulation area with the mouse. Owing to the keyboard requirement, a laptop/desktop is highly recommended for viewing the simulation.
All parameters including initial simulation values, creature sizes, sight ranges, as well as the relative weights used to balance the various forces as easily tweakable in the globals.js file in the source directory.
- Concepts and implementation of OOP with an emphasis on Inheritance and Polymorphism
- Common optimization techniques for simulations including Bin-Lattice Spatial Subdivision
- Extensive applications of Vectors and optimizations for operations on vectors