Flocking Bird Sandbox
Explore emergent flocking. Lead a flock with your cursor and tune separation, alignment and cohesion to see collective motion emerge from simple local rules.
Getting started
This sandbox demonstrates emergent collective motion using a flock of simple autonomous agents. Each bird only responds to nearby birds and the position of your cursor, yet the group can organise into coherent, fluid motion without any bird knowing the shape or direction of the flock as a whole.
When the page first loads, the birds sit irregularly along the bottom of the sandbox. Move your cursor into the black area to disturb them: they take off and begin following it.
- Move the cursor slowly through the sandbox and watch the flock reorganise around the moving target.
- Increase Separation to make nearby birds repel one another more strongly. The flock becomes more dispersed and collisions are avoided more aggressively.
- Increase Alignment to make each bird match the direction of its neighbours more strongly. The flock tends to form smoother, more coordinated streams.
- Increase Cohesion to make birds steer more strongly towards the local centre of nearby birds. The flock contracts into tighter groups.
- Try reducing one term almost to zero. The characteristic behaviour of the flock changes dramatically even though the other rules remain unchanged.
The interesting behaviour comes from the interaction between the three rules rather than any one rule in isolation.
The mathematics
The model is based on the classic boids idea: each bird is an agent with a position and velocity . At each update it examines neighbouring birds within a perception radius and constructs three steering terms.
For bird , let denote the set of nearby birds.
Separation
Separation pushes the bird away from neighbours that are too close:
where
The repulsion smoothly fades to zero at the separation radius rather than switching off abruptly.
Alignment
Alignment makes the bird turn towards the local average velocity:
with
Nearby birds contribute more strongly than birds near the edge of the perception radius.
Cohesion
Cohesion first calculates the weighted local centre of the flock,
then steers bird towards .
The neighbour weighting used by the sandbox is approximately
where is the perception radius.
The three flocking terms are combined with steering towards the cursor:
Here , and are the Separation, Alignment and Cohesion slider values. They are dimensionless steering weights, not physical constants.
steers the flock towards the cursor. A small tangential term, , encourages birds to flow around the target rather than collapsing onto a single point. Soft edge steering keeps birds inside the sandbox.
The model is inspired by Craig Reynolds’ original distributed flocking model: Boids.
How it is computed
The sandbox normally contains roughly 120–215 birds, with the exact population depending on canvas area. Each bird stores its two-dimensional position, velocity, steering acceleration, heading and wing-animation phase.
A naive flocking model would compare every bird with every other bird, requiring approximately neighbour checks. Instead, the sandbox divides the canvas into a spatial hash grid. Birds are inserted into cells with a width equal to the perception radius, and each bird only checks its own cell and the eight surrounding cells.
The main neighbourhood radius is approximately 76 px, while the stronger close-range separation rule acts inside approximately 27 px.
The simulation uses a fixed physics timestep of
on standard displays. Fixed-step integration prevents changes in browser frame rate from directly altering the flock dynamics. If reduced-motion mode is enabled, the simulation uses a lower update rate and fewer birds.
Steering acceleration is low-pass filtered before it changes velocity:
where
This removes high-frequency steering jitter while preserving a responsive flock. The direction of each bird is also limited to a maximum turn rate, so an agent cannot instantaneously reverse direction.
Bird speed is constrained to approximately 82–205 px/s. Rendering is extrapolated slightly beyond the latest physics state and the displayed heading is smoothed independently, which helps the motion remain visually continuous on high-refresh-rate screens.
Before the first interaction, the physics loop is effectively dormant and the birds are placed irregularly along the lower edge. Entering the sandbox launches them with slightly different initial upward velocities. From that point onwards, the cursor becomes a moving target for the flock.
The flapping wings are purely visual. Their phase differs between birds and their frequency increases slightly with flight speed; the wing animation does not feed back into the flocking equations.
What the model assumes
This is an agent-based behavioural model, not an aerodynamic simulation of bird flight.
Each bird is treated as a point-like agent moving in two dimensions. There is no lift, drag, gravity, body mass, energy expenditure, wake interaction or wing-generated aerodynamics. Birds do not explicitly collide; separation simply makes close approaches increasingly unlikely.
Neighbour awareness is isotropic: a bird can respond to agents around it rather than having a biologically realistic field of view. There are no obstacles, predators, leaders or communication delays. The cursor acts as an artificial moving attractor added to the standard flocking rules.
The slider values control the relative strengths of behavioural rules. They should therefore be interpreted as model parameters, not measurable properties of a real flock.
Most importantly, the global structure is never prescribed. No bird is told to form an arc, stream, cluster or rotating group. Those patterns arise from repeated local interactions between neighbouring agents.