From 4075c95dff60c4ca613cfb97d2608ff33231173e Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Mon, 28 Jan 2019 22:59:05 -0800 Subject: [PATCH] yeah that didn't fix it --- src/main.rs | 6 +++--- src/player.rs | 41 +++++++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3cb6e66d..87e9d8e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -150,16 +150,16 @@ fn main() { // intersection test let mut interferences = Vec::new(); { - let bv = player.bounding_aabb(); + // Get the AABB bounding box + let (bv, _) = player.future_bounding_aabb(delta_time); let mut thing = BoundingVolumeInterferencesCollector::new(&bv, &mut interferences); bvt.visit(&mut thing); } + let collision_rect = player.collision(&interferences, delta_time); - let collision_rect = player.collision(&interferences); player.update(delta_time); - let mut collision_sprite = RectangleShape::new(); collision_sprite.set_position((collision_rect.left, collision_rect.top)); collision_sprite.set_size((collision_rect.width, collision_rect.height)); diff --git a/src/player.rs b/src/player.rs index aaada818..13bf63b2 100644 --- a/src/player.rs +++ b/src/player.rs @@ -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 { - let bounds = self.head.global_bounds(); + pub fn future_bounding_aabb(&mut self, delta_t: f32) -> (AABB, 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: 40.0} + default_impulse: 10.0} } }