Boids

step 0
Loading the engineShared by all simulations, so the next one opens instantly.
polarization Φ·
mean cluster·
The physics

In 1987 Craig Reynolds showed that a convincing flock needs no leader and no plan. Each simulated bird, or boid, reacts only to neighbors within a short range: it steers away from those that are too close, turns toward their average heading, and heads for their center. Flocks and schools emerge from these local rules alone.

Governing equation

For each rule, boid i picks a desired direction 𝐝ˆ from the neighbors within that rule’s radius and inside its field of view:

𝐝ˆsep∝∑j𝐫i−𝐫j|𝐫i−𝐫j|2,𝐝ˆali∝∑j𝐯j,𝐝ˆcoh∝1|𝒩︀i|∑j𝐫j−𝐫i.

Following Reynolds, the steering for each rule is the change of velocity needed to fly that way at top speed, 𝐬=vmax𝐝ˆ−𝐯i, cut down to length Fmax if longer. Each step the velocity becomes

𝐯i←𝐯i+ws𝐬sep+wa𝐬ali+wc𝐬coh+𝜼,

where 𝜼 is a random kick, and the speed is then held between 0.1vmax and vmax. The polarization Φ=|⟨𝐯i/vi⟩|∈[0,1] measures how well the whole group is aligned.

What you see

Arrow-shaped boids fly over a square field whose edges wrap around, optionally with trails. The readouts under the canvas show the polarization Φ and the mean cluster size, the average number of boids within each boid’s cohesion radius.

Classic flock: 120 boids with separation, alignment and cohesion weights of 1.5, 1 and 1 and little noise. Groups form, merge and split.

Tight school (high cohesion): 200 boids with cohesion weight 3, stronger separation and alignment, and less noise, giving dense, well aligned groups.

Chaotic swarm (high noise): 150 boids with weak alignment and cohesion and five times the noise. The random kicks outweigh the steering, so Φ stays low.

Blind-spot flocking (FOV=200°): like the classic flock, but each boid ignores neighbors in the 160° behind it.

With predator: switches on the flee rule, and the pointer becomes the predator. Move it over the field: boids within the flee radius steer away from it, harder the closer it is.

Parameters

boids: the number of boids, 5 to 400.

max speed: the speed limit vmax.

max force: the steering limit Fmax; lower values make turns sluggish.

separation force, sep. radius: weight ws and range of the separation rule, which keeps boids from crowding.

alignment force, ali. radius: weight wa and range of the alignment rule, which drives the polarization.

cohesion force, coh. radius: weight wc and range of the cohesion rule, which keeps the group together.

noise: the length of the random kick as a fraction of the maximum speed, 0 to 0.3.

field of view °: the vision cone in degrees; below 360° each boid has a blind spot behind it.

wrap boundary: whether boids leaving one edge come back at the opposite one.

flee force, flee radius px: strength and range of the flee rule, shown when predator is on.