Boids- A Simple Way to Simulate How Birds Flock in Processing

It is mysterious how birds fly together in groups, heading in unpredictable directions at the same time. This behavior of flocks is not the result of one leader passing instructions to others, nor is it a result of all the birds knowing where they are heading to- instead, it is the cumulation of each bird’s reaction to its own immediate neighbor. For instance, birds try in general to stay close to their neighbors to protect themselves from their predators. However, they, of course, try to avoid crashing into each other.

This rather emergent behavior is known as the flocking system. I have probably tried creating flocking systems over ten times in various coding languages throughout my career. However, I’ve never had the chance to document how a simple flocking system works and can be simulated. In this entry, I would like to document how the most simple flocking system can be recreated.


Boids, “bird-oid objects”, is the combination of simple rules that simulates flock behavior. Boids was originally introduced by computer graphics expert, Craig Reynolds, who also worked on scenes for the original Tron movie from 1982 and Batman Returns in 1992. It consists of three fundamental rules: Cohesion, Alignment, and Separation. Just like many other emergent behaviors, each bird can only register and apply these rules to its immediate neighbors within the limited ranges. The following explains how each rule affects each bird.


Each bird tries to stay close to the other birds in the mass. When they register their neighbors, this rule tries to get each to come to the center of their neighbors defined space.


Each bird is flying in some direction. When they see others, this rule gets each bird to try to align their direction based on their immediate neighbor.


When birds get too crowded, this rule tries to keep enough of a boundary around each bird to avoid collisions.

Pseudo Code

Below pseudo code simply explains what each boid is/does.

void Setup

 Initialize x numbers of Boid class

 class Boid

Find its neighbor boids that are in the range and run the following methods.

Run Cohesion method and get the vector value
Run Separation method and get the vector value
Run Alignment method and get the vector value

Combine the Cohesion, Separation and Alignment vectors as an acceleration

Add acceleration to its vector

Move its position with its vector

vector Cohesion (this boid’s neighbors)

If there are no neighbors, return 0 vector.

Find the center position of its neighbors,
then find a new vector from this boid’s position to this center position.

return vector.

vector Separation (this boid’s neighbors)

If there are no neighbors, finish here and return 0 vector.

For each of its neighbor boid,
   Find a vector directing from each of its neighbor boid’s position to this boid’s position.

   If the above vector is greater than 0,
   the force should be inversely proportional to the distance.

Combine all forces calculated for each neighbor boid

return vector.

vector Alignment (this boid’s neighbors)

If there are no neighbors, finish here and return 0 vector.

For each of its neighbor boid,
get its current vector and combine all to find the average vector within its neighbors 

return vector;

Final codes

These are my final codes for the project (pde file on github). I also uploaded the version that runs in Unity3D. The version in Unity3D was made possible by the Udemy lecture: Game Devs Unleash Artificial Intelligence: Flocking Agents by Razvan Pistolea. Please subscribe to his lecture to fully comprehend the flocking system. In addition, the lecture provides an advanced version of the flocking system.


Increasing the magnitudes of Cohesion keeps units together.


Increasing the range of Separation disrupts the aligned movements.


Increasing the magnitude of Alignment makes the movements more straight.

Follow on Medium:


Boids: Flocking made simple | Hermen de Weerd

Why do birds flock together? | How it works

Boids – Stanford Computer Science

Game Devs Unleash Artificial Intelligence: Flocking Agents | Udemy

Leave a comment

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