lgeo/shapes/
aabb.rs

1use std::fmt::{Display, Formatter};
2use lmaths::*;
3use crate::Shape;
4use crate::util::*;
5
6/// Represent a Rectangle shape
7#[allow(dead_code)]
8#[derive(Clone, Copy, Default, PartialEq, Debug)]
9pub struct AABB {
10    /// Top Left Corner
11    pub position:Vector2,
12    pub size:Vector2,
13
14    /// The four corners of the rect
15    corners: [Vector2; 4],
16}
17
18impl AABB {
19    /// Create a new rectangle at the specified position, with the specified size
20    pub fn new (position:Vector2, size:Vector2) -> Self {
21        Self {
22            position,
23            size,
24            corners: Self::generate_corners(size / 2.0)
25        }
26    }
27
28    /// Generate the position of the four corners, to cache them
29    fn generate_corners(half_dim:Vector2) -> [Vector2; 4] {
30        [
31            Vector2::new(half_dim.x, half_dim.y),
32            Vector2::new(-half_dim.x, half_dim.y),
33            Vector2::new(-half_dim.x, -half_dim.y),
34            Vector2::new(half_dim.x, -half_dim.y),
35        ]
36    }
37}
38
39#[allow(dead_code)]
40impl Shape for AABB {
41
42    fn position(&self) -> Vector2 {
43        self.position
44    }
45
46    fn support_point(&self, direction: Vector2) -> Vector2 {
47        get_max_point(self.corners.iter(), direction, self.position())
48    }
49}
50
51impl Display for AABB {
52    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
53        write!(f, "AABB pos({0}), size({1})", self.position, self.size)
54    }
55}