Boids algorithm demonstration

Coherence
Separation
Alignment
Visual range

What is this?

This is a simple demonstration of the boids algorithm that's featured in this Smarter Every Day video:

Check the video out to learn how this simulation models flocking behavior in birds and other animals.

How does it work?

Each of the boids (bird-oid objects) obeys three simple rules:

1. Coherence

Each boid flies towards the the other boids. But they don't just immediately fly directly at each other. They gradually steer towards each other at a rate that you can adjust with the "coherence" slider.

2. Separation

Each boid also tries to avoid running into the other boids. If it gets too close to another boid it will steer away from it. You can control how quickly it steers with the "separation" slider.

3. Alignment

Finally, each boid tries to match the vector (speed and direction) of the other boids around it. Again, you can control how quickly they try to match vectors using the "coherence" slider.

Visual range

There are a ton of ways to extend this simple model to better simulate the behavior of different animals. An example I showed in the video is to limit the "visual range" of each boid. Real animals can't see the entire flock; they can only see the other animals around them. By adjusting the visual range slider, you can adjust how far each boid can "see"—that is which other boids it considers when applying the three rules above.

What else can I do?

The source code from the video is available on GitHub. If you want to go beyond what you can do by playing with the sliders on this page, I recommend grabbing the code and changing stuff to see what happens. All you need is a text editor and a web browser.

There are lots of features you could try adding to the code yourself:

See this link for more ideas and hints on how to do some of the ideas above.