Flocking Boids Algorithm
What is it?
Boids is an artificial life program, developed by Craig Reynolds in 1986, which simulates the flocking behaviour of birds. > His paper on this topic was published in 1987 in the proceedings of the ACM SIGGRAPH conference. The name "boid" corresponds to a shortened version of "bird-oid object", which refers to a bird-like object.
How did I make it?
I started by reading up on Craig Reynolds' paper along with some research into basic Vectors and Steering Behaviours.
From there I could develop the underlying code and front-end visualisation starting with a few basic boids, movement vectors and edge detection on the window. 100 boids all heading in random directions at random speeds.
Now I could start to implement the three steering behaviours for cohesion, alignment and separation. These are at the core simply vector calculations, adding or subtracting a vector to the boid to steer its movement. At this time I added the control panels and settings to allow me to test and refine the boids flight.
After some more fiddling and settings I found a set that produced the desired outcome.
- Cohesion = 0.18
- Alignment = 0.01
- Separation = 0.13
What did I use?
- VSCode editor
The three main variables for the boid's behaviour are :
Cohesion determines how strongly a boid is attracted to other boids within its field of perception. A higher cohesion value makes boids cluster together, while a lower value causes them to spread out.
Alignment dictates how much a boid adjusts its own flight direction to match that of other boids it can see. A high alignment value makes all the boids quickly align their flight directions, while lower values allow them to move independently.
Separation is the opposite of cohesion. It controls how much a boid is repelled by other boids within its perception. Higher separation values lead to boids spreading out, whereas lower values encourage them to flock together.
Adjusting these three values can result in various behaviors among the boids. Sometimes, one of these values can dominate and lead to chaotic or collapsed flock formations.
The preset values represent an optimal balance that mimics the natural behavior of flocking birds. Play with the settings to generate new behaviours, they are quite sensitive.
Michael Barnsley suggested that the settings or values that create a resemblance to real life could be considered similar to the codes carried in plant and animal DNA. His work on the Barnsley Fern is worth a read.
Idea for an evolutionary demo where boids are born with random settings - if they spend too much time outside the flock they get eaten, if they stay in the middle they die of starvation. Over enough generations you should see a familiar flocking behaviour emerge. Discussion
Page updated : calendar_month 1 month ago