Nobody is in charge of a starling flock. There is no lead bird, no signal, no plan, and yet thousands of them wheel over a winter roost as if one animal, splitting around a hawk and closing behind it. The simulator in this article runs that behaviour on a browser canvas with up to 4,000 agents. Each one obeys three rules that fit in a sentence apiece, looks only at its nearest neighbours, and knows nothing about the shape of the flock it belongs to. The shape appears anyway.
Video: Sebastian Lague, “Coding Adventure: Boids”
What the simulator does
Three sliders, one predator, and a flock that keeps re-forming
The canvas starts with 1,200 boids, each drawn as a small arrowhead pointing along its velocity. The Boids slider takes the count anywhere from 50 to 4,000. Separation, Alignment and Cohesion set the weight of the three steering rules, Vision sets how far a boid can see (10 to 120 pixels), and Speed caps how fast it can move. The predator, on by default, is a red arrowhead that chases the nearest boid; every boid that sees it coming adds a fourth, strongly weighted urge to get away. Trails leave a fading streak behind each agent, which turns the flock into a flow diagram.
Pause freezes the frame. Reset reseeds the positions. Everything else is live, so dragging Cohesion to zero while the simulation runs dissolves the flock in a second or two, and dragging it back assembles a new one from whatever is left. The simulator is a single page at apps.x9m8.com/flocking with the source on GitHub.
Reynolds’s three rules
Separation, alignment, cohesion, and nothing else
The model is called boids, and it dates from 1986. Craig Reynolds built it as an artificial life simulation and presented it at SIGGRAPH in 1987 as “Flocks, Herds, and Schools: A Distributed Behavioral Model.” Each boid steers by three rules applied to the flockmates within its field of view. In Reynolds’s own words on his boids page: separation is to “steer to avoid crowding local flockmates,” alignment is to “steer towards the average heading of local flockmates,” and cohesion is to “steer to move toward the average position of local flockmates.”
That is the whole model. There is no rule for “form a flock,” no rule for “turn together,” no rule for “split around an obstacle and rejoin.” Those behaviours happen because the three rules push and pull against each other. Separation keeps the flock from collapsing to a point; cohesion keeps it from drifting apart; alignment makes the neighbours’ headings converge, so a turn started at one edge propagates through the group. Reynolds’s page describes flocking as “a particularly evocative example of emergence,” and the Wikipedia summary of the model lists flocks splitting and reuniting after avoiding obstacles among the behaviours nobody programmed in.
In the simulator each rule produces a steering vector. The three vectors are scaled by their sliders, summed, clamped to a maximum force, and applied as an acceleration. Speed is clamped as well, so the flock neither stalls nor explodes. Separation is weighted by the inverse of distance, which is why two boids that are nearly touching repel each other hard while two at the edge of vision barely notice.

What real starlings do
Seven neighbours, not a fixed radius
Reynolds designed boids to look right, not to match field data. Twenty years later a group of physicists in Rome went and got the data. Ballerini, Cabibbo, Candelier, Cavagna and colleagues filmed airborne flocks of a few thousand birds with synchronised cameras, reconstructed the three-dimensional position of every bird, and asked a precise question: does a bird react to everything within some distance, or to a fixed number of neighbours regardless of how far away they are? The answer was the second. In the words of their 2008 paper, “each bird interacts on average with a fixed number of neighbours (six-seven),” a topological rule rather than a metric one. The Wikipedia article on flocking gives the same figure: starlings typically interact with at most seven topological neighbours.
The distinction matters when a flock is attacked. With a metric rule, a flock that gets stretched thin loses contact between its parts and falls apart. With a topological rule, a bird keeps tracking its six or seven nearest companions however far they spread, so the flock stays one flock through the manoeuvre. The simulator uses a metric vision radius, as the original boids do, and the difference is visible: push the predator into a dense flock with Vision set low and the flock shatters into fragments that have to find each other again.
Why starlings bother is a separate question. Flocking is generally believed to arise from the need for cover and protection from predators, which is the role the red arrowhead plays here. In winter, starlings gather in groups of hundreds to thousands, the murmurations whose swirling patterns have become the standard image of collective motion.
Making 4,000 boids run in a browser
The neighbour search, and why a grid matters
The expensive part of a flocking simulation is not the rules. It is finding the neighbours. Every boid needs the flockmates inside its vision radius, and checking every boid against every other boid costs N² comparisons per frame: for 4,000 boids that is sixteen million distance checks, sixty times a second, in JavaScript.
The simulator avoids that by dropping every boid into a uniform grid whose cell size equals the vision radius. A boid can only have neighbours in its own cell and the eight around it, so it tests those nine cells and ignores the rest. The cost falls to roughly N times the average occupancy of nine cells, which is what keeps a few thousand boids interactive without a GPU. Reynolds’s original 1987 implementation had no such luxury and ran on a Symbolics Lisp machine; the animated short that came out of it, Stanley and Stella in: Breaking the Ice, was shown at the SIGGRAPH ’87 Electronic Theater.

