Animation: Flocking Boids

This is a simulation of the flocking behaviour of birds, created in homage to Craig Reynolds, who originally wrote a simlar program in 1986 and published his work in 1987.

Each "boid" can only see a small distance, and follows a simple set of rules based on what it's neighbours are doing. No one boid is in control. Yet when the behaviour of all of the boids are combined, a beautiful and rich emergent flocking behaviour is created.

If you want to see the forces acting on each of the boids, you can click on "Toggle Show Forces". That will show you a circle that represents how far each boid can see, and lines within the circle representing forces acting on the boid. Take a look at what happens when two boids approach each other in different directions. 

You can also click on the animation to create news boids.

So what are the simple rules that each boid follows? Well, there are just 3. Alignment, Cohesion and Seperation.

Alignment

Boids will try and follow the crowd, and head in the same direction as their neighbours are going. So if all a boid's neighbours are going one way, a boid will try and turn to go in the same direction.

Cohesion

Boids are sociable souls and so they like to stay close to their neighbours. Each boid will try and move towards the center of all of the neighbouring boids it can see. 

Seperation

Not too close! Getting too close would surely interfere with flying, and so boids are repelled from their neighbours, particularly if they are very close (this is through an inverse square relationship).

Emergent Flocking

Following these 3 simple rules, the birds form into flocks and fly together in the same direction. This is a really nice example of complex and ordered patterns emerging on a large scale through the interactions of actors on a small scale. 

This animation was created using JavaScript and HTML5 Canvas. the code for this animation, and all of my animations, can be found on this public github repository.

Read next