better collision detection, i hope

This commit is contained in:
2015-01-06 23:12:35 +01:00
parent b5f94170f7
commit ff9ab15240
4 changed files with 115 additions and 74 deletions

View File

@@ -1,13 +1,13 @@
use sdl2::render::Renderer;
use sdl2::pixels::Color;
use sdl2::rect::Rect;
use vec::Vec2;
pub struct Player {
pub position: Vec2<f32>,
pub velocity: Vec2<f32>,
pub x: f32,
pub y: f32,
pub w: i32,
pub h: i32,
pub dx: f32,
pub dy: f32,
pub gravity: f32,
pub on_ground: bool
}
@@ -15,27 +15,28 @@ pub struct Player {
impl Player {
pub fn new(x: f32, y: f32) -> Player {
Player {
position: Vec2 { x: x, y: y },
velocity: Vec2 { x: 0.0, y: 0.0 },
x: x,
y: y,
w: 32,
h: 32,
dx: 0.0,
dy: 0.0,
gravity: 0.3,
on_ground: false
}
}
pub fn update(&mut self) {
self.velocity.y += self.gravity;
self.dy += 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;
if self.dy > 8.0 {
self.dy = 8.0;
} else if self.dy < -8.0 {
self.dy = -8.0;
}
self.position.x += self.velocity.x;
self.position.y += self.velocity.y;
}
pub fn to_rect(&self) -> Rect {
Rect::new(self.position.x as i32 - 16, self.position.y as i32 - 16, 32, 32)
Rect::new(self.x as i32, self.y as i32, self.w, self.h)
}
}