From Gotham to the Xen cliffs
Where boids went after the paper
The first feature film to use the model was Tim Burton’s Batman Returns in 1992, which contained computer-simulated bat swarms and penguin flocks built with modified versions of the original boids software. Andy Kopra at VIFX produced the bat swarm; Andrea Losch at Boss Films and Paul Ashdown animated the penguin army marching through Gotham. Reynolds himself was on the video image crew for the film, a decade after working as a scene programmer on Tron. Valve’s Half-Life used the model in 1998 for the flying creatures on Xen, and the game files call them “boid.”
Reynolds was not quite first. The Wikipedia entry notes that Ichiro Aoki published closely related work on schooling in 1982, four years before boids, and the model’s own page lists obstacle avoidance and goal seeking among the rules that can be bolted on once the three basics are in place. Reynolds is also the author of OpenSteer, a library of the same steering behaviours for games and animation.
In 1998 the Academy of Motion Picture Arts and Sciences gave Reynolds a Scientific and Technical Award for “his pioneering contributions to the development of three-dimensional computer animation for motion picture production.” The International Society for Artificial Life gave him its lifetime achievement award in 2023. Boids is also, as his own page notes, an oft-cited example of the principles of artificial life, the same tradition as the cellular automata covered in an earlier x9m8 article: simple local rules, complex global behaviour, and no designer of the result.

Experiments worth running
- Set Alignment to zero and leave the other two rules on. The boids clump but never agree on a direction, and the clumps mill in place like a swarm of gnats rather than a flock of birds.
- Set Cohesion to zero. Alignment alone produces long parallel streams with no reason to stay together; the flock becomes weather.
- Push Vision to 120 with 4,000 boids. Every boid sees hundreds of neighbours and the whole population moves as one slow mass. Drop Vision to 10 and it fractures into dozens of small independent flocks.
- Turn the predator off, let a single large flock form, then turn it back on. Watch how far the reaction to the predator travels through the flock, and how quickly the hole closes behind it.
- Turn Trails on, drop the count to 200, and watch the paths. The predator’s track is a chase; the prey’s tracks are a wake.
For the mathematical side of why simple rules produce complex wholes, Melanie Mitchell’s Complexity: A Guided Tour covers emergence, cellular automata and collective behaviour in one readable volume. A related x9m8 piece asks whether the same word, emergence, means anything when applied to large language models.
Complexity: A Guided Tour by Melanie Mitchell. Emergence, cellular automata, collective behaviour and the science of systems whose wholes outrun their parts. See it on Amazon (affiliate link)
As an Amazon Associate I earn from qualifying purchases. A commission may be earned if you buy through this link, at no extra cost to you.
Sources
- Reynolds’s boids page: the three rules, the 1987 paper, Stanley and Stella, Batman Returns credits, emergence: Craig Reynolds, Boids
- Boids history, the three rules, film and game uses: Wikipedia, Boids
- Reynolds’s career, Tron, the 1998 Academy award, ISAL award: Wikipedia, Craig Reynolds (computer graphics)
- The six-to-seven neighbour finding: Ballerini et al., “Interaction Ruling Animal Collective Behaviour Depends on Topological rather than Metric Distance” (arXiv 0709.1916)
- Flocking as emergent behaviour, predator-avoidance hypothesis, murmurations: Wikipedia, Flocking
- Video: Sebastian Lague, Coding Adventure: Boids
- Book: Complexity: A Guided Tour on Amazon (affiliate link)
