As a part my of physics based modelling course, I implemented a flocking simulation using threejs.
Flocking (or Swarming) is a nice example of something known as emergent behavior (wiki).
Emergence is a phenomenon whereby larger entities arise through interactions among smaller or simpler entities such that the larger entities exhibit properties the smaller/simpler entities do not exhibit.
This behavior is common among various animal groups such as birds, ants, bees, fishes. Here is a nice video of starlings flocking around.
The idea was popularized in the field of graphics by Craig Reynolds, when he created the famous Boids artificial life simulation in 1986.
At its core, each Boid (Bird-oid object!) follows three simple rules:
- Seperation Avoid crowding local buddies.
- Alignment Direction of velocity should be along the average direction of local buddies.
- Cohesion Stay centered between local buddies.
And that's it! This leads to some cool emergent behavior. This can be extended in many ways, by adding some obstacle avoidance, common goal and other effects. Something I find really cool is that we can start each boid at some random initial velocity, and see them slowly come together into a nice flock.
The source code is available here, in case someone wants to play with it. The implementation is based on this one by Ellie.