fiddling with the movement
This commit is contained in:
24
src/main.rs
24
src/main.rs
@@ -27,6 +27,8 @@ use sfml::system::Vector2 as sfVec2;
|
||||
use ncollide2d::bounding_volume::{self, AABB, BoundingVolumeInterferencesCollector};
|
||||
use ncollide2d::partitioning::BVT;
|
||||
use sfml::graphics::RectangleShape;
|
||||
use std::{thread, time};
|
||||
|
||||
|
||||
|
||||
fn main() {
|
||||
@@ -39,12 +41,17 @@ fn main() {
|
||||
let mut block_sprite = Sprite::new();
|
||||
block_sprite.set_texture(&spritesheet_text, false);
|
||||
block_sprite.set_texture_rect(&util::grab_sheet_rec(String::from("blockBrown.png"), &spritesheet_desc));
|
||||
block_sprite.set_position((70.0, 70.0));
|
||||
block_sprite.set_position((64.0, 64.0));
|
||||
|
||||
let mut block_sprite2 = Sprite::new();
|
||||
block_sprite2.set_texture(&spritesheet_text, false);
|
||||
block_sprite2.set_texture_rect(&util::grab_sheet_rec(String::from("blockBrown.png"), &spritesheet_desc));
|
||||
block_sprite2.set_position((170.0, 170.0));
|
||||
block_sprite2.set_position((128.0, 64.0));
|
||||
|
||||
let mut block_sprite3 = Sprite::new();
|
||||
block_sprite3.set_texture(&spritesheet_text, false);
|
||||
block_sprite3.set_texture_rect(&util::grab_sheet_rec(String::from("blockBrown.png"), &spritesheet_desc));
|
||||
block_sprite3.set_position((192.0, 64.0));
|
||||
|
||||
let idx_and_bounding_spheres: Vec<(&Sprite, AABB<f64>)> = vec![
|
||||
(
|
||||
@@ -65,6 +72,15 @@ fn main() {
|
||||
na::Point2::new((pos.x + bounds.width) as f64, (pos.y + bounds.width) as f64))
|
||||
},
|
||||
),
|
||||
(
|
||||
&block_sprite3,
|
||||
{
|
||||
let bounds = &block_sprite3.local_bounds();
|
||||
let pos = &block_sprite3.position();
|
||||
bounding_volume::AABB::new(na::Point2::new(pos.x as f64, pos.y as f64),
|
||||
na::Point2::new((pos.x + bounds.width) as f64, (pos.y + bounds.width) as f64))
|
||||
},
|
||||
),
|
||||
];
|
||||
|
||||
let bvt = BVT::new_balanced(idx_and_bounding_spheres);
|
||||
@@ -149,6 +165,9 @@ fn main() {
|
||||
collision_sprite.set_size((collision_rect.width, collision_rect.height));
|
||||
|
||||
|
||||
let ten_millis = time::Duration::from_millis(10);
|
||||
thread::sleep(ten_millis);
|
||||
|
||||
window.clear(&Color::BLACK);
|
||||
window.draw(&player);
|
||||
window.draw(&collision_sprite);
|
||||
@@ -156,6 +175,7 @@ fn main() {
|
||||
if interferences.len() == 0 {
|
||||
window.draw(&block_sprite);
|
||||
window.draw(&block_sprite2);
|
||||
window.draw(&block_sprite3);
|
||||
}
|
||||
|
||||
window.display();
|
||||
|
||||
@@ -11,12 +11,19 @@ pub struct Player<'s> {
|
||||
head: CircleShape<'s>,
|
||||
delta: Vector2<f32>,
|
||||
pub pos: Vector2<f32>,
|
||||
|
||||
default_impulse: f32,
|
||||
}
|
||||
|
||||
impl<'s> Player<'s> {
|
||||
pub fn impulse(&mut self, delta_v: &Vector2<f32>) {
|
||||
self.delta.x += delta_v.x * 2.0;
|
||||
self.delta.y += delta_v.y * 2.0;
|
||||
self.delta.x += delta_v.x * self.default_impulse;
|
||||
self.delta.y += delta_v.y * self.default_impulse;
|
||||
}
|
||||
|
||||
pub fn velocity(&mut self, delta_v: &Vector2<f32>) {
|
||||
self.delta.x = delta_v.x * self.default_impulse;
|
||||
self.delta.y = delta_v.y * self.default_impulse;
|
||||
}
|
||||
|
||||
pub fn collision(&mut self, objects: &Vec<&Sprite>) -> FloatRect {
|
||||
@@ -25,22 +32,44 @@ impl<'s> Player<'s> {
|
||||
|
||||
for i in objects {
|
||||
match self.head.global_bounds().intersection(&i.global_bounds()) {
|
||||
Some(r) => {
|
||||
let tested = &i.global_bounds();
|
||||
Some(overlap) => {
|
||||
|
||||
// if r.width > tested.width / 2.0 {
|
||||
// self.delta.x = 1.0;
|
||||
// } else if r.width < tested.width / 2.0 {
|
||||
// self.delta.x = -1.0;
|
||||
// }
|
||||
//
|
||||
// if r.height > tested.height / 2.0 {
|
||||
// self.delta.y = -1.0;
|
||||
// } else if r.height < tested.height / 2.0 {
|
||||
// self.delta.y = 1.0;
|
||||
// }
|
||||
//println!("{:?}", r);
|
||||
collided = r;
|
||||
// Get the bounds of the object we're intersecting
|
||||
let intersector = &i.global_bounds();
|
||||
|
||||
let bounding_box = self.head.global_bounds();
|
||||
|
||||
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;
|
||||
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;
|
||||
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;
|
||||
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;
|
||||
reposition.y = intersector.top + intersector.height + 1.0;
|
||||
}
|
||||
}
|
||||
|
||||
self.head.set_position(reposition);
|
||||
self.pos = reposition;
|
||||
self.velocity(&deflection);
|
||||
collided = overlap;
|
||||
},
|
||||
None => continue
|
||||
}
|
||||
@@ -50,9 +79,9 @@ impl<'s> Player<'s> {
|
||||
}
|
||||
|
||||
pub fn bounding_aabb(&mut self) -> AABB<f64> {
|
||||
let pos = self.pos;
|
||||
let a = na::Point2::new(pos.x as f64, pos.y as f64);
|
||||
let b = na::Point2::new((pos.x + 10.0) as f64, (pos.y + 10.0) as f64);
|
||||
let bounds = self.head.global_bounds();
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -60,13 +89,18 @@ impl<'s> Player<'s> {
|
||||
self.pos.x += self.delta.x * delta_t * 1.0;
|
||||
self.pos.y += self.delta.y * delta_t * 1.0;
|
||||
|
||||
let friction = 10.0 * self.delta;
|
||||
self.delta -= friction * delta_t;
|
||||
let friction = 10.0;
|
||||
let ratio = 1.0 / (1.0 + delta_t * friction);
|
||||
self.delta.x *= ratio;
|
||||
|
||||
// Gravity
|
||||
self.delta.y += 45.0 * delta_t;
|
||||
|
||||
self.head.set_position((self.pos.x, self.pos.y));
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
|
||||
let mut delta = Vector2::new(0.0, 0.0);
|
||||
let mut pos = Vector2::new(0.0, 0.0);
|
||||
|
||||
@@ -74,7 +108,8 @@ impl<'s> Player<'s> {
|
||||
head.set_position((delta.x, delta.y));
|
||||
head.set_fill_color(&Color::RED);
|
||||
|
||||
Self { head, delta, pos }
|
||||
Self { head, delta, pos,
|
||||
default_impulse: 40.0}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user