@@ -38,32 +38,23 @@ impl Collide for Collidable {
3838 }
3939}
4040
41- /// calculate the axis aligned bounding box for a set of collidables
41+ /// calculate the minimal axis aligned bounding box that contains all `Collidable`s of a given set
4242pub fn calculate_aabb < ' a , I : Iterator < Item = & ' a Collidable > > ( vals : I ) -> AABox {
43- let ( mut minx, mut maxx, mut miny, mut maxy) = ( 0.0 , 0.0 , 0.0 , 0.0 ) ;
44- let mut first_iter = true ;
45- let mut f = |points : & [ Vec2 ] | {
46- for point in points {
47- if first_iter {
48- minx = point. x ( ) ;
49- maxx = point. x ( ) ;
50- miny = point. y ( ) ;
51- maxy = point. y ( ) ;
52- first_iter = false ;
53- } else {
54- minx = f32:: min ( minx, point. x ( ) ) ;
55- maxx = f32:: max ( maxx, point. x ( ) ) ;
56- miny = f32:: min ( miny, point. y ( ) ) ;
57- maxy = f32:: max ( maxy, point. y ( ) ) ;
58- }
59- }
43+ let [ ( mut minx, mut maxx) , ( mut miny, mut maxy) ] = [ ( f32:: INFINITY , f32:: NEG_INFINITY ) ; 2 ] ;
44+ let mut minmax = |v1 : & Vec2 , v2 : & Vec2 | {
45+ minx = f32:: min ( minx, v1. x ( ) ) ;
46+ maxx = f32:: max ( maxx, v2. x ( ) ) ;
47+ miny = f32:: min ( miny, v1. y ( ) ) ;
48+ maxy = f32:: max ( maxy, v2. y ( ) ) ;
6049 } ;
6150 for val in vals {
6251 match val {
63- Collidable :: Point ( v) => f ( & [ * v] ) ,
64- Collidable :: AABox ( AABox { pos, size } ) => f ( & [ * pos, * pos + * size] ) ,
65- Collidable :: RBox ( RBox { pos, v1, v2 } ) => {
66- f ( & [ * pos, * pos + * v1, * pos + * v2, * pos + * v1 + * v2] )
52+ Collidable :: Point ( v) => minmax ( v, v) ,
53+ Collidable :: AABox ( AABox { pos, size } ) => minmax ( pos, & ( * pos + * size) ) ,
54+ & Collidable :: RBox ( RBox { pos, v1, v2 } ) => {
55+ for point in & [ pos, pos + v1, pos + v2, pos + v1 + v2] {
56+ minmax ( point, point)
57+ }
6758 }
6859 } ;
6960 }
0 commit comments