› Boids Flocking Simulator Back
Emergent Behavior Simulator

Boids Flocking Simulator — Emergent Collective Behavior

Fish schools and bird flocks arise from just 3 simple rules. Adjust the weights of Separation, Alignment, and Cohesion to control the nature of the flock in real time.

Parameters
120
2.5
60 px
1.5
1.0
1.0
Presets
Display options
Interaction

Click → predator / right-click → attractor / click a boid on the canvas to make it the focal boid

Live metrics
120
Number of boids
—
Mean neighbours
—
Alignment φ (0–1)
—
Mean speed
—
Real-time flocking animation

Click: add predator / right-click: add attractor / long tap: attractor

Separation Alignment Cohesion Resultant (steering) Focal boid's field of view
Theory & Key Formulas

Each boid computes three forces from its neighbours within the visual radius $r$:

  • Separation: $\mathbf{F}_s = -\sum_{j \in N_{sep}}\frac{\mathbf{r}_{ij}}{|\mathbf{r}_{ij}|^2}$
  • Alignment: $\mathbf{F}_a = \langle \mathbf{v}_j \rangle - \mathbf{v}_i$
  • Cohesion: $\mathbf{F}_c = \langle \mathbf{x}_j \rangle - \mathbf{x}_i$

Final acceleration: $\mathbf{a}_i = w_s \mathbf{F}_s + w_a \mathbf{F}_a + w_c \mathbf{F}_c$ (shown on the focal boid as the three coloured vectors above and the white resultant arrow)

Flock order (alignment): $\varphi = \dfrac{1}{N}\left|\sum_i \hat{\mathbf{v}}_i\right|$ ($\varphi\to1$: perfectly aligned, $\varphi\to0$: disordered)

Speed is clamped at $v_{max}$. Boids that reach an edge reappear on the opposite side (torus space). The neighbour search uses a spatial hash to stay $O(N)$ and keep 60 fps.

What Is the Boids Simulator?

🙋
What exactly is a "boid"? It just looks like dots moving around, so how does such complex, fish-school-like motion emerge?
🎓
Roughly speaking, each boid is a smart particle that moves by following just three rules: separation, alignment and cohesion. Look at the glowing yellow "focal boid". The red, blue and green arrows are the separation, alignment and cohesion forces, and the white arrow is their sum, the actual steering direction. Try setting "Separation weight" to 0 with the slider above. The red arrow disappears and the boids start bumping into each other, right? That is the basic collision-avoidance rule.
🙋
Really! And changing the strength of each rule changes the motion completely. Making "alignment" stronger lines them up like soldiers, and the "alignment φ" got close to 1 too. Is this used in the real world?
🎓
Exactly! φ (the order parameter) measures how well the flock is aligned: 1 means perfectly aligned, 0 means disordered. In practice it is often used to generate crowd scenes in CG and group AI for NPCs in games. In the CAE world it is also important as a foundation for particle-method simulation: the way the calculation is organised, searching only nearby particles and adding up local forces, is shared with particle methods such as SPH.
🙋
So it relates to CAE too! When I pressed "Add predator", the flock split apart and fled. Is that emergent avoidance?
🎓
Sharp! Nobody gives the order "run", yet just by each boid adding a local force away from the predator, the whole flock splits and regroups. Global order arising from purely local information is what makes "emergence" fascinating. Watch the focal boid's vectors and the "mean neighbours" value as the behaviour shifts from scattered to flock to swirl.

Frequently Asked Questions

A larger separation weight widens the spacing between individuals and makes the flock break up more easily; a larger alignment weight makes them move in one direction. Stronger cohesion tends to form dense clumps, while weaker cohesion lets the flock spread out. The red (separation), blue (alignment) and green (cohesion) vectors and the white resultant arrow drawn on the focal boid show the contribution of each rule directly.
With a small visual radius each boid only perceives nearby individuals, so the flock splits more easily. With a large radius it is also influenced by distant individuals, so large flocks that hold together form. The dashed circle and sector on the focal boid show the range it actually "sees" and its field of view.
This simulator treats the screen edges as a torus (periodic boundary), so boids reappear on the opposite side and never leave the screen. Note that with an extremely small separation weight individuals tend to overlap.
It is suited to education and demonstrations for understanding the basic principle of emergence. Reproducing the complex behaviour of real animals (obstacle avoidance, predator response and so on) requires additional rules and parameter tuning.

