Flocking LabBeta
Watch bird flocks and fish schools emerge from just three rules
This tool is a beta version still being stabilized. Please let us know if a result looks off.
Each boid sees only the neighbors within its vision radius and field of view (narrow the angle and it stops seeing those behind it) and follows three rules: steer away when closer than the separation radius (separation), fly the same way as neighbors (alignment), and move toward their center (cohesion). Nobody leads, yet a flock emerges. Press or drag on the canvas to become food or a predator. Order is 1 when everyone heads the same way and near 0 when they scatter.
Working…
0.0
It hasn't frozen — the result will appear when the calculation finishes.
About Flocking Lab
Every dot here is a “boid”, the model Craig Reynolds presented at SIGGRAPH in 1987. Each boid sees only neighbours inside its vision radius (50 pixels by default) and does three things: it moves away from neighbours that are too close (separation), steers towards their average heading (alignment), and steers towards their average position (cohesion). Nothing in the code describes the flock's shape or appoints a leader.
The “Order” meter adds up every boid's unit heading vector and divides the length of the sum by the number of boids. If all fly the same way it reads 1; if headings are random it is close to 0. It turns “a flock has formed” into a number you can watch.
How the three rules are computed
Each rule produces one desired direction. Alignment uses the average velocity of neighbours; cohesion uses the average offset to them; separation points away from neighbours inside the separation radius (20 pixels by default), each weighted by one over the squared distance. For each direction the engine works out the velocity change needed to fly that way at top speed, caps it at the maximum steering force (one thirtieth of top speed), multiplies by the slider weight and adds it up. Separation gets an extra internal factor of 1.5.
Speed is kept between the maximum and one third of it: with the default 180 that is 60 to 180 pixels per second, so boids can never hover. Neighbour search divides the canvas into cells the size of the vision radius and checks only the surrounding nine, which is why even 5,000 boids run in real time.
Experiment 1: set alignment to zero
Start from the default flock, wait until Order climbs above 0.9, then drag Alignment to 0. The boids still clump, but headings inside each clump scatter and Order drops sharply. For 1,500 boids pointing in truly random directions you would expect about 1/√1500 ≈ 0.03.
Now try the opposite: cohesion and separation at 0, alignment alone. The boids do not gather, yet their headings slowly line up. That setup resembles the self-propelled particle model Vicsek and colleagues proposed in 1995, which showed that matching neighbours' directions plus noise is enough to produce a transition from disorder to collective motion.
Experiment 2: vision radius sets the size of the flock
Drop the vision radius to 10 and most boids have no neighbours at all. Small groups form, drift in different directions, and global Order stays low even though each group is internally aligned. Raise it to 150 and within seconds everything merges into one big flock.
Expected neighbour counts explain it. On a 900×560-pixel canvas, 1,500 boids spread evenly give a density of 1,500 ÷ 504,000 ≈ 0.003 boids per square pixel. A radius of 50 covers about 7,854 square pixels, so roughly 23 neighbours; a radius of 10 covers 314, fewer than one. With no neighbours, none of the three rules produces any force.
Experiment 3: a predator and walls
Set the pointer to “Predator” and hold it in the middle of a flock. Boids within 160 pixels flee and the flock splits in two; release, and cohesion pulls them back together. It looks a lot like starlings dodging a falcon, but all that is happening is one extra “away from here” direction added to each boid's steering.
Switch the edges to walls and a push towards the centre appears within 50 pixels of the border. Flocks slide along walls and turn often, so Order swings much more than in wrap-around mode.
What this simulation leaves out
- The field of view defaults to a full 360° circle. Reynolds' original boids could not see directly behind; lower the field-of-view slider to get the same effect. At 270°, for example, neighbours in the 90° wedge behind a boid are ignored.
- The three forces are summed with weights. The 1987 paper described prioritised arbitration, where urgent needs such as avoiding a collision are satisfied first.
- It is 2D, with no obstacle avoidance, energy or flight mechanics. The steering cap and minimum speed are chosen for looks, not measured from animals.
Worked examples
Order with random headings
- Input
1,500 boids, alignment 0, cohesion 0- Output
About 0.03
The sum of N random unit vectors has length around √N, so √1500 ÷ 1500 ≈ 0.026.
Expected neighbours by radius
- Input
900×560 canvas, 1,500 boids- Output
Radius 50 → about 23; radius 10 → about 0.9
Density 0.00298 × π × 50² ≈ 23; × π × 10² ≈ 0.9.
Speed range
- Input
Max speed 180- Output
60–180 px/s, steering cap 6
Minimum speed is one third of the maximum; steering cap is one thirtieth.
How to use it
- Pick a flock type (Flock, School, Swarm, Chaos) as a starting point.
- Move separation, alignment, cohesion, vision radius, separation radius and field of view one at a time and watch Order.
- Press or drag on the canvas to act as food or a predator.
Frequently asked questions
What does “boid” mean?
Reynolds' shortening of “bird-oid”, a bird-like object. The model appeared in his 1987 SIGGRAPH paper “Flocks, Herds, and Schools: A Distributed Behavioral Model”.
Do real birds follow these three rules?
The model is not a replica of a bird's brain; it is a minimal rule set that reproduces flock-like motion. The key idea, that large coordinated groups can arise from each animal reacting only to nearby neighbours, is central in real studies of collective animal behaviour.
With no leader, why do they all end up going one way?
Alignment makes neighbours' headings slightly more similar, and that similarity spreads through neighbours of neighbours. On a wrap-around screen nothing interrupts it, so a single common direction usually wins.
Are boids used in films and games?
Computer animation was the paper's purpose: instead of scripting every bird's path, give each one rules and simulate the flock. That approach became standard in visual effects and games.
Related guides
Sources
- C. W. Reynolds, Flocks, herds and schools: A distributed behavioral model, SIGGRAPH '87, Computer Graphics 21(4), 25–34 (1987)
- T. Vicsek et al., Novel type of phase transition in a system of self-driven particles, Phys. Rev. Lett. 75, 1226 (1995)
Last reviewed: