Flocking simulation

I was going through my github and cleaning up, (mostly deleting empty stuff from projects I started but lost interest) when I came across a small simulation I wrote several years ago. The program simulates birds (called boids) moving together as a swarm. Craig Reynolds came up with the idea and implemented it by making each boid follow a set of simple rules.


Explanation for various visuals of simulation

Red dot: A boid that has not detected any other boids in its proximity (no other boids in view distance and/or in view angle). It will just follow its current vector until it encounters another boid.

Green dot: Boid that is aware of one or more other boids in its proximity.

Green line between boids: If the option is enabled the boid will draw a line to all other boids that it can see. It will use these boids when it applies the rules of the simulation (listed below) to adjust its movement vector.

Red line: Movement vector of boid.

The Rules

(images are from Craig Reynolds website)

Separation: each boid adjusts its vector so that it moves away from the densest concentration of boids that are in its immediate proximity.


alignment: boids adjust their vector so that it matches the average vector of the boids around them.


cohesion: the boids steer to the mid point of the flock formed by nearby boids.

Additionally each boid also has a field of view, the rules listed above only apply to he boids that the boid can see. This means that if several boids are behind a boid the boid in front wont adjust its vector to steer towards them, but the other boids will follow it.

Simulation features

  • Variable number of boids
  • Can save/load the simulation
  • Switch on/off various aspects of the boid rendering
  • Can interact with boids via mouse
  • Simulation accuracy (how often the rules listed above are checked) is adjustable
  • Boids wrap on hitting the edge of the screen
  • Simulation parameters (view angle, distance etc) are adjustable
  • …etc

If you are interested, and more importantly if you can stomach the cringy commit messages and partially cancerous code (also I did not know what the .gitignore files is used for back then) then please have a look at the repository.

Leave a Reply

Your email address will not be published. Required fields are marked *