1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! # Boids Flocking
//!
//! Classic boids algorithm: separation, cohesion, and alignment create
//! emergent flocking behavior from simple local rules.
//!
//! ## What This Demonstrates
//!
//! - `with_spatial_config()` - enables efficient neighbor queries
//! - `Rule::Separate` - avoid crowding nearby boids
//! - `Rule::Cohere` - steer toward average position of neighbors
//! - `Rule::Align` - match velocity with nearby boids
//!
//! ## The Algorithm
//!
//! Each boid looks at neighbors within a radius and:
//! 1. **Separate**: Steer away from very close neighbors (avoid collision)
//! 2. **Cohere**: Steer toward the center of nearby neighbors (stay together)
//! 3. **Align**: Match the average heading of neighbors (move as a group)
//!
//! ## Spatial Hashing
//!
//! `with_spatial_config(cell_size, resolution)` divides space into a grid
//! for O(1) neighbor lookups instead of O(n²) brute force.
//!
//! - `cell_size` should be >= your largest interaction radius
//! - `resolution` must be power of 2 (8, 16, 32, 64)
//!
//! ## Try This
//!
//! - Adjust strengths to change flocking tightness
//! - Add `Rule::SpeedLimit { min: 0.1, max: 1.0 }` for more natural motion
//! - Try `Rule::WrapWalls` instead of `BounceWalls` for toroidal space
//!
//! Run with: `cargo run --example boids`
use *;