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

Commit dd0a350

Browse files
committed
Enhance calculate_aabox function
1 parent fe18210 commit dd0a350

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

rask-engine/src/collide.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,23 @@ impl Collide for Collidable {
3838
}
3939
}
4040

41-
/// calculate the axis aligned bounding box for a set of collidables
41+
/// calculate the minimal axis aligned bounding box that contains all `Collidable`s of a given set
4242
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-
}
43+
let [(mut minx, mut maxx), (mut miny, mut maxy)] = [(f32::INFINITY, f32::NEG_INFINITY); 2];
44+
let mut minmax = |v1: &Vec2, v2: &Vec2| {
45+
minx = f32::min(minx, v1.x());
46+
maxx = f32::max(maxx, v2.x());
47+
miny = f32::min(miny, v1.y());
48+
maxy = f32::max(maxy, v2.y());
6049
};
6150
for val in vals {
6251
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])
52+
Collidable::Point(v) => minmax(v, v),
53+
Collidable::AABox(AABox { pos, size }) => minmax(pos, &(*pos + *size)),
54+
&Collidable::RBox(RBox { pos, v1, v2 }) => {
55+
for point in &[pos, pos + v1, pos + v2, pos + v1 + v2] {
56+
minmax(point, point)
57+
}
6758
}
6859
};
6960
}

rask-engine/src/engine/systems.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<'a> System<'a> for VelocitySystem {
6363
let mut percent = -1.0;
6464
let mut ids = (entity1.id(), 0);
6565
let v = vel.0 * dt.0.as_secs_f32();
66-
'b: for (_, _, _pos2, entity2) in (&collider, &terrain, &pos, &entities).join() {
66+
for (_, _, _pos2, entity2) in (&collider, &terrain, &pos, &entities).join() {
6767
for e1 in hierarchy.children(entity1) {
6868
for e2 in hierarchy.children(entity2) {
6969
if let (

0 commit comments

Comments
 (0)