Each robot i steers by summing three Reynolds rules over its local neighbor set N(i) in the 2D plane, then integrates that steering into its velocity every step:
Separation: v_sep = -Σ (p_j - p_i) / |p_j - p_i|² (push away from close neighbors)
Alignment: v_ali = mean(v_j) - v_i (match neighbors' heading)
Cohesion: v_coh = mean(p_j) - p_i (steer toward local centroid)
v_i(t+dt) = clamp( v_i + dt·(w_s·v_sep + w_a·v_ali + w_c·v_coh), v_max )
p_i(t+dt) = p_i + dt·v_i
Neighbor selection is the robotics-relevant design choice, and its geometry is genuinely different in 2D than in 3D:
- Metric model — every robot within a fixed sensing radius R counts. For robots scattered with area-density ρ = N/Area over a 2D floor, the expected neighbor count inside a disc of radius R is the Poisson-process prediction ρ·π·R² — the disc-area law. (A 3D swarm sensing a spherical volume instead follows ρ·(4/3)·π·R³, a steeper cubic scaling — the same sensing radius picks up far more neighbors per unit density increase in 3D than in 2D.) The readouts on the left measure this live: "measured / predicted" should sit close to 1.0 away from the arena walls, confirming robots really are Poisson-scattered before flocking order sets in.
- Topological model — each robot tracks a fixed number k of its nearest neighbors regardless of distance (the interaction rule measured in real starling flocks by Ballerini et al., 2008), bounding each robot's communication/compute load to O(k) and keeping the flock connected even as density varies.
The Scatter button randomly displaces a third of the swarm to simulate a density disruption (e.g. robots forced through a narrow gap). Watch the polarization order parameter — the topological model recovers coherent flocking faster because it always maintains k links per robot, while the metric model can fragment into disconnected clusters when local density drops below what the fixed radius can reach.
Polarization is the classic Vicsek order parameter Φ = |Σ v̂_i| / N (1 = perfectly aligned, 0 = disordered), used in both biology and swarm-robotics papers to quantify flocking coherence. Ground-plane robots (warehouse AMRs, ground swarms) genuinely operate in this 2D regime — their onboard 2D LIDAR or proximity ring sees a disc, not a sphere, so the πR² scaling above is the one that actually governs their sensor-to-neighbor-count relationship, not the 3D formula used for aerial swarms.