Boid Simulator: Coding Challenge #1

ViscousPotential
Analytics Vidhya
Published in
4 min readJun 19, 2021

--

Birds flocking like boids

This is the first part in a series that will be exploring various coding challenges for beginners and experts alike. These articles will be going in-depth about the concepts of various projects while keeping everything simple and logical enough to understand. I will not be including any real code, so as to allow you to create this project in whichever language or framework you prefer.

What is a boid?

The word boid comes from a program developed by Craig Reynolds to simulate the flocking behaviour of birds. This behaviour is also very similar to that of fish and other flocking animals. While, the behaviour of birds seems very complex on initial observation, Craig Reynolds produced three base behaviours that, when implemented together, simulate this very complex behaviour. This is referred to as emergent behaviour. These basic rules are still sometimes used to this day to produce the flocking of birds and fish in video games.

The three behaviours are as follows:

  • SEPARATION: steer to avoid crowding local flock mates
  • ALIGNMENT: steer towards the average heading of local flock mates
  • COHESION: steer to move towards the centre of mass of local flock mates

Aside from this, each boid is usually given a cone/sector of vision. This prevents boids from reacting to other boids that it should not be able to see. Obviously, real animals cannot see behind themselves, so it makes sense to simulate that behaviour too. In code this tends to be implemented as a number of ray casts between set angles of the boid. It can also be done manually, where ray casts are not available, by mathematically calculating the line between the current boid and another boid and checking if the distance and angle are within respective limits. It is highly recommended to use classes to represent each boid, as to do otherwise would make the process far more difficult. The class should hold an x, y and rotation variable along with the functions for each rule. Alternatively, the class could hold a single vector variable to hold all three values.

Boid Separation

This is essential to the behaviour of a boid as it ensures that they do not run into one another. It is the most basic behaviour and goes a long way towards making each boid seem intelligent. In code, this can be done by ray casting out and, if another boid is hit, turning in the opposite direction of that ray. Again, if ray casts are not available, you can just use some vector math and figure out the direction of any boids nearby and turn in the opposite direction. You must keep in mind that each boid must steer away from every other boid it has in its range of vision and average the steering direction. I do not recommend directly setting the angle to be anything specific. Instead, nudging the rotation looks smoother and more real.

Boid Alignment

This is the rule that creates the generic behaviour of birds of flocking and moving in the same general direction as flock mates. It is quite simple to implement and just requires finding boids within the cone of vision and querying their current rotation. The average rotations of all other boids nearby can be used to inform the rotation of each boid. Again, it is recommended to nudge the rotation, rather than set it to the average, to prevent sudden changes in direction.

Boid Cohesion

This is the final rule and creates one of the most beautiful behaviours in boids. Cohesion is what produces the swirling patterns in boids/birds, where they seem to circle each other during flight. This is also quite simple to implement and requires finding the average positions (not rotations) of all boids nearby. Averaging these positions gives the centre of each group of boids near each boid. Now nudging the rotation towards that centre point is all that is needed to implement cohesion.

Conclusion

Once these rules have been implemented, you should have a decent boid simulation. You may need to play with some of the rotation strengths for each rule to refine the behaviour. From here there are various modifications you can make to add to the functionality:

  • (SIMPLE) If your original implementation was 2D, try converting it into 3D
  • (MODERATE) Add a strong wind force to affect the flock
  • (MODERATE) Add perching behaviour so that the birds stop at the bottom of the screen for some time before moving on.
  • (HARD) Create a predator that the boids have to avoid causing the flock to scatter

--

--

ViscousPotential
Analytics Vidhya

A developer and electronics enthusiast wanting to share his experiences