1use std::fmt::{Display, Formatter};
2use lmaths::*;
3use crate::Shape;
4use crate::util::*;
5
6#[allow(dead_code)]
8#[derive(Clone, Copy, Default, PartialEq, Debug)]
9pub struct AABB {
10 pub position:Vector2,
12 pub size:Vector2,
13
14 corners: [Vector2; 4],
16}
17
18impl AABB {
19 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 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}