DEV Community

Cover image for Boids
No_Arms_studio
No_Arms_studio

Posted on

Boids

< I have been experimenting with something lately, this thing is called boids or bird-oids. This wonderful algorithm was invented in 1986 by Craig Reynolds and it mimics the flocking behaviors of birds, fish, and other things such as herds. This effect is accomplished by using 3 simple rules, Separation, Alignment, and Cohesion. There are of course other things that will be Tweaked such as the range a boid can see, its target speed, or its resolve for said speed.
First I will talk about Separation. This is exactly as it sounds, boid is in range of another boid then it will try to get away from that boid with the strength of this effect being controlled by the Separation value.
Next up is Alignment, All boids will try to face in the same direction as boids in view range and this again is controlled by the Alignment value.
Finally there is Cohesion. Every boid wants to be at the center of attention and be at the middle of its flock and one last time, this is controlled by the Cohesion value.
Using all of these values you can make a good simulation of boids but how do you make these happen? My Implantation for separation was changing the x velocity by ((0 - Separation) * (distance x / distance)) and the same thing with the y velocity. The way that I achieved Cohesion is by adding up all of the distance x and distance y and dividing them by the amount of boids within range then finally multiplying them by the Cohesion value (the math looks like this (Cohesion * (sum x / boid#))) I recommend setting the cohesion to 0.1. The way to get the Alignment is found by finding the difference in x and y velocities from the boid its checking to the current boid then multiply the value by Alignment, the equation looks like (Alignment * (sum x / boid#)) and again I recommend 0.1 as the Alignment.
This is pretty much the end of this article, thanks for reading and have a great day :) happy coding.

Top comments (1)

Collapse
 
no_arms_studio profile image
No_Arms_studio

I do love boids