Each robot i steers by summing three Reynolds rules over its local neighbor set N(i), 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. A real robot cannot see the whole swarm — it must decide who counts as a "neighbor" from onboard sensing:
- Metric model — every robot within a fixed sensing radius R counts, as in classic Reynolds boids and simple proximity-sensor robots. When local density is high, neighbor count explodes; when it's low, a robot can lose all neighbors and drift off alone.
- 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, and adopted in swarm-robotics research because it bounds each robot's communication/compute load to O(k) and keeps 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.