@ -26,42 +26,47 @@ impl<'s> Player<'s> {
self . delta . y = delta_v . y * self . default_impulse ;
}
pub fn collision ( & mut self , objects : & Vec < & Sprite > ) -> FloatRect {
pub fn collision ( & mut self , objects : & Vec < & Sprite > , delta_t : f32 ) -> FloatRect {
let mut collided = FloatRect ::new ( 0.0 , 0.0 , 0.0 , 0.0 ) ;
let ( _ , future_bounding ) = self . future_bounding_aabb ( delta_t ) ;
for i in objects {
match self . head . global_bounds ( ) . intersection ( & i . global_bounds ( ) ) {
match future_bounding . intersection ( & i . global_bounds ( ) ) {
Some ( overlap ) = > {
// Get the bounds of the object we're intersecting
let intersector = & i . global_bounds ( ) ;
let bounding_box = self . head . global_bounds ( ) ;
let bounding_box = future_bounding ;
let mut deflection = self . delta ;
let mut reposition = self . pos ;
if overlap . width < overlap . height {
if bounding_box . left + bounding_box . width > = intersector . left & &
bounding_box . left < intersector . left {
deflection . x = - 0.1 ;
deflection . x = - 0.1 * delta_t ;
reposition . x = intersector . left - bounding_box . width - 1.0 ;
} else if bounding_box . left < = intersector . left + intersector . width & &
bounding_box . left + bounding_box . width > intersector . left + bounding_box . width {
deflection . x = 0.1 ;
deflection . x = 0.1 * delta_t ;
reposition . x = intersector . left + intersector . width + 1.0 ;
}
} else {
if bounding_box . top + bounding_box . height > = intersector . top & &
bounding_box . top < intersector . top {
deflection . y = - 0.1 ;
deflection . y = - 0.1 * delta_t ;
reposition . y = intersector . top - bounding_box . height - 1.0 ;
} else if bounding_box . top < = intersector . top + intersector . height & &
bounding_box . top + bounding_box . height > intersector . top + bounding_box . height {
deflection . y = 0.1 ;
deflection . y = 0.1 * delta_t ;
reposition . y = intersector . top + intersector . height + 1.0 ;
}
}
@ -78,23 +83,27 @@ impl<'s> Player<'s> {
return collided ;
}
pub fn bounding_aabb ( & mut self ) -> AABB < f64 > {
let bounds = self . head . global_bounds ( ) ;
pub fn future_bounding_aabb ( & mut self , delta_t : f32 ) -> ( AABB < f64 > , FloatRect ) {
let mut bounds = self . head . global_bounds ( ) ;
bounds . left + = self . delta . x * delta_t * 8.0 ;
bounds . top + = self . delta . y * delta_t * 8.0 ;
let a = na ::Point2 ::new ( bounds . left as f64 , bounds . top as f64 ) ;
let b = na ::Point2 ::new ( ( bounds . left + bounds . width ) as f64 , ( bounds . top + bounds . height ) as f64 ) ;
AABB ::new ( a , b )
( AABB ::new ( a , b ) , bounds )
}
pub fn update ( & mut self , delta_t : f32 ) {
self . pos . x + = self . delta . x * delta_t * 1 .0;
self . pos . y + = self . delta . y * delta_t * 1 .0;
self . pos . x + = self . delta . x * delta_t * 8 .0;
self . pos . y + = self . delta . y * delta_t * 8 .0;
println! ( "{:?}" , self . delta ) ;
let friction = 10.0 ;
let ratio = 1.0 / ( 1.0 + delta_t * friction ) ;
self . delta . x * = ratio ;
self . delta * = ratio ;
// Gravity
self . delta . y + = 45.0 * delta_t ;
// self.delta.y += 45.0 * delta_t;
self . head . set_position ( ( self . pos . x , self . pos . y ) ) ;
}
@ -109,7 +118,7 @@ impl<'s> Player<'s> {
head . set_fill_color ( & Color ::RED ) ;
Self { head , delta , pos ,
default_impulse : 4 0.0}
default_impulse : 1 0.0}
}
}