1. Overview & Theoretical Physics
The Boids Flocking Simulator implements a multi-agent vector synthesis architecture pioneered by computer graphics researcher Craig Reynolds in 1986. The core paradigm models biological self-organization observed across avian murmurations, marine ichthyological shoaling, and bacterial colony swarm intelligence. Rather than prescribing global trajectory paths through centralized computation, high-level group coherence emerges organically from three localized mathematical rules executed by individual autonomous entities ($boids$).
Each individual agent continuously samples its localized spatial neighbourhood bounded by a perception sphere radius $r_p$. By evaluating the spatial vectors and velocity scalars of nearby neighbors, the agent synthesizes a localized steering force $F_{steer}$. This force modifies the agent's velocity vector $\mathbf{v}$ subject to strict acceleration limits $F_{max}$ and top velocity bounds $v_{max}$.
The primary vector differential equations governing classic flocking dynamics are:
$$\mathbf{F}_{net} = w_s \mathbf{S}_i + w_a \mathbf{A}_i + w_c \mathbf{C}_i + w_p \mathbf{P}_i$$
Where $w_s, w_a, w_c, w_p$ represent the user-configurable weight scalars for Separation ($\mathbf{S}_i$), Alignment ($\mathbf{A}_i$), Cohesion ($\mathbf{C}_i$), and External Field Dynamics ($\mathbf{P}_i$). In natural biological settings, these parameter weightings fluctuate depending on metabolic states, predator stress, and environmental turbulence, causing sudden phase shifts between disordered scattering, orderly aligned streams, and dense toroidal swarm vortices.
3. Technical Details & Mathematical Formulation
To calculate steering forces efficiently without falling into $O(N^2)$ brute-force performance bottlenecks at higher agent counts, this simulator uses spatial grid binning. The 2D canvas workspace is partitioned into uniform sub-cells of width $r_p$. Neighbor searches evaluate only the current cell and its 8 adjacent grid cells, reducing compute complexity towards $O(N)$.
A. Mathematical Force Derivations
For a given boid $i$ at position $\mathbf{r}_i$ with velocity $\mathbf{v}_i$, and its set of perceived neighbors $N_i$:
1. Separation Vector ($\mathbf{S}_i$):
$$\mathbf{S}_i = \sum_{j \in N_i, d_{ij} < r_s} \frac{\mathbf{r}_i - \mathbf{r}_j}{\|\mathbf{r}_i - \mathbf{r}_j\|^2}$$
Separation applies an inverse-square repulsion force relative to distance $d_{ij} = \|\mathbf{r}_i - \mathbf{r}_j\|$, preventing physical clustering collisions.
2. Alignment Vector ($\mathbf{A}_i$):
$$\mathbf{\bar{v}} = \frac{1}{|N_i|} \sum_{j \in N_i} \mathbf{v}_j, \quad \mathbf{A}_i = \text{Steer}(\mathbf{\bar{v}} - \mathbf{v}_i)$$
Alignment computes the mean velocity vector of neighbor agents and computes the delta necessary to match their heading.
3. Cohesion Vector ($\mathbf{C}_i$):
$$\mathbf{\bar{r}} = \frac{1}{|N_i|} \sum_{j \in N_i} \mathbf{r}_j, \quad \mathbf{C}_i = \text{Steer}(\mathbf{\bar{r}} - \mathbf{r}_i)$$
Cohesion identifies the neighbor center of mass $\mathbf{\bar{r}}$ and generates a seeking vector pointing directly toward it.
4. Global Swarm Order Parameter ($\Phi$):
$$\Phi = \frac{1}{N} \left\| \sum_{k=1}^N \frac{\mathbf{v}_k}{\|\mathbf{v}_k\|} \right\|$$
The order metric $\Phi \in [0, 1]$ displayed on the diagnostic HUD quantifies group alignment. A value near $1.0$ indicates perfect parallel flocking stream dynamics, whereas $0.0$ signifies chaotic, uncoordinated Brownian-like motion.