technique

Flocking

Each agent continuously turns toward its single closest neighbor by a fixed angle increment, producing an emergent flock-like swirl from many instances.

Also called: nearest-neighbor flocking · single-neighbor flocking · boids · Flocking Simulation · separation alignment cohesion

What it is

Flocking names three separate sources in this corpus, plus five aliases the registry attaches to them. Nature of Code’s own definition: flocking “is a group animal behavior found in many living creatures, such as birds, fish, and insects” and, separately, “to describe the elements of a flocking system.” The Coding Train — Daniel Shiffman’s coding-challenge site — contributes the alias Flocking Simulation and four short excerpts: “Discussing the flocking algorithm,” and the exclamations “Separation!”, “Alignment!”, “Cohesion!” Happy Coding’s page contributes two words: “flocking simulation.” The registry treats all three as one entity, but the evidence behind them is uneven: two possible mechanisms are described below, and only one of them is well evidenced.

How it works

The three-force version — Nature of Code. Three separate steering rules, computed for every boid from its neighbours:

  1. Separation — “Steer to avoid colliding with your neighbors.”
  2. Alignment — “Steer in the same direction as your neighbors.”
  3. Cohesion — “Steer toward the center of your neighbors (stay with the group).”

The Coding Train’s excerpts include the same three words as isolated exclamations — “Separation!”, “Alignment!”, “Cohesion!” — alongside “Discussing the flocking algorithm.” Nothing in this bundle records where those exclamations sit in the source or in what order; they’re evidence The Coding Train covers the same three terms, not confirmation of a shared step-by-step structure.

One further excerpt — “Add all the vectors together and increment the count.” — isn’t tied to any one of the three rules above in the evidence captured here. Whether it applies to each rule separately, what happens to that running sum, and whether the three rules combine into a single steering force afterward, isn’t recorded. Treat all of that as open.

A second mechanism, aliased but unconfirmed. The registry attaches two further aliases here — “nearest-neighbor flocking” and “single-neighbor flocking” — both sourced to Happy Coding, and registers flocking as a variant of seek. Seek’s own record describes an agent that rotates its heading a fixed amount per frame toward the angle to a target point — nearest neighbour or the mouse — then moves forward along that heading. If that’s what the two aliases point at, it would be a single-neighbour reduction of the three-force version above: one target instead of three summed rules. But the only excerpt this bundle captured from Happy Coding’s own page is the two words “flocking simulation,” with nothing to check that mechanism against. Whether Happy Coding’s page actually describes this, the three-force version, or something else again is open — read it directly to settle it.

Parameters & tuning

This bundle gives no relative-weighting numbers between the three rules above, and no confirmation that they’re combined with weights at all rather than simply summed — nor any figure for how large a group of “neighbors” should count. If the second, aliased mechanism above matches seek’s description, its only knob is the size of the per-frame turn angle, and nothing here says what raising or lowering it does to the picture. Both are gaps in this evidence set, not settled choices — pick numbers experimentally rather than assume a canonical value.

Where it’s been used

Everything in this bundle is teaching material, not a named artwork: Nature of Code’s autonomous-agents chapter, The Coding Train’s page on flocking, and Happy Coding’s page. No specific piece or artist that used flocking appears in the evidence gathered for this page — an open gap worth filing once a source naming one turns up.

Variants & neighbours

Flocking is registered as a variant of seek: seek’s own record describes an agent that rotates its heading a fixed amount per frame toward the angle to a target point — nearest neighbour or the mouse — then moves forward along that heading. The three-force version above differs by summing contributions from every neighbour rather than steering at just one target.

It contrasts with wander: wander steers toward a target that moves randomly around a circle projected ahead of the agent, and the registry describes that projection as producing directionally-ordered motion rather than purely random drift — so wander isn’t aimless either. The difference is what supplies the direction: wander’s circle has no relationship to any other agent, while flocking’s direction comes from real neighbour positions and headings.

It also contrasts with arrive, which decelerates smoothly inside a slowing radius instead of overshooting its target — nothing in this bundle says whether the three-force rules above include any comparable deceleration.

And with path following: it predicts a vehicle’s future position, finds the nearest point on a path, and seeks a target a little ahead of that point only if the vehicle has strayed too far. Flocking’s direction instead comes from other agents’ current positions and headings, recomputed every frame rather than measured against a path.

All of this sits inside the wider idea of steering force: flocking is built from it, and steering force’s own definition is a general formula — subtract an agent’s current velocity from a desired velocity to get the force that nudges it toward that desire. This bundle doesn’t confirm that each of the three named rules above runs through that exact subtraction; the link holds at the level of the whole technique, not verified rule by rule.

Go deeper

Connected to

Variant of: Seek steering behavior

Further reading: The Coding Train

Built from: Steering force

Try it yourself

One boid class computes three separate vectors each frame — one steering away from anything too close, one steering in the same direction as nearby boids, one steering toward the center of nearby boids. A different boid class has no such method: each frame it just measures the angle to whichever other boid is nearest and rotates a fixed amount toward it. Which one do the Nature of Code excerpts describe, and which one matches what seek's own record already looks like?

The first matches Nature of Code: the excerpts name three rules — steer to avoid colliding, steer in the same direction, steer toward the center. The second matches seek's own description — rotate the heading a fixed amount per frame toward the angle to a target, here the nearest boid instead of a fixed point or the mouse. Whether any of the three sources in this corpus actually implements that second version isn't confirmed by the excerpts captured here.

Three separate vectors versus a single heading nudging toward one other boid's angle, frame after frame.

The registry sources the aliases 'nearest-neighbor flocking' and 'single-neighbor flocking' to Happy Coding, and a separate alias, 'Flocking Simulation,' to The Coding Train. The only excerpt this bundle captured from Happy Coding's own page is the phrase 'flocking simulation.' Does an alias — or an excerpted phrase — tell you what algorithm a source's page actually runs?

No. The registry's aliases record names associated with a source, not necessarily words the source used verbatim — 'nearest-neighbor flocking' and 'single-neighbor flocking' don't themselves appear in any excerpt captured here. The excerpted phrase is just two words pulled from the page, with no surrounding context. Neither tells you what mechanism the page actually describes. Confirming that means reading it directly, not reasoning from its aliases or its wording.

No picture changes here — the ambiguity is about which source teaches what, not about anything the algorithm draws.


Revision 4 · 3 sources · Something wrong? Tell us.