boids

how this works

Nobody wrote the flock. There is no plan, no leader, and no path for the group to follow. Every bird on that screen is running the same three instructions, checking only the neighbours close enough to see, and the shape you are watching is what falls out of that — a whole made entirely of parts that have no idea the whole exists.

The idea is Craig Reynolds', from 1986. He was after believable flocking for computer graphics and found that three rules were enough. They are still enough.

The three desires

Each rule is an urge pulling on a bird every single frame. None of them wins outright; the bird steers along the sum of all three, and the character of a flock is decided by which urge is loudest.

SeparationHow much they want to spread out — steer away from the birds it can see crowding too close.
AlignmentHow much they want to fall in line — match the heading of the birds it can see.
CohesionHow much they want to stay together — drift toward the middle of the birds it can see.

Those three sliders have no units, because there is nothing to measure them in. What matters is not how big one of them is but how big it is next to the other two. So instead of a number they report appetite, on one scale shared by all three:

indifferentreluctantwillingeagerdesperate

Which means a preset can be read as three words. Here are the five, in order of spread out · fall in line · stay together:

Flockeager · willing · willing
Swarmreluctant · reluctant · eager
Schooleager · desperate · willing
Gnatsdesperate · indifferent · reluctant
Crystaldesperate · desperate · reluctant

Read the Gnats row aloud — desperate to spread out, indifferent to direction, reluctant to gather — and you have described a cloud of gnats completely. Crystal differs from it by one word, and that one word is the whole difference between a jittering cloud and a lattice.

What a bird can see

Notice that all three rules end the same way: the birds it can see. That is not a turn of phrase, it is the mechanism. A bird has no idea the flock exists. It knows only its own small neighbourhood, and every urge it feels is computed from that handful of neighbours and nothing else.

Vision sets how big that neighbourhood is, which makes it the most powerful control on the panel — it does not change any rule, it changes who every rule is about. Widen it and news travels across the whole population, so they move as one body. Narrow it and the flock loses the ability to agree with itself, and breaks into separate groups that drift past each other like strangers.

It is measured in body lengths, not pixels, because that is the only scale a bird would recognise — and it is the unit the people who study real flocks use for exactly that reason. At the default, a bird watches about six birds' worth of space around itself.

The rest of the dials

Flock size
How many birds. Moving it starts a fresh flock rather than adding to the one on screen, because the birds have to be given somewhere to be.
Trails
How long the path behind each bird stays on screen before fading. Long trails turn the flock into a picture of where it has been; short ones show you only where it is.
Speed
How fast the birds fly, in body lengths per second. They all travel at exactly the same speed — the rules turn birds, they never hurry them.
Pause, and stepping
Speed sets how fast time runs; these stop it and move it by hand. Stepping forward runs one frame. Stepping back rewinds through frames that have been kept, which is a few hundred with a small flock and fewer with a large one — the birds are remembered, not re-derived.

Why they change colour

Every bird is coloured by one thing: how many neighbours it can see right now.

aloneseven or more

The ramp saturates at seven, and that number is not a design choice. In 2008 the STARFLAG project reconstructed the three-dimensional positions of thousands of starlings over Rome and found that each bird attends to a roughly fixed number of neighbours — six or seven — rather than to everything within a fixed distance. So a bird burning hot on your screen is seeing a real starling's neighbourhood.

Watch the colour move rather than the birds, and you can see the crowding travel through the flock as a wave.

Keeping what you make

Every setting lives in the page's own address. The field at the bottom of the panel always shows the link to exactly what is on screen — drag a slider and it updates. Send that link to someone and they open your flock, not the default one.

Two more conveniences: space stops and starts the clock, and escape puts the panel away when you would rather just watch. The ☰ in the corner brings it back.