Skip to content

Commit 5f8a8f1

Browse files
committed
Add AABB
1 parent 40331f5 commit 5f8a8f1

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

blobs/src/lib.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ impl Shape for Ball {
5757
fn as_cuboid(&self) -> Option<&Cuboid> {
5858
None
5959
}
60+
61+
fn calculate_aabb(&self, transform: Affine2) -> AABB {
62+
let min = transform.translation - vec2(self.radius, self.radius);
63+
let max = transform.translation + vec2(self.radius, self.radius);
64+
65+
AABB::new(min, max)
66+
}
6067
}
6168

6269
impl Ball {
@@ -68,6 +75,37 @@ impl Ball {
6875
pub trait Shape: 'static + Debug {
6976
fn as_ball(&self) -> Option<&Ball>;
7077
fn as_cuboid(&self) -> Option<&Cuboid>;
78+
fn calculate_aabb(&self, transform: Affine2) -> AABB;
79+
}
80+
81+
#[derive(Debug, Clone, Copy)]
82+
pub struct AABB {
83+
pub min: Vec2,
84+
pub max: Vec2,
85+
}
86+
87+
impl AABB {
88+
pub fn new(min: Vec2, max: Vec2) -> Self {
89+
Self { min, max }
90+
}
91+
92+
pub fn center(&self) -> Vec2 {
93+
(self.min + self.max) * 0.5
94+
}
95+
96+
pub fn size(&self) -> Vec2 {
97+
self.max - self.min
98+
}
99+
100+
pub fn expand_to_include_point(&mut self, point: Vec2) {
101+
self.min = self.min.min(point);
102+
self.max = self.max.max(point);
103+
}
104+
105+
pub fn expand_to_include_aabb(&mut self, other: &AABB) {
106+
self.min = self.min.min(other.min);
107+
self.max = self.max.max(other.max);
108+
}
71109
}
72110

73111
#[derive(Copy, Clone, Debug)]

0 commit comments

Comments
 (0)