No fish is in charge
A school of fish turning together in a smooth, coordinated wave looks choreographed, but there is no leader fish directing the group and no central plan. Each fish is reacting only to its nearby neighbours, using a small set of simple local rules — and the striking group-level behaviour emerges from thousands of these local, independent decisions happening at once. This kind of self-organising group motion is called flocking, and the classic computational model for it is Boids, created by computer scientist Craig Reynolds in 1986.
Reynolds proposed just three rules, each applied only to a fish's near neighbours within some visual radius:
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
Balance these three forces — usually with separation weighted a bit more strongly, so fish don't collide — and update every fish's velocity a small amount each frame based on only its local neighbourhood, and the group as a whole starts turning, splitting around obstacles and reforming, and rippling in waves, all without any fish knowing the shape of the whole school.
Why real fish actually do this
Schooling offers real fish measurable survival advantages, which is why the behaviour evolved independently across so many species. A tight, fast-moving school makes it harder for a predator to target any single individual — a phenomenon researchers call the confusion effect — and a sudden, synchronised turn away from a threat can propagate through a school as a visible wave in a fraction of a second, far faster than any one fish reacting alone. Swimming near others can also reduce drag for some fish (similar in principle to birds flying in formation) and improves the group's collective ability to detect food or danger, since many pairs of eyes and lateral-line sensors cover more area than one.
Sensing without eye contact: the lateral line
Fish do not rely on vision alone to track their neighbours' position and speed. Running along each side of a fish's body is the lateral line, a row of sensory organs that detect tiny pressure changes and water movement caused by nearby fish swimming. This lets a fish sense a neighbour's motion even in murky water or from slightly behind, where it cannot be seen directly — which is part of why schools can hold tight formation and react in unison even at night or in cloudy water.
Adding obstacles and feeding to the simulation
A fourth rule, obstacle avoidance, steers each simulated fish away from tank walls, rocks or other boundaries — usually implemented by casting a short ray or checking a nearby zone ahead of the fish and adding a repulsive steering force if an obstacle is detected, on top of the three flocking forces. Feeding behaviour adds a target-seeking force toward food particles that temporarily overrides or blends with cohesion, which is why a scattering school will break formation and converge on food, then reassemble into schooling behaviour once it is consumed — the same weighted-force blending trick, just with a new attractor added to the mix.
Frequently asked questions
Do the fish in a Boids simulation actually see the whole school?
No — each simulated fish only reacts to neighbours within a limited radius around it, mimicking a real fish's limited field of view and lateral-line sensing range. The coordinated, whole-school motion is an emergent side effect of many fish following the same local rules, not the result of any fish having global awareness.
Why do real fish school in the first place?
Schooling reduces an individual's chance of being caught by a predator through the confusion effect, allows rapid group-wide response to threats, and can improve foraging efficiency and swimming energetics. It is a case of a simple, individually low-cost behaviour producing a large collective survival benefit.
What are the three core rules of the Boids model?
Separation (steer away from crowding neighbours), alignment (match the average heading of nearby neighbours) and cohesion (steer toward the average position of nearby neighbours). Craig Reynolds introduced these three rules in 1986, and variants of them still power most flocking and schooling animations today.
Try it live
Everything above runs in your browser — open Aquarium and change the parameters while it is running. Nothing is installed, nothing is uploaded, the whole model lives in one tab.
▶ Open Aquarium simulation