Boids Simulation

A modular flocking simulation based on local interaction rules, used both as an artificial-life sandbox and as the moving background on this site.

Overview

This project started from a simple observation: birds moving together in the evening can look almost choreographed. What I found striking is that this kind of collective motion can be produced with a small mathematical model.

Craig Reynolds’ Boids model describes flocking through local rules[1]. Each boid does not know the whole flock; it only reacts to nearby neighbours. Even so, the group begins to move in a coordinated way.

I built a modular version of this idea so I could experiment with flocking behaviour, parameter choices, obstacles, predators, and multi-species grouping. A restrained version of the simulation is also used as the animated background on my website.

The three rules

Each boid has a position $x_i$ and velocity $v_i$. At each time step, it computes steering forces from nearby boids. The basic model uses three local rules: separation, alignment, and cohesion.

Separation rule diagram for Boids

Separation

Steer away from nearby boids to avoid crowding. The closer a neighbour is, the stronger the repulsion.

Alignment rule diagram for Boids

Alignment

Adjust direction towards the average heading of neighbours, leading to synchronised motion across the flock.

Cohesion rule diagram for Boids

Cohesion

Move towards the local centre of mass of nearby boids, preventing the group from dispersing.

Rule diagrams adapted from Craig Reynolds’ public-domain Boids diagrams[2].

Mathematical form

For a boid $i$, let $\mathcal{N}_i$ be the set of nearby boids. Separation can be written as a repulsion from close neighbours:

$$ F_{\mathrm{sep}} = \sum_{j \in \mathcal{N}_i} \frac{x_i - x_j}{\|x_i - x_j\|^2}. $$

Alignment compares the boid’s velocity with the average velocity of its neighbours:

$$ F_{\mathrm{align}} = \frac{1}{|\mathcal{N}_i|} \sum_{j \in \mathcal{N}_i} v_j - v_i. $$

Cohesion pulls the boid towards the local centre of mass:

$$ F_{\mathrm{coh}} = \frac{1}{|\mathcal{N}_i|} \sum_{j \in \mathcal{N}_i} x_j - x_i. $$

The three steering terms are weighted and combined:

$$ v_i^{n+1} = v_i^n + w_s F_{\mathrm{sep}} + w_a F_{\mathrm{align}} + w_c F_{\mathrm{coh}}. $$

The position is then advanced by

$$ x_i^{n+1} = x_i^n + \Delta t\,v_i^{n+1}. $$

Implementation

The simulation is structured as a small framework rather than a single script. The core boid dynamics, visualisation tools, command-line interface, and experiments are kept separate.

Boids are rendered as oriented triangles. Their heading is computed from the velocity vector, so the visual direction of each boid follows its motion. I also used wrap-around boundary conditions, so the screen behaves like a torus: a boid leaving one side reappears on the opposite side.

The repository includes both a Matplotlib visualisation and an interactive Pygame sandbox, where parameters such as alignment, cohesion, separation, species count, obstacles, and predators can be adjusted.

Experiments

The repository includes several small experiments to test how the flock behaves under different rules and environments.

Baseline flocking

The baseline experiment uses the standard three-rule model. This is the cleanest version of the simulation and shows how separation, alignment, and cohesion interact to produce smooth flocking.

Neighbourhood radius test

This experiment changes how far each boid can “see”. A small radius makes the flock more fragmented, while a large radius makes the group synchronise more quickly.

Speed vs force test

This explores the balance between speed, steering force, and stability. If steering is too weak, the flock does not organise well; if it is too strong, the motion can become rigid or unstable.

Grouping / multi-species flocking

In this version, boids are split into different species. Same-species attraction and cross-species repulsion can produce partial segregation, where groups begin to form separate flocks.

Obstacle avoidance

Circular obstacles are added to the environment. Boids must steer around them while still trying to maintain flock structure.

Predator chase

Predator agents are introduced as a second type of moving object. Prey boids flee when predators enter a certain radius, and an optional “eat” mode removes prey that are caught.

Why I used it

I liked this project because it is a compact example of emergence: simple local rules can generate motion that looks complex and organic.

That made it a good fit for the website background. It gives the page a sense of movement, while still connecting to the broader themes I enjoy: simulation, dynamical systems, artificial life, and mathematical modelling.

References

  1. Craig Reynolds, ‘Flocks, Herds, and Schools: A Distributed Behavioral Model’, ACM SIGGRAPH (1987).
  2. Craig Reynolds, ‘Boids: Background and Update’, red3d.com/cwr/boids/. Rule diagrams are public-domain materials based on simple geometry.