Agent-Based Systems
Boids, Flow-field following, and predator/prey simulations
System Overview
Boids simulate flocking behavior through three simple rules: alignment (steer toward average heading), cohesion (steer toward average position), and separation (avoid crowding). These local rules create complex, emergent group behaviors like flocks, schools, and herds.
Each boid only perceives nearby neighbors, making the system scalable. The balance between forces determines behavior: too much cohesion creates tight clusters, too much separation causes scattering.
Why Games Use This
- Crowd Simulation: Realistic group movement without scripting
- AI Behavior: Emergent tactics from simple rules
- Performance: O(n²) but can be optimized with spatial partitioning
- Variety: Different force weights create different behaviors
- Natural Appearance: Organic, believable movement
Key Parameters
- Alignment: How strongly boids match neighbor velocities
- Cohesion: How strongly boids move toward group center
- Separation: How strongly boids avoid crowding
- Perception Radius: How far boids can "see" neighbors
- Max Speed/Force: Limits on movement and acceleration
Failure Modes
- Too many boids: O(n²) neighbor checking becomes expensive
- Extreme forces: Unbalanced weights create chaotic or frozen behavior
- Small perception: Boids can't form groups
- Large perception: All boids influence each other, losing local behavior
- No spatial optimization: Checking all pairs is slow for large groups
Scaling Behavior
Naive implementation is O(n²) per frame. Use spatial hashing or quadtrees to reduce to O(n) average case. For 1000+ boids, spatial partitioning is essential. GPU implementations can handle 10,000+ boids.
Memory is O(n) for boid storage. Each boid needs position, velocity, and optional state.
Related Algorithms
- Flow Fields: Boids follow vector fields instead of neighbors
- Steering Behaviors: Seek, flee, pursue, evade
- Reynolds Behaviors: Extended boid rules (wander, obstacle avoidance)
- Particle Systems: Similar but without social forces
- Pathfinding: Combine with A* for goal-directed movement
Free Tools & Libraries
- p5.js: Built-in examples and tutorials
- Unity DOTS: High-performance boids with ECS
System-Thinking Prompts
- What happens with 1000 boids? How does performance degrade?
- Where do boids break? Edge cases, extreme parameters?
- How could players exploit predictable flocking? Predictable paths?
- Which force dominates? Alignment, cohesion, or separation?
- What's the optimal perception radius? Too small vs too large?
- How do force weights affect behavior? What ratios create interesting patterns?
- Can we guarantee interesting behavior? Or is it parameter tuning?