The Boids Flocking Model, Explained

After reading this you will be able to predict how a flock of boids behaves from three steering rules, compute one boid's update by hand, and tune separation, alignment and cohesion to move a swarm between a loose gas and a tight murmuration.

What the boids model is

A flock has no leader. No bird holds a map of the whole group, yet the group turns, splits and reforms as one shape. Craig Reynolds built a working demonstration of this in 1987. He gave each simulated agent, a "boid", the same three rules and let each one look only at its close neighbours. Coordinated motion appeared with no global plan.

The three rules are short to state. Separation: steer away from neighbours that are too close. Alignment: steer toward the average heading of nearby boids. Cohesion: steer toward the average position of nearby boids. Every boid runs all three at every frame. Nothing else.

Here is the hook. Set 300 boids moving in random directions. Within a few seconds they organise into ribbons and rolling waves that look like a starling murmuration. You did not program the ribbons. They are what happens when 300 agents each obey the same local arithmetic.

When the model fits and when it does not

Reach for boids when you want to show that group order can come from local reactions alone. It is a strong teaching example for emergence, and it is a reasonable first approximation for bird flocks, fish schools and insect swarms, where each animal reacts to a handful of neighbours.

Do not read it as a measured biological law. Real starlings track a roughly fixed number of nearest neighbours, about six or seven, rather than everything inside a fixed distance (Ballerini et al., 2008). This simulator uses a fixed perception radius, which is simpler but behaves differently as density changes. When boids crowd, a fixed radius pulls in dozens of neighbours and the flock can freeze; a fixed neighbour count would not.

The model produces motion that looks alive. That is not evidence that real birds use these exact rules. A toy that reproduces an appearance has not proven the mechanism behind the real thing. Treat boids as an existence proof for leaderless coordination, not as a measurement of it.

The three rules as vectors

Fix one boid i with position \mathbf{p}_i and velocity \mathbf{v}_i. Let N be the set of neighbours within the perception radius r. Each rule returns a steering vector.

Cohesion steers toward the neighbours' average position:

\mathbf{c} = \left( \frac{1}{|N|} \sum_{j \in N} \mathbf{p}_j \right) - \mathbf{p}_i

Here |N| is the neighbour count, \mathbf{p}_j is a neighbour's position, and the sum divided by |N| is the flock centre. Subtracting \mathbf{p}_i gives the direction from the boid to that centre.

Alignment steers toward the neighbours' average velocity:

\mathbf{a} = \frac{1}{|N|} \sum_{j \in N} \mathbf{v}_j

Separation pushes away from each close neighbour, weighted so that nearer neighbours push harder:

\mathbf{s} = \sum_{j \in N} \frac{\mathbf{p}_i - \mathbf{p}_j}{\lVert \mathbf{p}_i - \mathbf{p}_j \rVert^2}

The term \mathbf{p}_i - \mathbf{p}_j points from the neighbour back to the boid. Dividing by the squared distance \lVert \mathbf{p}_i - \mathbf{p}_j \rVert^2 makes a neighbour at distance 5 push 4 times harder than one at distance 10.

The three vectors combine with weights, then update velocity and position:

\mathbf{v}_i' = \mathbf{v}_i + w_s \mathbf{s} + w_a \mathbf{a} + w_c \mathbf{c}, \quad \mathbf{p}_i' = \mathbf{p}_i + \mathbf{v}_i'

The weights w_s, w_a and w_c are the sliders you control. In practice the new speed is also capped at a maximum so boids do not accelerate forever, and small implementation choices (whether to normalise each vector first) shift the numbers, so treat the exact constants as tunable rather than sacred.

A worked update for one boid

Work in 2D with the demo defaults: perception radius r = 50 and weights w_s = 1.5, w_a = 1.0, w_c = 1.0. Take a focal boid and two neighbours inside the radius.

Steering with two neighbours

Focal boid: position \mathbf{p}_i = (100, 100), velocity \mathbf{v}_i = (1, 0). Neighbour 1 at (110, 100) with velocity (0, 1). Neighbour 2 at (100, 130) with velocity (1, 1). Both are within radius 50 (distances 10 and 30).

  1. Cohesion. Average neighbour position is ((110+100)/2, (100+130)/2) = (105, 115). Subtract the focal position: \mathbf{c} = (5, 15).
  2. Alignment. Average neighbour velocity: \mathbf{a} = ((0+1)/2, (1+1)/2) = (0.5, 1).
  3. Separation. Neighbour 1 gives (100-110, 100-100)/10^2 = (-0.1, 0). Neighbour 2 gives (100-100, 100-130)/30^2 = (0, -0.0333). Sum: \mathbf{s} = (-0.1, -0.0333).
  4. Weighted sum. w_s \mathbf{s} = (-0.15, -0.05), w_a \mathbf{a} = (0.5, 1), w_c \mathbf{c} = (5, 15). Add to velocity: \mathbf{v}_i' = (1, 0) + (-0.15, -0.05) + (0.5, 1) + (5, 15) = (6.35, 15.95).
  5. New position: \mathbf{p}_i' = (100, 100) + (6.35, 15.95) = (106.35, 115.95).

