use bevy::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Debug, Default, Clone, Copy, Reflect, Serialize, Deserialize)]
pub struct Aabb {
pub extents: Vec2,
pub position: Vec2,
}
impl Aabb {
pub fn new(extents: Vec2, position: Vec2) -> Aabb {
Aabb {
extents: extents.abs(),
position
}
}
pub fn size(size: Vec2, position: Vec2) -> Aabb {
Aabb {
extents: size.abs() * 0.5,
position,
}
}
pub fn from_min_max(min: Vec2, max: Vec2) -> Aabb {
Aabb::size(max - min, (min + max) * 0.5)
}
pub fn min_max(&self) -> (Vec2,Vec2) {
(self.position - self.extents, self.position + self.extents)
}
pub fn collides(&self, other: &Aabb) -> bool {
let (min1, max1) = self.min_max();
let (min2, max2) = other.min_max();
min1.x < max2.x && min1.y < max2.y &&
min2.x < max1.x && min2.y < max1.y
}
}