Real-World Applications

Computer graphics and game development:Widely used in films and games to animate natural flocks of birds, schools of fish or crowds (hordes of zombies, for example). Large crowd scenes can be created efficiently without building complex AI into each agent.

Swarm robotics:Applied as the basis of control algorithms that coordinate many autonomous robots. Research is under way, for example, on using robot swarms for search at disaster sites or for moving goods in warehouses.

Particle-method simulation (CAE):Particle-based flow analysis such as SPH (Smoothed Particle Hydrodynamics) computes local interactions between particles. The boids algorithm is useful in teaching as a prototype of this kind of neighbour search and inter-particle force calculation.

Swarm intelligence algorithms (PSO):Particle Swarm Optimization applies the boids concept to optimisation problems. Particles in the search space (candidate solutions) move towards the optimum while sharing information about their own best solution and the best solution of the whole swarm, and the method is used for engineering design optimisation and more.

Common Misconceptions and Points to Note

First, a common belief when people start playing with this simulator is that "the more extreme the parameters, the more realistic it gets". For example, setting the separation, alignment and cohesion weights all to their maximum actually produces an unnatural, twitching motion. In practice,balance is everything, depending on observation data of the animals and on your purpose. To reproduce a school of fish it often helps to make alignment slightly stronger, and for a flock of birds to make cohesion slightly weaker. The trick is to fine-tune from the default balance (for example separation 1.5, alignment 1.0, cohesion 1.0).

Next, the mistake ofoverlooking the relationship between visual radius and maximum speed. Even with a wider visual radius, if the maximum speed is too low an individual can obtain the information but cannot move enough to respond. Conversely, if the speed is too high the steering cannot keep up and the flock falls apart. For example, with a visual radius of 50 pixels, starting from a maximum speed of about 5–10% of that (2.5–5.0 pixels/step) and adjusting tends to be stable.

Finally, an implementation pitfall:do not underestimate the cost of distance calculations. Computing the distance between every pair of particles at every step becomes very slow once the number of particles N exceeds about 1000. In practice, optimising the neighbour search with spatial partitioning (a spatial grid or a quadtree) is essential (this simulator also uses a spatial hash to make it O(N)). Also watch for division errors when the number of neighbours |N| is 0 (nobody within the field of view); in that case each force should be treated as a zero vector, for example.

How to Use

  1. Set the number of boids between 20 and 300. Typical flocking is easy to observe with 150–200 (a spatial hash keeps it running at a target of 60 fps even with 300)
  2. Adjust the visual radius between 20 and 150 pixels. The smaller the radius, the more local the interactions and the faster the flock breaks into clusters. The dashed circle around the focal boid shows its actual perception range
  3. Move the separation, alignment and cohesion weights and watch the focal boid's red, blue and green vectors and white resultant arrow, together with the "alignment φ" and "mean neighbours" readouts, as the behaviour changes from scattered to flock to swirl

Worked Example

With the "Tight flock" preset (120 boids, visual radius 70 pixels, maximum speed 2.0 px/step, separation 0.9, alignment 2.0, cohesion 2.4), the mean number of neighbours is high, about 10, and because individuals jostle inside the dense flock the alignment φ usually fluctuates between 0.7 and 0.95, and can drop to around 0.4 when the flock splits into two clumps heading in different directions. With "Loose flock" (90 boids, visual radius 45, maximum speed 3.2, separation 2.2, alignment 0.8, cohesion 0.5) the mean number of neighbours drops to about 6, but the alignment force keeps acting, so within about 20 seconds φ rises to 0.85–0.99 and the sparse individuals fly in the same direction. In other words, φ measures how well the headings agree, not how dense the flock is. The values vary randomly, so move the sliders and observe the trends.

Practical Notes

🎬 Watch it in motion

Boids | three simple rules become a living flock #Shorts
Boids | three simple rules become a living flock #Shorts
A flock with no leader — Boids #Shorts
A flock with no leader — Boids #Shorts