Cohesion dominated this raw sum because the neighbours sat 5 to 30 units away, so \mathbf{c} was large. That is why real implementations normalise each steering vector to a fixed length before weighting, then cap the final speed. Without that cap the boid would leap 17 units in one frame. The lesson stands: the boid turned sharply up and to the right, straight toward the pair it could see.

Reading and tuning the swarm

Each slider has a distinct visual signature. Push cohesion up and the flock contracts into a tight ball. Push separation up and it spreads into a loose gas where boids keep their personal space. Push alignment up and boids lock heading, producing long parallel streams. The perception radius sets how far influence reaches: a small radius fragments the group into many local clusters, a large radius fuses everything into one mass.

A useful summary statistic is the polarisation, the length of the average unit heading. If every boid points the same way it is 1; if headings are random it is near 0.

\phi = \left\lVert \frac{1}{n} \sum_{i=1}^{n} \frac{\mathbf{v}_i}{\lVert \mathbf{v}_i \rVert} \right\rVert

Here n is the boid count and each \mathbf{v}_i / \lVert \mathbf{v}_i \rVert is a unit heading. The chart below shows how \phi typically rises as you raise the alignment weight while holding the other two fixed.

Polarisation stays low while alignment is weak, then climbs steeply between weights 0.6 and 1.2 as headings lock together, then saturates near 0.95.

To find rolling murmurations rather than a static blob, keep cohesion and alignment close in value (both near 1.0) and set separation slightly higher (near 1.5). Equal cohesion and separation with strong alignment gives you flowing waves instead of a frozen clump.

Tune the three weights yourself

Use the widget to build intuition before you touch the live simulation. Slide one weight and watch the flock shape and the polarisation number respond.

With separation high and cohesion low the boids spread into a diffuse gas with polarisation near 0.2. With cohesion high and separation low they collapse into a tight ball, polarisation near 0.5. With alignment high and the other two moderate they form parallel streams, polarisation near 0.9.

Common mistakes

The errors that ruin a boids run are usually numeric, not conceptual.

Forgetting the speed cap
As the worked example showed, the raw steering sum can be 17 units in one frame. Without a maximum speed the flock explodes off screen. Cap the new velocity length, typically near 4 in these units.
Skipping separation normalisation
Because separation divides by squared distance, two boids that touch produce an enormous force. Cap or normalise it, or a near-collision throws a boid across the screen.
Reading a fixed radius as biology
A fixed radius means dense flocks pull in dozens of neighbours and can lock solid. Real flocks track a fixed neighbour count instead, so do not conclude that real birds freeze when they crowd.
Over-cohesion
Set cohesion much higher than separation and the flock collapses to a single point with polarisation near 0, because there is no shared heading, only a shared target. Balance the two.

Related simulations on this site

Boids is one of several models here where simple local rules build global order. The clearest cousin is the Firefly Synchronization model, where pulse-coupled oscillators fall into unison with no conductor, the timing analogue of a flock's spatial coordination. The Ant Colony Pheromone Trails and Slime Mould Networks simulators show the same theme with a shared chemical memory instead of direct neighbour sensing. For smooth continuous self-organisation rather than discrete agents, see Lenia Continuous Life. If you are curious how chance alone, not rules, can drive a population, compare the Genetic Drift Simulator.

Frequently asked questions

Why do boids form a flock with no leader?

Because every boid applies the same three local rules to whatever neighbours it can see, order spreads outward from overlapping neighbourhoods. A turn by one boid nudges its neighbours, which nudge theirs, so alignment propagates through the group without any single boid directing it.

What do separation, alignment and cohesion each do?

Separation sets the spacing between boids, cohesion sets how tightly the group clusters, and alignment sets how strongly headings agree. Raising alignment alone drives polarisation from about 0.2 to about 0.95, as the chart above shows.

How many boids can run at once?

Hundreds. A naive version compares every pair, which costs on the order of n^2 checks, so 500 boids means 250,000 comparisons per frame. A spatial hash grid buckets boids by cell so each one checks only nearby buckets, dropping the cost close to linear in n and keeping the frame rate smooth.

Is the boids model an accurate description of real birds?

It captures the qualitative pattern but not the measured mechanism. Real starlings track a fixed number of nearest neighbours, about six or seven, rather than a fixed radius. Boids proves leaderless coordination is possible, not that birds use these exact equations.

What does the predator do to the flock?

The hawk hunts the local flock centre while boids add a strong steering term away from it. The result is a scatter-and-reform pattern: the flock splits around the predator, then closes back up once it passes, the same behaviour cohesion produces after any disturbance.