A colourful 3D aquarium where fish school together using Boids-like flocking rules. Each fish follows three simple rules โ separation, alignment and cohesion โ yet together they form beautiful, lifelike schools.
Emergent flocking behaviour from local rules. Fish avoid obstacles, seek food and form coordinated groups without any central leader.
Adjust fish count and speed. Click to drop food and watch the school react. Try different preset environments.
Fish schooling evolved as anti-predator defence. A school confuses predators through the "confusion effect" โ many similar targets make it harder to single one out.
This aquarium animates a school of fish using a classic Boids flocking model, where every fish steers itself from three local rules โ separation, alignment and cohesion โ with no central leader. Each fish only looks at neighbours within a short radius, yet coordinated, lifelike schools emerge from the combined behaviour. Tossed food adds a simple attraction force, so the shoal reorganises itself around the nearest crumb in real time.
Emergent flocking from Craig Reynolds' Boids algorithm. Within a 160-pixel radius each fish steers toward the average position (cohesion) and matches the average velocity (alignment) of its neighbours, while pushing away from any fish closer than 80 pixels (separation). Food within 250 pixels adds an attraction force, and speed is capped each frame so movement stays smooth.
Use the sliders to set the number of fish (5โ60), their maximum speed (0.5โ4) and the cohesion strength (0โ0.05) that decides how tightly they cluster. Press "Throw food" or click anywhere on the water to drop crumbs that the shoal swims to. Switch between the Tropical, Arctic and Deep presets to change the palette and lighting.
Schooling is largely an anti-predator strategy. A dense, similar-looking shoal triggers the "confusion effect", making it harder for a predator to lock onto and track any single target among the many.
It models a fish school as a Boids system, a flocking algorithm devised by Craig Reynolds in 1986. Instead of scripting the school as a whole, each fish is an independent agent that reacts only to nearby neighbours, so the recognisable schooling shapes are an emergent result rather than something programmed directly.
Every frame each fish surveys its neighbours and combines three steering forces: separation pushes it away from fish within 80 pixels, alignment nudges it to match the average heading of fish within 160 pixels, and cohesion pulls it toward their average position. These forces are summed, the resulting speed is clamped to the maximum you set, and the fish moves one step.
The Fish slider sets how many agents are in the tank (5 to 60) and rebuilds the school. Speed sets the maximum velocity each fish may reach. Cohesion (0 to 0.05) scales the pull toward the group's centre โ low values give loose, scattered fish, while higher values produce tight, ball-like shoals.
When food exists, each fish finds the nearest crumb and, if it is within 250 pixels, adds a steering force of roughly 0.15 toward it. A crumb is eaten once a fish comes within 10 pixels, which increments the "Fed" counter. The food therefore acts as a temporary attractor layered on top of the normal flocking rules.
It is a faithful demonstration of the principle that complex group behaviour can arise from simple local rules, which biologists do observe in real shoals. It is not a hydrodynamic or biological simulation, though: there is no water resistance, vision cones, energy or species behaviour, and the motion runs in two dimensions despite the underwater scene.
This simulation models a school of fish swimming in a virtual aquarium using the Boids algorithm, a computational model of emergent flocking behaviour invented by Craig Reynolds in 1986. Each fish follows three simple local rules โ separation (avoid crowding neighbours), alignment (steer toward the average heading of nearby fish), and cohesion (move toward the average position of nearby fish) โ and from these rules alone, realistic schooling patterns emerge without any central coordination.
Fish schooling is found across thousands of species and is studied in fields from marine biology to robotics. The same three-rule principle underlies swarm algorithms used in drone coordination, traffic modelling, and crowd simulation.
Boids is a computer model created by Craig Reynolds in 1986 to simulate the flocking behaviour of birds (the name comes from "bird-oid objects"). Each agent, or "boid", applies three steering rules every frame: it moves away from neighbours that are too close (separation), it steers to match the average velocity of nearby neighbours (alignment), and it steers toward the average position of nearby neighbours (cohesion). No agent has a global view of the group โ the coordinated flock emerges purely from these local interactions.
Use the Fish slider to set the number of fish in the tank (5 to 60), the Speed slider to control how fast they swim, and the Cohesion slider to decide how tightly the school clusters together. Click the "Throw food" button or click anywhere on the water to drop food crumbs โ fish within 250 pixels will steer toward them and eat them when they get within 10 pixels. Use the Tropical, Arctic, and Deep preset buttons to change the colour palette and environment theme.
The Cohesion slider (range 0 to 0.05) scales the strength of the force that pulls each fish toward the average position of its neighbours within a 160-pixel radius. At very low values fish drift apart and school loosely. At higher values the group compresses into a tight, ball-like shoal that moves as one unit. Setting cohesion to zero removes this force entirely, leaving only separation and alignment, which produces scattered, wandering fish rather than a coherent school.
Each frame, a fish computes three force vectors. Separation: for every neighbour closer than 80 pixels, add a unit vector pointing away, scaled by 0.04. Alignment: average the velocity vectors of all neighbours within 160 pixels, scale by 0.02, and add this to the fish's velocity. Cohesion: compute the vector from the fish to the average position of neighbours within 160 pixels, normalise it, and scale by the cohesion parameter. The three forces are summed and applied. Speed is then clamped to the maximum set by the Speed slider. These are Euler-integration steering forces rather than Newtonian physics, meaning acceleration and drag are implicit in the clamping.
Schooling in real fish has evolved primarily as an anti-predator defence. A dense shoal of hundreds or thousands of identical-looking fish triggers what biologists call the "confusion effect" โ a predator finds it nearly impossible to track and isolate a single target among the rapid, coordinated movement of many similar fish. Schools also benefit from the "many eyes" effect: with more individuals watching for danger, the group detects threats sooner. Some schooling species additionally gain hydrodynamic advantages, as fish can draft in the pressure wake of neighbours and reduce their energy expenditure.
A common misconception is that a school is led by a "leader fish" at the front that the others follow. In reality, schooling fish have no leader and no hierarchy. The fish that appears to be at the front simply happens to be there because of the combined steering of all individuals. If that fish turns, the whole group reorganises, and a different fish may end up at the front. Every individual makes its own decisions based only on the fish it can immediately perceive, and the orderly group movement is a self-organised emergent property.
Craig Reynolds, then a computer graphics researcher, published the Boids algorithm in 1987 in the SIGGRAPH proceedings paper "Flocks, Herds, and Schools: A Distributed Behavioral Model". The model was immediately influential in film and animation โ it was first used in a major production in the 1992 Tim Burton film Batman Returns, where thousands of bats and penguins were animated using Boids. Reynolds received an Academy Award for Technical Achievement in 1998 for his work on motion control and behavioural animation.
The aquarium is closely related to the 3D Boids simulation on this site, which shows flocking in three-dimensional space without the aquarium theme. The Fish School 3D simulation also uses flocking rules but renders fish as 3D meshes. More broadly, the same emergent-behaviour principles appear in the Game of Life (cellular automaton emergence), the SIR Epidemic Model (agent spread through a population), and L-Systems (growth rules producing complex plant structures). All of these show how simple local rules can generate complex global patterns.
Boids-derived algorithms are used extensively in swarm robotics, where groups of simple autonomous drones or ground robots must coordinate without a central controller. The same rules enable decentralised search-and-rescue operations, agricultural drone swarms for crop monitoring, and military multi-UAV coordination. In traffic engineering, flocking models inform how autonomous vehicles can form platoons to reduce aerodynamic drag. In network engineering, ant-colony and swarm optimisation algorithms โ mathematically related to Boids โ solve routing and load-balancing problems across large distributed systems.
Researchers are currently investigating how real fish schools make collective decisions, such as choosing which direction to turn when a predator approaches, without any individual having full information. Studies using high-speed multi-camera tracking have revealed that information propagates through a school as a wave moving at roughly 20 fish-lengths per second โ far faster than any individual reaction time. Open questions include how schools maintain cohesion in three dimensions in turbulent water, how mixed-species shoals form and dissolve, and how the brain of each fish encodes and rapidly processes social cues from dozens of neighbours simultaneously.