How Do Birds Fly in Flocks?
A murmuration of starlings looks like a single living creature — hundreds of thousands of birds moving in perfect unison without any leader or plan. The secret? Each bird follows just three simple rules with its nearest neighbours. Complexity emerges for free.
The Mystery of Murmuration
A murmuration is the aerobatic display of thousands of European starlings (Sturnus vulgaris) at dusk. The whole flock rolls, pulses and reshapes like a living fluid — yet no single bird directs the others.
Scientists were puzzled for centuries. Early guesses included telepathy, air-pressure signals or a leader bird giving silent orders. The real answer, discovered in the 1980s, is far more elegant: each bird tracks only its seven nearest neighbours and obeys three local rules. Global beauty emerges from entirely local information.
Three Simple Rules
In 1986, computer graphics researcher Craig Reynolds created a program he called Boids (short for "bird-oids") that replicated flocking with just three steering rules:
Separation
Steer away from any nearby boids that are too close — avoid collisions.
Alignment
Steer to match the average direction and speed of nearby boids.
Cohesion
Steer towards the average position of nearby boids — stay together as a group.
The Maths Behind the Rules
Each boid has a position vector p and a velocity vector v. On every time step, we compute three acceleration contributions and add them to the velocity.
Separation
For each neighbour j closer than a separation radius
r_sep, subtract a repulsion vector weighted by distance:
separation_force += (p_self - p_j) / |p_self - p_j|²
Alignment
Average the velocities of all neighbours within radius r_align
and steer towards that average heading:
alignment_force = mean(v_neighbours) - v_self
Cohesion
Steer towards the average position (centre of mass) of neighbours within
r_cohesion:
cohesion_force = mean(p_neighbours) - p_self
Each force is scaled by a weight constant, then all three are added to the current velocity. The velocity is then clamped to a maximum speed so boids don't accelerate forever.
Minimal JavaScript Implementation
// For each boid, find neighbours and compute forces
function updateBoid(b, others) {
let sep = {x:0,y:0}, ali = {x:0,y:0}, coh = {x:0,y:0}, n = 0;
for (const o of others) {
const dx = b.x - o.x, dy = b.y - o.y;
const d = Math.hypot(dx, dy);
if (d < RADIUS_SEP && d > 0) {
sep.x += dx / (d * d);
sep.y += dy / (d * d);
}
if (d < RADIUS_VIS) {
ali.x += o.vx; ali.y += o.vy;
coh.x += o.x; coh.y += o.y;
n++;
}
}
if (n > 0) {
ali.x = ali.x/n - b.vx; ali.y = ali.y/n - b.vy;
coh.x = coh.x/n - b.x; coh.y = coh.y/n - b.y;
}
b.vx += sep.x * W_SEP + ali.x * W_ALI + coh.x * W_COH;
b.vy += sep.y * W_SEP + ali.y * W_ALI + coh.y * W_COH;
// clamp speed, then move
const speed = Math.hypot(b.vx, b.vy);
if (speed > MAX_SPEED) { b.vx *= MAX_SPEED/speed; b.vy *= MAX_SPEED/speed; }
b.x += b.vx; b.y += b.vy;
}
Emergence: More Than the Sum of Parts
Flocking is a classic example of emergence: complex collective behaviour that arises from simple individual rules without any central control. The flock as a whole exhibits properties that no single boid has.
Other examples of emergent systems:
- Ant colonies solve the shortest-path problem through pheromone trails — each ant is simple, the colony is intelligent.
- Traffic jams form and dissolve as waves, even when no accident has occurred — caused purely by drivers reacting to each other.
- The brain produces consciousness from billions of individual neurons, none of which is conscious alone.
- The internet routes data reliably from billions of decentralised nodes following local protocols.
Real-World Applications
The Boids algorithm and variants of swarm intelligence are used in many fields:
- Computer graphics & games: Realistic crowds, schools of fish, and insect swarms in films and video games (the original use case).
- Drone swarms: Military and rescue drones coordinate using Boids-like rules without centralized control, so the swarm survives even if the coordinator fails.
- Robotics: Swarm robots in warehouses (like Amazon's Kiva robots) navigate using emergent coordination inspired by ant colonies.
- Particle swarm optimization: A mathematical algorithm that mimics boid-like searching to find optimal solutions in engineering and machine learning.
- Traffic modelling: Highway simulations use agent-based models where each car follows simple local rules — flocking math applied to roads.
Try It Yourself
You can explore flocking behaviour interactively in the simulations below:
- Boids Flocking Simulation — The classic Reynolds Boids with adjustable weights for all three rules. Try cranking up separation and watch the flock shatter into lonely birds.
- Birds Flock (3D) — A three-dimensional murmuration-style flock using Three.js instanced rendering with thousands of birds.
- Ant Colony — Another emergent system: watch ants lay pheromone trails and collectively discover shortest paths.