loading...
running...
How to use
Press a preset or move the sliders: the three rule strengths separation, alignment and cohesion decide whether you get a tight school, a loose swarm or a rotating mill. Watch the polarization and milling curves, the nearest-neighbour distance (it should hover around the separation radius) and the speed histogram. Drag on the field to place a hawk; Drag to orbit, wheel to zoom. Space pauses, R resets.
Experiments: (1) set alignment to 0: polarization falls to the random level (grey line); (2) raise noise to 8 and watch the order collapse, then lower it again; (3) narrow the field of view to 120 degrees; (4) turn on six pillars and see the flock split and re-merge; (5) press Scatter headings and measure how long the flock needs to re-align as a function of the alignment rate.
The sea stacks, rocky shore, reeds, sea surface, the bird (body and two flapping wings) and the hawk were modelled in Blender 5.2 with baked PBR textures and exported as a Draco-compressed GLB; the flock dynamics is exactly the model of the 2D page.
The science
Boids (Craig Reynolds, 1986/87) show that a convincing flock needs no leader and no global plan: every bird follows three local steering rules using only the neighbours it can see within a vision radius rv and a field of view. The acceleration of bird i is
ai = wS ∑d<rs (1 - d/rs) (ri - rj)/d + wA (<vj> - vi) + wC (<rj> - ri) + wind + walls + pillars + hawk
Separation pushes away from neighbours closer than rs with a force that grows linearly towards contact. Alignment relaxes the velocity towards the neighbour average, so the velocity difference of two interacting birds decays as exp(-2 wA t) (the in-page check measures this). Cohesion pulls towards the local centre of the visible neighbours. Speed is clamped between vmin (birds cannot hover) and vmax, and the steering acceleration is capped at fmax. Integration is semi-implicit Euler at 1/60 s with all accelerations computed from the old state, so the update is symmetric.
Order parameters. The polarization is the length of the mean heading φ = |∑ vi/|vi|| / N: 1 for perfect alignment, and for N uncorrelated headings its expectation is √π / (2√N) in 2D and √(8/(3π)) / √N in 3D, which is the small value drawn as the grey line (checked numerically with 1500 random draws). The milling order |∑ (ri - rc) × v̂i| / ∑|ri - rc| is large when the flock rotates around its centre as a ring, a state that real fish schools and ant mills also show. Adding random kicks of strength η reproduces the order-disorder transition of the Vicsek model: low noise gives a polarized flock, high noise a disordered cloud.
Efficiency. Looking at all N-1 others costs O(N2). The page uses a spatial hash grid with cell size at least rv, so only 3 cells per axis are visited and the cost is proportional to N times the neighbours in view. A check compares the grid result with the brute-force double loop on 450 boids in a periodic 3D box (identical pair counts).
Predator. Birds within the alarm distance accelerate away from the hawk by wH (1 - d/R) and may fly 35 % faster while alarmed. The hunting hawk simply steers towards the nearest bird (pure pursuit) and cannot catch anything: it only disturbs the flock, so the page is about collective motion, not about predation.
Honest simplifications. Birds are point masses with a heading, no banking, wing-beat or aerodynamic model (the 3D wing flapping is cosmetic). Real starlings interact with a fixed number of about 7 topological neighbours rather than a fixed radius, and real flocks are far larger; this model uses a metric radius because it is the classic version. The soft walls and pillars are numerical devices, not physics. No wind field varies in space.
In-page checks. (1) grid neighbour search equals brute force; (2) separation is pairwise equal-and-opposite so total momentum is conserved to about 1e-13 m/s; (3) the alignment relaxation of two birds follows exp(-2 wA t); (4) and (5) the order parameter of random headings matches the closed forms in 2D and 3D; (6) no bird ends up inside a pillar or outside the box after a long run with a hunting hawk.