Boids Flocking
In 1986 Craig Reynolds showed that three steering rules — avoid collisions, match neighbours' headings, stay with the group — are sufficient to produce flocking that is visually indistinguishable from starling murmurations or schooling fish. No leader. No global plan. Pure emergence.
The three rules
Each boid perceives its local neighbourhood and applies three independent steering forces, blended by their respective weights:
What to watch for
The order parameter φ in the readout is the Vicsek order parameter: the magnitude of the mean unit velocity vector. φ ≈ 1 means the entire flock moves as one; φ ≈ 0 means completely disordered motion. Tune the steering weights and watch φ undergo what looks very much like a phase transition — there's a threshold coupling at which coherent flocking crystallises from random motion.
Zero out alignment and the flock mills in tight circles. Zero out cohesion and boids scatter into independent streams. The Murmur preset maximises alignment to mimic the long-range correlations observed in real starling murmurations, where each bird is statistically coupled to approximately seven neighbours regardless of flock size.
Implementation
Steering forces use Reynolds' "desired velocity minus current velocity" formulation,
capped at maxForce to prevent sharp turns. The simulation wraps toroidally
so boids seamlessly cross canvas edges; neighbour distances are computed in the toroidal
metric to avoid boundary artifacts. O(N²) neighbour search — at N ≤ 350 this runs
comfortably at 60 fps.