From c80e5286a5b8284c7bc7cf029eb206be34a91a92 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Wed, 10 Oct 2018 14:24:43 +0200 Subject: [PATCH] Starting to work on motion through acceleration --- src/engine/character/mod.rs | 13 +++++++++++-- src/engine/map/mod.rs | 16 +++++----------- src/engine/math/mod.rs | 11 +++++++++++ src/engine/physics/mod.rs | 6 ++++++ 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/engine/character/mod.rs b/src/engine/character/mod.rs index 3de743c..5494844 100644 --- a/src/engine/character/mod.rs +++ b/src/engine/character/mod.rs @@ -20,6 +20,7 @@ use engine::physics; use engine::math::{ duration_as_f32, duration_as_frame, + clamp, }; /// The different sides a character can face. @@ -185,16 +186,24 @@ impl Updatable for Character { let duration = duration_as_f32(duration); // Compute acceleration - let accel = physics::G; + let accel = physics::G + force * 64.0 * 64.0; // Compute speed self.speed.x *= physics::GROUND_FRICTION.x; - self.speed += accel * duration + force * 64.0; + self.speed += accel * duration; if self.speed.y > physics::MAXIMUM_VERTICAL_SPEED { self.speed.y = physics::MAXIMUM_VERTICAL_SPEED; } + let limit = if Key::Space.is_pressed() { + physics::MAXIMUM_RUNNING_SPEED + } else { + physics::MAXIMUM_WALKING_SPEED + }; + + self.speed.x = clamp(self.speed.x, -limit, limit); + // Compute position self.position += self.speed * duration; diff --git a/src/engine/map/mod.rs b/src/engine/map/mod.rs index f93274f..52844e2 100644 --- a/src/engine/map/mod.rs +++ b/src/engine/map/mod.rs @@ -6,7 +6,10 @@ use sfml::graphics::{ use engine::texture::Texture; use engine::renderer::Drawable; -use engine::math::Matrix; +use engine::math::{ + Matrix, + clamp, +}; /// This enum represents if the collision happens on the X axis or the Y axis. #[derive(Copy, Clone)] @@ -506,13 +509,4 @@ pub fn overlap(box1: FloatRect, box2: FloatRect) -> bool { box2.top + box2.height > box1.top } -/// Clamp a number between two boundaries. -pub fn clamp(number: f32, min: f32, max: f32) -> f32 { - if number < min { - min - } else if number > max { - max - } else { - number - } -} + diff --git a/src/engine/math/mod.rs b/src/engine/math/mod.rs index 29b2909..1729dd6 100644 --- a/src/engine/math/mod.rs +++ b/src/engine/math/mod.rs @@ -5,6 +5,17 @@ use std::ops::{ IndexMut }; +/// Clamp a number between two boundaries. +pub fn clamp(number: f32, min: f32, max: f32) -> f32 { + if number < min { + min + } else if number > max { + max + } else { + number + } +} + /// Converts a duration into an animation frame number. pub fn duration_as_frame(duration: &Duration, total: usize) -> i32 { let secs = duration_as_f32(duration); diff --git a/src/engine/physics/mod.rs b/src/engine/physics/mod.rs index b100677..c182744 100644 --- a/src/engine/physics/mod.rs +++ b/src/engine/physics/mod.rs @@ -11,3 +11,9 @@ pub const JUMP_SPEED: Vector2 = Vector2 { x: 0.0, y: -12.5 * 32.0 }; /// The maximum vertical speed. pub const MAXIMUM_VERTICAL_SPEED: f32 = 10.0 * 32.0; + +/// The maximul walking speed. +pub const MAXIMUM_WALKING_SPEED: f32 = 2.5 * 32.0; + +/// The maximum running speed. +pub const MAXIMUM_RUNNING_SPEED: f32 = 5.0 * 32.0;