What It Demonstrates

This simulation models collective animal behaviour using Craig Reynolds' Boids algorithm (1986). Each fish follows three simple local rules — separation, alignment, and cohesion — yet the global result is realistic, fluid schooling. When the predator shark approaches, fish switch to a flee response, generating spectacular split-and-regroup dynamics.

How to Use

Adjust Fish Count, Speed, and the three flocking-force radii. Toggle the predator on/off to compare schooling with and without threat. Try large cohesion radius for tight schools, or large alignment radius for highly synchronised groups.

Did You Know?

Fish schools confuse predators via the confusion effect — a predator targeting one fish loses it in the crowd. Boids-like models are also used in animation (e.g. the stampede in The Lion King) and autonomous drone swarm coordination.

About this simulation

This simulation renders a Boids flocking model (Craig Reynolds, 1986) as a top-down pseudo-3D fish school. Every fish scans nearby neighbours within three independent radii — separation, alignment and cohesion — and combines the resulting steering vectors into its own heading each frame. A hunting shark adds a fourth force, flee, that overrides flocking when a fish enters the panic radius, producing the split-and-regroup behaviour real schools use to survive predation.

🔬 What it shows

200 fish (adjustable 50–400) are assigned to one of three depth layers, each with its own palette and size, so overlapping schools read as a 3D ocean on a 2D canvas. Each fish only reacts to neighbours in adjacent layers, checking squared distance against separation (default 28px), alignment (55px) and cohesion (80px) radii to compute a steering force every frame, alongside a shark that steers toward the fish centroid and fish that flee once inside the 110px panic radius.

🎮 How to use

Drag Fish Count and Speed to change scale and tempo, then widen or narrow the three flocking radii to see tighter or looser schools, more or less synchronised turning. Toggle Predator ON/OFF to compare calm schooling against panic dynamics, and adjust Shark Speed and Panic Radius to make the hunt easier or harder to escape. The live stats panel reports fish count, cluster count (a flood-fill over the cohesion radius), panicking fish and FPS.

💡 Did you know?

Reynolds' original 1987 Boids paper needed no leader and no global rulebook — schooling, flocking and herding all emerge from three purely local rules applied by every individual simultaneously. Real fish schools use the same trick against predators: a shark targeting one silhouette in a fast-moving crowd repeatedly loses its lock, known as the confusion effect, which is exactly why the fish in this simulation scatter and reform instead of just fleeing in a straight line.

Frequently asked questions

What are the three Boids rules and what do they do?

Separation steers each fish away from neighbours that are too close, preventing collisions. Alignment steers a fish to match the average heading of neighbours within its alignment radius, producing synchronised turning. Cohesion steers a fish toward the average position of neighbours within its cohesion radius, keeping the group together. All three forces are summed each frame to produce one final steering vector per fish.

Why do the fish look like they're swimming in 3D on a flat canvas?

Each fish is randomly assigned one of three depth layers with a distinct size and colour palette — larger, brighter fish for the surface layer and smaller, darker fish for the deep layer. Fish only flock with neighbours in the same or an adjacent layer, and they are drawn deepest-layer-first each frame, so the layering plus occlusion creates a convincing illusion of depth without any real 3D geometry.

How does the shark decide where to hunt?

The shark computes the centroid (average position) of every fish on screen and steers toward it with a small constant acceleration, capped at the Shark Speed setting. It does not target individual fish — it simply chases the crowd's centre of mass, which is why a school that splits into two clusters can genuinely confuse it and buy time for both halves to escape.

What triggers a fish to panic and how does that change its behaviour?

A fish enters panic mode when it falls within the Panic Radius of the shark; the closer it is, the stronger an added flee force pushes it directly away from the shark. Panicking fish are drawn in a warning yellow-orange, swim up to twice as fast as their normal top speed, and beat their tails faster, and panic decays gradually once the shark is no longer nearby.

How is the "Clusters" stat calculated?

The simulation runs a flood-fill (breadth-first search) over the fish array: starting from an unvisited fish, it repeatedly adds any other unvisited fish within the cohesion radius to the same group, then moves to the next unvisited fish and starts a new group. The total number of groups formed this way is the cluster count shown in the stats panel, updated roughly six times per second.