SimuVeda/ Computer Science

Computer Science · Simulator 056

Flocking

Each bird (“boid”) follows three simple rules about its nearby neighbours only. There is no leader and no plan, yet the group swirls and flows like a real flock. Turn each rule up or down, release a predator, and see which behaviour each rule controls.

CollegeBeyond the syllabus: complex systems

Experiments 0 of 0 Challenge not tried

How the model works

Craig Reynolds' boids (1987) steer by three local rules, each a small force computed from neighbours within a vision radius:

  • Separation: steer away from neighbours that are too close (avoid crowding).
  • Alignment: steer towards the average heading of neighbours.
  • Cohesion: steer towards the average position of neighbours.

Speeds are kept between a minimum and maximum, and the world wraps around. The order parameter (length of the average heading vector, from 0 = random to 1 = all aligned) measures how coordinated the flock is.

A predator adds a strong flee force for boids within its range, producing the splitting and re-forming seen in starling murmurations and fish schools. Lookups use a spatial grid so hundreds of boids stay fast.

An agent-based illustration, not a calibrated model of any species. Starting positions use a fixed seed.

Guided experiments

  1. All three rules. Default settings. Prediction: within seconds, scattered boids form moving flocks, and the order parameter rises towards 1.
  2. No alignment. Set alignment to 0. Prediction: boids clump but mill around in a swarm with no common direction. Order stays low.
  3. Predator. Turn on the predator. Prediction: the flock parts around the predator and re-forms behind it.

Challenge

Which single rule would you remove to make boids crash into each other, and which to make them never gather into groups?

Reveal the explanation

Without separation, cohesion and alignment pull boids on top of each other, so they collide. Without cohesion (and with little alignment), nothing draws them together, so they stay spread out. Try each with the sliders.

References

  • Reynolds, C. W. (1987), “Flocks, herds and schools: a distributed behavioral model”, SIGGRAPH '87, 25–34.
  • Ballerini et al. (2008), “Interaction ruling animal collective behavior…”, PNAS 105: 1232–1237.

Related simulators

Worked example

Measuring order: the order parameter is the length of the average heading. Four boids heading east, east, north and south: the unit vectors add to (2, 0), length 2, divided by 4 gives 0.5.

All aligned: 1. Random headings: about 1/√N, so roughly 0.08 for 150 boids.

Cohesion: a boid whose neighbours are at (10, 20), (30, 0) and (20, 40) steers towards their centre, (20, 20).

How many neighbours? A vision radius of 50 px covers π × 50² ≈ 7 850 px² of the flat 800 × 500 view. With 150 boids spread evenly that is 150 × 7 850 / 400 000 ≈ 3 neighbours each, enough for local rules to link into a flock. With 400 boids it is about 8.

Start with 150 boids and alignment 1, then set alignment to 0 and watch the order drop towards 0.08.

Common questions

What are the three boids rules?

Separation: steer away from neighbours that are too close. Alignment: match the average heading of neighbours. Cohesion: steer towards their average position.

Why is flocking called emergent behaviour?

No boid knows about the flock or follows a leader. Each one reacts only to a few nearby boids, yet the whole group moves as one.

Where are boids used?

In films and games to animate crowds, birds and fish, in robot swarms and drone shows, and in research on how real animals coordinate.

Keyboard shortcuts

Space
Play or pause
S
Step
R
Reset
F
Full screen
?
Show this list