×
Fish Flocking Model - Complete Information
Overview
This is an implementation of Craig Reynolds' boids model (1986), which demonstrates emergent flocking
behavior from simple local rules. The model simulates agent-based collective motion similar to bird
flocks, fish schools, or insect swarms.
Simulation Algorithm
The simulation runs in discrete time steps (frames). In each frame, the following process occurs:
- Calculate Forces: For every fish, we look at its neighbors and calculate the
steering forces (Separation, Alignment, Cohesion) based on their positions and velocities.
- Apply Forces: We sum these forces (plus any fear response) to get a total
acceleration.
- Update Physics: We update the velocity (adding acceleration) and then the position
(adding velocity) for every fish.
- Repeat: This process repeats 60 times per second to create smooth motion.
Core Flocking Rules
Click on the rules below to see the mathematical details.
1. Separation (Collision Avoidance)
Prevents crowding by steering away from nearby neighbors. The force increases with proximity.
Force calculation:
F = Σ[(position_self - position_neighbor) / distance²]
The force is inversely proportional to the square of the distance, meaning it gets much stronger
very quickly as fish get closer.
2. Alignment (Velocity Matching)
Steers toward the average heading of local flockmates to create coordinated movement.
Force calculation:
v_avg = Σ(velocity_neighbor) / n
F = (v_avg · max_speed / |v_avg|) - velocity_self
3. Cohesion (Flock Centering)
Steers toward the average position of local flockmates to maintain group cohesion.
Force calculation:
center = Σ(position_neighbor) / n
desired_v = center - position_self
F = (desired_v · max_speed / |desired_v|) - velocity_self
Predator-Prey Dynamics
Predators preferentially hunt isolated fish. Fish respond with a fear behavior, fleeing from
predators when they get too close.
Target Selection:
Score = distance - (IsolationFactor * 30)
Predators target the closest fish that has the fewest neighbors.
Fear Response:
Force increases quadratically as the predator gets closer, overriding normal flocking behavior.