Skip to content
This repository was archived by the owner on Mar 26, 2024. It is now read-only.

Commit fe18210

Browse files
committed
Add function to calculate bounding box of collidables
1 parent 2e3035d commit fe18210

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

rask-engine/src/collide.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,41 @@ impl Collide for Collidable {
3838
}
3939
}
4040

41+
/// calculate the axis aligned bounding box for a set of collidables
42+
pub fn calculate_aabb<'a, I: Iterator<Item = &'a Collidable>>(vals: I) -> AABox {
43+
let (mut minx, mut maxx, mut miny, mut maxy) = (0.0, 0.0, 0.0, 0.0);
44+
let mut first_iter = true;
45+
let mut f = |points: &[Vec2]| {
46+
for point in points {
47+
if first_iter {
48+
minx = point.x();
49+
maxx = point.x();
50+
miny = point.y();
51+
maxy = point.y();
52+
first_iter = false;
53+
} else {
54+
minx = f32::min(minx, point.x());
55+
maxx = f32::max(maxx, point.x());
56+
miny = f32::min(miny, point.y());
57+
maxy = f32::max(maxy, point.y());
58+
}
59+
}
60+
};
61+
for val in vals {
62+
match val {
63+
Collidable::Point(v) => f(&[*v]),
64+
Collidable::AABox(AABox { pos, size }) => f(&[*pos, *pos + *size]),
65+
Collidable::RBox(RBox { pos, v1, v2 }) => {
66+
f(&[*pos, *pos + *v1, *pos + *v2, *pos + *v1 + *v2])
67+
}
68+
};
69+
}
70+
AABox {
71+
pos: Vec2::new(minx, miny),
72+
size: Vec2::new(maxx - minx, maxy - miny),
73+
}
74+
}
75+
4176
// Given a fraction of the dv vectors length,
4277
// return the amount to set the objects back
4378
// in case of a collision

rask-engine/src/engine/systems.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ impl<'a> System<'a> for UpdateAnimationSystem {
199199
let ci = hierarchy.children(entity);
200200
let mut ci: Vec<_> = ci.to_vec();
201201
ci.sort_unstable_by_key(|x| x.id()); //TODO we might be able to remove this
202+
let aabb = crate::collide::calculate_aabb(
203+
ci.iter()
204+
.map(|e| sub.get(*e).map(|s| &s.collider))
205+
.flatten(),
206+
);
202207
let mut ci = ci.iter();
203208
for (i, s) in sprites.enumerate() {
204209
let s = s.unwrap();
@@ -244,6 +249,15 @@ impl<'a> System<'a> for UpdateAnimationSystem {
244249
}
245250
}
246251
}
252+
let new_aabb = crate::collide::calculate_aabb(
253+
hierarchy
254+
.children(entity)
255+
.iter()
256+
.map(|e| sub.get(*e).map(|s| &s.collider))
257+
.flatten(),
258+
);
259+
260+
// modify position to avoid collisions
247261
}
248262
}
249263
}

0 commit comments

Comments
 (0)