Three rules, no leader
A school of fish has no leader, no shared map, and no fish that knows the group's overall shape — yet it turns, splits around obstacles and reassembles as one coherent body. Craig Reynolds modelled exactly this in 1986 with Boids, showing that convincing flocking emerges from just three local rules applied by every individual, using only what it can see of its nearest neighbours:
separation: steer away from neighbours that are too close (avoid crowding) alignment: steer toward the average heading of nearby neighbours cohesion: steer toward the average position of nearby neighbours each boid only looks at neighbours within its own perception radius
No fish computes the school's shape, its centre, or its heading directly — those are emergent properties that appear only when you watch the whole group, built purely from bottom-up local decisions repeated across every individual, every frame. This is the same class of phenomenon as an ant colony's foraging trails or a crowd's lane formation: complex, apparently coordinated group behaviour with no coordinator.
Weighting the three forces
Each rule produces a steering vector, and the boid's actual acceleration is a weighted sum of the three, typically with separation weighted most heavily since collision avoidance is the most urgent local concern:
steer = w_sep * separation_vec + w_align * alignment_vec + w_coh * cohesion_vec velocity += steer * dt velocity = clamp(velocity, max_speed) position += velocity * dt
Small changes in the relative weights produce qualitatively different group behaviour: heavy cohesion with light separation collapses the group into a tight, jostling cluster; heavy separation spreads it into a loose, barely-coordinated cloud; heavy alignment with the other two moderate produces the long, coherent, banking school shape most associated with real fish and starling murmurations. Real fish schools appear to weight these forces adaptively, tightening cohesion when predators are near and loosening it while foraging.
Predator response: real biology, not just aesthetics
Field biology on real schooling fish documents specific, repeatable group responses to an approaching predator that a boids-style local model reproduces surprisingly well: the fountain effect, where the school splits into two streams that flow around an attacking predator and rejoin behind it; flash expansion, a near-instantaneous radial burst outward the instant a predator strikes, driven by each fish's separation rule spiking in response to a suddenly close, fast-moving neighbour; and the selfish herd effect, first formalised by W.D. Hamilton in 1971, where each individual fish's simplest survival strategy is to minimize its own exposed surface by moving toward the geometric centre of its neighbours — cohesion, essentially, but motivated by predation risk rather than social preference, and it alone is sufficient to produce compact, tightly aggregated groups without any need for altruism or coordination.
Why 3D flocking needs an extra idea: pseudo-depth
Adding a shark hunting from below, and rendering the whole thing convincingly on a flat 2D canvas, requires a pseudo-3D trick: each fish carries a depth coordinate (commonly z, or distance-from-camera) that is not rendered as true perspective geometry but is used to scale each fish's apparent size and to offset its vertical draw position, and, critically, is included as a third dimension in the neighbour-distance calculation, so 'close' means close in x, y and z, not just close on screen. A predator attacking from below then reads visually as fish scattering both sideways and upward, exactly the flash-expansion pattern seen in real footage, without needing a full 3D rendering pipeline.
From flocks to films
Reynolds' original Boids algorithm won a Scientific and Technical Academy Award in 1998 for its influence on computer animation — it was used, adapted with extra rules, to animate the bat swarm in Batman Returns and the wildebeest stampede in The Lion King, both scenes where hand-animating thousands of individually plausible but collectively coherent creatures would have been impractical. The same three-rule core, sometimes extended with obstacle avoidance and goal-seeking terms, remains the standard starting point for crowd simulation in games and film to this day.
Frequently asked questions
Does each fish in the simulation know the shape of the whole school?
No — that is the entire point of the model. Every fish only reacts to the handful of neighbours within its own local perception radius using the three rules (separation, alignment, cohesion). The school's overall shape, its turns and splits, are emergent group behaviour with no individual fish and no central controller aware of it.
What is the 'fountain effect' seen when a predator attacks a school?
It's a documented real-world response where the school splits into two curved streams that flow around the attacking predator on either side and reform behind it. In a boids-style model it emerges naturally from the separation rule causing nearby fish to swerve away from the fast-approaching predator while alignment and cohesion keep the two split groups internally coherent.
Why does the fish school in this simulation still look 3D on a flat canvas?
The model gives each fish a depth value that isn't rendered with true 3D perspective but is used to scale each fish's size, offset its vertical position, and factor into how 'close' neighbours are judged. This pseudo-3D trick is enough to make a shark attacking from below produce a convincing sideways-and-upward scatter without a full 3D rendering engine.
Try it live
Everything above runs in your browser — open Fish School 3D and change the parameters while it is running. Nothing is installed, nothing is uploaded, the whole model lives in one tab.
▶ Open Fish School 3D simulation