Craig Reynolds published Boids in 1987. The model uses three steering rules to produce realistic flocking behavior.
The Three Rules
**1. Separation** — steer away from neighbors that are too close.
**2. Alignment** — steer toward the average heading of nearby neighbors.
**3. Cohesion** — steer toward the average position of nearby neighbors.
That's it. Three local rules → emergent flocking.
Pseudocode
for each boid:
neighbors = find_neighbors(boid, radius=50)
sep = separation(boid, neighbors) // push away
ali = alignment(boid, neighbors) // match velocity
coh = cohesion(boid, neighbors) // pull toward center
boid.velocity += sep * w_sep + ali * w_ali + coh * w_coh
boid.velocity = clamp(boid.velocity, max_speed)
boid.position += boid.velocity * dt
Key Implementation Notes
›Use a **spatial hash grid** for neighbor queries. Brute force is O(n²) — fine for 100 boids, terrible for 10,000.
›Clamp velocity at max_speed to prevent exponential runaway.
›Tune weights: separation too high → fragmented clusters; cohesion too high → one dense blob.
Next: Full Python Implementation
In lesson 04 we implement this in Python with Pygame, spatial hashing, and configurable weights.