Starting to work on motion through acceleration

This commit is contained in:
Thomas Forgione 2018-10-10 14:24:43 +02:00
parent 467aed4374
commit c80e5286a5
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
4 changed files with 33 additions and 13 deletions

View File

@ -20,6 +20,7 @@ use engine::physics;
use engine::math::{ use engine::math::{
duration_as_f32, duration_as_f32,
duration_as_frame, duration_as_frame,
clamp,
}; };
/// The different sides a character can face. /// The different sides a character can face.
@ -185,16 +186,24 @@ impl Updatable for Character {
let duration = duration_as_f32(duration); let duration = duration_as_f32(duration);
// Compute acceleration // Compute acceleration
let accel = physics::G; let accel = physics::G + force * 64.0 * 64.0;
// Compute speed // Compute speed
self.speed.x *= physics::GROUND_FRICTION.x; 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 { if self.speed.y > physics::MAXIMUM_VERTICAL_SPEED {
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 // Compute position
self.position += self.speed * duration; self.position += self.speed * duration;

View File

@ -6,7 +6,10 @@ use sfml::graphics::{
use engine::texture::Texture; use engine::texture::Texture;
use engine::renderer::Drawable; 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. /// This enum represents if the collision happens on the X axis or the Y axis.
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
@ -506,13 +509,4 @@ pub fn overlap(box1: FloatRect, box2: FloatRect) -> bool {
box2.top + box2.height > box1.top 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
}
}

View File

@ -5,6 +5,17 @@ use std::ops::{
IndexMut 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. /// Converts a duration into an animation frame number.
pub fn duration_as_frame(duration: &Duration, total: usize) -> i32 { pub fn duration_as_frame(duration: &Duration, total: usize) -> i32 {
let secs = duration_as_f32(duration); let secs = duration_as_f32(duration);

View File

@ -11,3 +11,9 @@ pub const JUMP_SPEED: Vector2<f32> = Vector2 { x: 0.0, y: -12.5 * 32.0 };
/// The maximum vertical speed. /// The maximum vertical speed.
pub const MAXIMUM_VERTICAL_SPEED: f32 = 10.0 * 32.0; 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;