Skip to content

Commit 29a4073

Browse files
committed
dla: tune movement
1 parent 9c79764 commit 29a4073

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/lib.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rand::Rng;
2+
use rand::seq::SliceRandom;
23

34
pub mod geo;
45
pub use geo::Vec3;
@@ -11,6 +12,7 @@ use crate::octree::Octree;
1112
pub struct Dla {
1213
spawn_radius: i64,
1314
attraction_radius: i64,
15+
attraction_radius2: i64,
1416

1517
cells: Octree,
1618
bbox: Bbox,
@@ -41,6 +43,7 @@ impl Dla {
4143
bbox,
4244
spawn_radius: i64::from(spawn_radius),
4345
attraction_radius: i64::from(attraction_radius),
46+
attraction_radius2: i64::from(attraction_radius).pow(2),
4447
})
4548
}
4649

@@ -88,15 +91,14 @@ impl Dla {
8891
break;
8992
}
9093
None => {
91-
let d = match rng.gen_range(0, 6) {
92-
0 => Vec3::new(-1, 0, 0),
93-
1 => Vec3::new(1, 0, 0),
94-
2 => Vec3::new(0, -1, 0),
95-
3 => Vec3::new(0, 1, 0),
96-
4 => Vec3::new(0, 0, -1),
97-
5 => Vec3::new(0, 0, 1),
98-
_ => unreachable!(),
99-
};
94+
let d = Vec3::new(
95+
rng.gen_range(1, self.attraction_radius / 2)
96+
* *[-1, 1].choose(rng).unwrap(),
97+
rng.gen_range(1, self.attraction_radius / 2)
98+
* *[-1, 1].choose(rng).unwrap(),
99+
rng.gen_range(1, self.attraction_radius / 2)
100+
* *[-1, 1].choose(rng).unwrap(),
101+
);
100102
cell = cell + d * self.attraction_radius;
101103

102104
if !spawn_bbox.contains(cell) {
@@ -110,10 +112,9 @@ impl Dla {
110112
}
111113

112114
pub fn stuck(&self, p: Vec3) -> Option<Vec3> {
113-
let (n, _d2) = self.cells.nearest(p)?;
115+
let (n, d2) = self.cells.nearest(p)?;
114116

115-
let d = n - p;
116-
if d.x.abs() + d.y.abs() + d.z.abs() <= self.attraction_radius {
117+
if d2 <= self.attraction_radius2 {
117118
Some(n)
118119
} else {
119120
None

0 commit comments

Comments
 (0)