2021-05-14 18:45:31 +00:00
|
|
|
//! Utilities for detecting if and on which side two axis-aligned bounding boxes (AABB) collide.
|
|
|
|
|
2020-07-16 23:51:45 +00:00
|
|
|
use bevy_math::{Vec2, Vec3};
|
2020-06-27 04:40:09 +00:00
|
|
|
|
2023-03-18 04:55:31 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
2020-06-27 04:40:09 +00:00
|
|
|
pub enum Collision {
|
|
|
|
Left,
|
|
|
|
Right,
|
|
|
|
Top,
|
|
|
|
Bottom,
|
2022-02-01 22:40:25 +00:00
|
|
|
Inside,
|
2020-06-27 04:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: ideally we can remove this once bevy gets a physics system
|
|
|
|
/// Axis-aligned bounding box collision with "side" detection
|
2021-12-29 18:49:43 +00:00
|
|
|
/// * `a_pos` and `b_pos` are the center positions of the rectangles, typically obtained by
|
2023-04-23 17:28:36 +00:00
|
|
|
/// extracting the `translation` field from a [`Transform`](bevy_transform::components::Transform) component
|
2021-12-29 18:49:43 +00:00
|
|
|
/// * `a_size` and `b_size` are the dimensions (width and height) of the rectangles.
|
2022-09-12 01:25:34 +00:00
|
|
|
///
|
2023-04-23 17:28:36 +00:00
|
|
|
/// The return value is the side of `B` that `A` has collided with. [`Collision::Left`] means that
|
|
|
|
/// `A` collided with `B`'s left side. [`Collision::Top`] means that `A` collided with `B`'s top side.
|
2022-09-12 01:25:34 +00:00
|
|
|
/// If the collision occurs on multiple sides, the side with the deepest penetration is returned.
|
2023-04-23 17:28:36 +00:00
|
|
|
/// If all sides are involved, [`Collision::Inside`] is returned.
|
2020-06-27 04:40:09 +00:00
|
|
|
pub fn collide(a_pos: Vec3, a_size: Vec2, b_pos: Vec3, b_size: Vec2) -> Option<Collision> {
|
|
|
|
let a_min = a_pos.truncate() - a_size / 2.0;
|
|
|
|
let a_max = a_pos.truncate() + a_size / 2.0;
|
|
|
|
|
|
|
|
let b_min = b_pos.truncate() - b_size / 2.0;
|
|
|
|
let b_max = b_pos.truncate() + b_size / 2.0;
|
|
|
|
|
|
|
|
// check to see if the two rectangles are intersecting
|
2020-11-17 21:40:18 +00:00
|
|
|
if a_min.x < b_max.x && a_max.x > b_min.x && a_min.y < b_max.y && a_max.y > b_min.y {
|
2020-06-27 04:40:09 +00:00
|
|
|
// check to see if we hit on the left or right side
|
2020-11-17 21:40:18 +00:00
|
|
|
let (x_collision, x_depth) = if a_min.x < b_min.x && a_max.x > b_min.x && a_max.x < b_max.x
|
|
|
|
{
|
2022-02-01 22:40:25 +00:00
|
|
|
(Collision::Left, b_min.x - a_max.x)
|
2020-11-17 21:40:18 +00:00
|
|
|
} else if a_min.x > b_min.x && a_min.x < b_max.x && a_max.x > b_max.x {
|
2022-02-01 22:40:25 +00:00
|
|
|
(Collision::Right, a_min.x - b_max.x)
|
2020-11-17 21:40:18 +00:00
|
|
|
} else {
|
2022-02-01 22:40:25 +00:00
|
|
|
(Collision::Inside, -f32::INFINITY)
|
2020-11-17 21:40:18 +00:00
|
|
|
};
|
2020-06-27 04:40:09 +00:00
|
|
|
|
|
|
|
// check to see if we hit on the top or bottom side
|
2020-11-17 21:40:18 +00:00
|
|
|
let (y_collision, y_depth) = if a_min.y < b_min.y && a_max.y > b_min.y && a_max.y < b_max.y
|
|
|
|
{
|
2022-02-01 22:40:25 +00:00
|
|
|
(Collision::Bottom, b_min.y - a_max.y)
|
2020-11-17 21:40:18 +00:00
|
|
|
} else if a_min.y > b_min.y && a_min.y < b_max.y && a_max.y > b_max.y {
|
2022-02-01 22:40:25 +00:00
|
|
|
(Collision::Top, a_min.y - b_max.y)
|
2020-11-17 21:40:18 +00:00
|
|
|
} else {
|
2022-02-01 22:40:25 +00:00
|
|
|
(Collision::Inside, -f32::INFINITY)
|
2020-11-17 21:40:18 +00:00
|
|
|
};
|
2020-06-27 04:40:09 +00:00
|
|
|
|
|
|
|
// if we had an "x" and a "y" collision, pick the "primary" side using penetration depth
|
2022-02-01 22:40:25 +00:00
|
|
|
if y_depth.abs() < x_depth.abs() {
|
|
|
|
Some(y_collision)
|
|
|
|
} else {
|
|
|
|
Some(x_collision)
|
2020-06-27 04:40:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2022-09-12 01:25:34 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
fn collide_two_rectangles(
|
|
|
|
// (x, y, size x, size y)
|
|
|
|
a: (f32, f32, f32, f32),
|
|
|
|
b: (f32, f32, f32, f32),
|
|
|
|
) -> Option<Collision> {
|
|
|
|
collide(
|
|
|
|
Vec3::new(a.0, a.1, 0.),
|
|
|
|
Vec2::new(a.2, a.3),
|
|
|
|
Vec3::new(b.0, b.1, 0.),
|
|
|
|
Vec2::new(b.2, b.3),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inside_collision() {
|
|
|
|
// Identical
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let res = collide_two_rectangles(
|
|
|
|
(1., 1., 1., 1.),
|
|
|
|
(1., 1., 1., 1.),
|
|
|
|
);
|
|
|
|
assert_eq!(res, Some(Collision::Inside));
|
|
|
|
// B inside A
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let res = collide_two_rectangles(
|
|
|
|
(2., 2., 2., 2.),
|
|
|
|
(2., 2., 1., 1.),
|
|
|
|
);
|
|
|
|
assert_eq!(res, Some(Collision::Inside));
|
|
|
|
// A inside B
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let res = collide_two_rectangles(
|
|
|
|
(2., 2., 1., 1.),
|
|
|
|
(2., 2., 2., 2.),
|
|
|
|
);
|
|
|
|
assert_eq!(res, Some(Collision::Inside));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn collision_based_on_b() {
|
|
|
|
// Right of B
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let res = collide_two_rectangles(
|
|
|
|
(3., 2., 2., 2.),
|
|
|
|
(2., 2., 2., 2.),
|
|
|
|
);
|
|
|
|
assert_eq!(res, Some(Collision::Right));
|
|
|
|
// Left of B
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let res = collide_two_rectangles(
|
|
|
|
(1., 2., 2., 2.),
|
|
|
|
(2., 2., 2., 2.),
|
|
|
|
);
|
|
|
|
assert_eq!(res, Some(Collision::Left));
|
|
|
|
// Top of B
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let res = collide_two_rectangles(
|
|
|
|
(2., 3., 2., 2.),
|
|
|
|
(2., 2., 2., 2.),
|
|
|
|
);
|
|
|
|
assert_eq!(res, Some(Collision::Top));
|
|
|
|
// Bottom of B
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let res = collide_two_rectangles(
|
|
|
|
(2., 1., 2., 2.),
|
|
|
|
(2., 2., 2., 2.),
|
|
|
|
);
|
|
|
|
assert_eq!(res, Some(Collision::Bottom));
|
|
|
|
}
|
|
|
|
|
|
|
|
// In case the X-collision depth is equal to the Y-collision depth, always
|
|
|
|
// prefer X-collision, meaning, `Left` or `Right` over `Top` and `Bottom`.
|
|
|
|
#[test]
|
|
|
|
fn prefer_x_collision() {
|
|
|
|
// Bottom-left collision
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let res = collide_two_rectangles(
|
|
|
|
(1., 1., 2., 2.),
|
|
|
|
(2., 2., 2., 2.),
|
|
|
|
);
|
|
|
|
assert_eq!(res, Some(Collision::Left));
|
|
|
|
// Top-left collision
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let res = collide_two_rectangles(
|
|
|
|
(1., 3., 2., 2.),
|
|
|
|
(2., 2., 2., 2.),
|
|
|
|
);
|
|
|
|
assert_eq!(res, Some(Collision::Left));
|
|
|
|
// Bottom-right collision
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let res = collide_two_rectangles(
|
|
|
|
(3., 1., 2., 2.),
|
|
|
|
(2., 2., 2., 2.),
|
|
|
|
);
|
|
|
|
assert_eq!(res, Some(Collision::Right));
|
|
|
|
// Top-right collision
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let res = collide_two_rectangles(
|
|
|
|
(3., 3., 2., 2.),
|
|
|
|
(2., 2., 2., 2.),
|
|
|
|
);
|
|
|
|
assert_eq!(res, Some(Collision::Right));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the collision intersection area stretches more along the Y-axis then
|
|
|
|
// return `Top` or `Bottom`. Otherwise, `Left` or `Right`.
|
|
|
|
#[test]
|
|
|
|
fn collision_depth_wins() {
|
|
|
|
// Top-right collision
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let res = collide_two_rectangles(
|
|
|
|
(3., 3., 2., 2.),
|
|
|
|
(2.5, 2.,2., 2.),
|
|
|
|
);
|
|
|
|
assert_eq!(res, Some(Collision::Top));
|
|
|
|
// Top-right collision
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let res = collide_two_rectangles(
|
|
|
|
(3., 3., 2., 2.),
|
|
|
|
(2., 2.5, 2., 2.),
|
|
|
|
);
|
|
|
|
assert_eq!(res, Some(Collision::Right));
|
|
|
|
}
|
|
|
|
}
|