@@ -57,6 +57,13 @@ impl Shape for Ball {
57
57
fn as_cuboid ( & self ) -> Option < & Cuboid > {
58
58
None
59
59
}
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
+ }
60
67
}
61
68
62
69
impl Ball {
@@ -68,6 +75,37 @@ impl Ball {
68
75
pub trait Shape : ' static + Debug {
69
76
fn as_ball ( & self ) -> Option < & Ball > ;
70
77
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
+ }
71
109
}
72
110
73
111
#[ derive( Copy , Clone , Debug ) ]
0 commit comments