-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathboid.rs
More file actions
180 lines (156 loc) · 5.2 KB
/
boid.rs
File metadata and controls
180 lines (156 loc) · 5.2 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use std::iter;
use rand::RngExt;
use crate::math::{self, Mean, Vector2D, WeightedMean};
use crate::settings::Settings;
use crate::simulation::SIZE;
#[derive(Clone, Debug, PartialEq)]
pub struct Boid {
pub position: Vector2D,
pub velocity: Vector2D,
pub radius: f64,
pub hue: f64,
}
impl Boid {
pub fn new_random(settings: &Settings) -> Self {
let mut rng = rand::rng();
let max_radius = settings.min_distance / 2.0;
let min_radius = max_radius / 6.0;
// by using the third power large boids become rarer
let radius = min_radius + rng.random::<f64>().powi(3) * (max_radius - min_radius);
Self {
position: Vector2D::new(rng.random::<f64>() * SIZE.x, rng.random::<f64>() * SIZE.y),
velocity: Vector2D::from_polar(rng.random::<f64>() * math::TAU, settings.max_speed),
radius,
hue: rng.random::<f64>() * math::TAU,
}
}
fn coherence(&self, boids: VisibleBoidIter, factor: f64) -> Vector2D {
Vector2D::weighted_mean(
boids.map(|other| (other.boid.position, other.boid.radius * other.boid.radius)),
)
.map(|mean| (mean - self.position) * factor)
.unwrap_or_default()
}
fn separation(&self, boids: VisibleBoidIter, settings: &Settings) -> Vector2D {
let accel = boids
.filter_map(|other| {
if other.distance > settings.min_distance {
None
} else {
Some(-other.offset)
}
})
.sum::<Vector2D>();
accel * settings.separation_factor
}
fn alignment(&self, boids: VisibleBoidIter, factor: f64) -> Vector2D {
Vector2D::mean(boids.map(|other| other.boid.velocity))
.map(|mean| (mean - self.velocity) * factor)
.unwrap_or_default()
}
fn adapt_color(&mut self, boids: VisibleBoidIter, factor: f64) {
let mean = f64::mean(boids.filter_map(|other| {
if other.boid.radius > self.radius {
Some(math::smallest_angle_between(self.hue, other.boid.hue))
} else {
None
}
}));
if let Some(avg_hue_offset) = mean {
self.hue += avg_hue_offset * factor;
}
}
fn keep_in_bounds(&mut self, settings: &Settings) {
let min = SIZE * settings.border_margin;
let max = SIZE - min;
let mut v = Vector2D::default();
let turn_speed = self.velocity.magnitude() * settings.turn_speed_ratio;
let pos = self.position;
if pos.x < min.x {
v.x += turn_speed;
}
if pos.x > max.x {
v.x -= turn_speed
}
if pos.y < min.y {
v.y += turn_speed;
}
if pos.y > max.y {
v.y -= turn_speed;
}
self.velocity += v;
}
fn update_velocity(&mut self, settings: &Settings, boids: VisibleBoidIter) {
let v = self.velocity
+ self.coherence(boids.clone(), settings.cohesion_factor)
+ self.separation(boids.clone(), settings)
+ self.alignment(boids, settings.alignment_factor);
self.velocity = v.clamp_magnitude(settings.max_speed);
}
fn update(&mut self, settings: &Settings, boids: VisibleBoidIter) {
self.adapt_color(boids.clone(), settings.color_adapt_factor);
self.update_velocity(settings, boids);
self.keep_in_bounds(settings);
self.position += self.velocity;
}
pub fn update_all(settings: &Settings, boids: &mut [Self]) {
for i in 0..boids.len() {
let (before, after) = boids.split_at_mut(i);
let (boid, after) = after.split_first_mut().unwrap();
let visible_boids =
VisibleBoidIter::new(before, after, boid.position, settings.visible_range);
boid.update(settings, visible_boids);
}
}
}
#[derive(Debug)]
struct VisibleBoid<'a> {
boid: &'a Boid,
offset: Vector2D,
distance: f64,
}
#[derive(Clone, Debug)]
struct VisibleBoidIter<'boid> {
// Pay no mind to this mess of a type.
// It's just `before` and `after` joined together.
it: iter::Chain<std::slice::Iter<'boid, Boid>, std::slice::Iter<'boid, Boid>>,
position: Vector2D,
visible_range: f64,
}
impl<'boid> VisibleBoidIter<'boid> {
fn new(
before: &'boid [Boid],
after: &'boid [Boid],
position: Vector2D,
visible_range: f64,
) -> Self {
Self {
it: before.iter().chain(after),
position,
visible_range,
}
}
}
impl<'boid> Iterator for VisibleBoidIter<'boid> {
type Item = VisibleBoid<'boid>;
fn next(&mut self) -> Option<Self::Item> {
let Self {
ref mut it,
position,
visible_range,
} = *self;
it.find_map(move |other| {
let offset = other.position - position;
let distance = offset.magnitude();
if distance > visible_range {
None
} else {
Some(VisibleBoid {
boid: other,
offset,
distance,
})
}
})
}
}