better collision handling

This commit is contained in:
2014-12-23 21:51:17 +01:00
parent 4c298d3a64
commit e313bbf80a
2 changed files with 36 additions and 17 deletions

View File

@@ -1,5 +1,6 @@
extern crate sdl2;
use std::cmp::{max, min};
use sdl2::video::{Window, WindowPos, OPENGL};
use sdl2::event::{poll_event, Event};
@@ -114,25 +115,30 @@ fn main() {
player.on_ground = false;
for object in objects.iter() {
let mut i : uint = 0;
if collision_detection(&object.get_rect(), &player.get_rect()) {
let intersect = collision_intersect(&object.get_rect(), &player.get_rect());
loop {
if !collision_detection(&object.get_rect(), &player.get_rect()) {
if i > 0 {
if intersect.w >= intersect.h {
let mut delta = intersect.h as f32;
if player.velocity.y >= 0.0 {
delta *= -1.0;
}
player.position.y += delta;
player.velocity.y = 0.0;
player.on_ground = true;
} else {
let mut delta = intersect.w as f32;
if player.velocity.x >= 0.0 {
delta *= -1.0;
}
break;
player.position.x += delta;
player.velocity.x = 0.0;
}
i += 1;
if i > 100 {
break;
}
player.position.y -= player.velocity.y * 0.05;
}
}
@@ -157,13 +163,20 @@ fn main() {
}
fn collision_detection(lhs: &Rect, rhs: &Rect) -> bool {
if lhs.x + lhs.w < rhs.x || rhs.x + rhs.w < lhs.x {
if lhs.x + lhs.w <= rhs.x || rhs.x + rhs.w <= lhs.x {
false
}
else if lhs.y + lhs.h < rhs.y || rhs.y + rhs.h < lhs.y {
else if lhs.y + lhs.h <= rhs.y || rhs.y + rhs.h <= lhs.y {
false
}
else {
true
}
}
fn collision_intersect(lhs: &Rect, rhs: &Rect) -> Rect {
let x = max(lhs.x, rhs.x);
let y = max(lhs.y, rhs.y);
Rect::new(x, y, min(lhs.x + lhs.w, rhs.x + rhs.w) - x, min(lhs.y + lhs.h, rhs.y + rhs.h) - y)
}

View File

@@ -23,6 +23,12 @@ impl Player {
pub fn update(&mut self) {
self.velocity.y += self.gravity;
if self.velocity.y > 8.0 {
self.velocity.y = 8.0;
} else if self.velocity.y < -8.0 {
self.velocity.y = -8.0;
}
self.position.x += self.velocity.x;
self.position.y += self.velocity.y;
}