From 52764376ae635a418f32ed4e35fe797174746c3a Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Tue, 4 Jun 2019 10:09:34 -0600 Subject: [PATCH] Use constants and normalized move direction --- common/src/sys/inputs.rs | 15 +++++++++++---- common/src/sys/phys.rs | 4 +++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/common/src/sys/inputs.rs b/common/src/sys/inputs.rs index 2e340ec7fd..97b88c3e2b 100644 --- a/common/src/sys/inputs.rs +++ b/common/src/sys/inputs.rs @@ -24,6 +24,8 @@ const HUMANOID_AIR_SPEED: f32 = 100.0; const HUMANOID_JUMP_ACCEL: f32 = 16.0; const GLIDE_ACCEL: f32 = 15.0; const GLIDE_SPEED: f32 = 45.0; +// Gravity is 9.81 * 4, so this makes gravity equal to .15 +const GLIDE_ANTIGRAV: f32 = 9.81 * 3.95; impl<'a> System<'a> for Sys { type SystemData = ( @@ -88,11 +90,16 @@ impl<'a> System<'a> for Sys { && vel.0.z <= 0.0; let gliding = glides.get(entity).is_some() && vel.0.z < 0.0; + let move_dir = if control.move_dir.magnitude() > 0.0 { + control.move_dir.normalized() + } else { + control.move_dir + }; if on_ground { // Move player according to move_dir if vel.0.magnitude() < HUMANOID_SPEED { - vel.0 += Vec2::broadcast(dt.0) * control.move_dir * HUMANOID_ACCEL; + vel.0 += Vec2::broadcast(dt.0) * move_dir * HUMANOID_ACCEL; } // Jump @@ -101,11 +108,11 @@ impl<'a> System<'a> for Sys { jumps.remove(entity); } } else if gliding && vel.0.magnitude() < GLIDE_SPEED { - let anti_grav = 9.81 * 3.95 + vel.0.z.powf(2.0) * 0.2; + let anti_grav = GLIDE_ANTIGRAV + vel.0.z.powf(2.0) * 0.2; vel.0.z += dt.0 * anti_grav * Vec2::::from(vel.0 * 0.15).magnitude().min(1.0); - vel.0 += Vec2::broadcast(dt.0) * control.move_dir * GLIDE_ACCEL; + vel.0 += Vec2::broadcast(dt.0) * move_dir * GLIDE_ACCEL; } else if vel.0.magnitude() < HUMANOID_AIR_SPEED { - vel.0 += Vec2::broadcast(dt.0) * control.move_dir * HUMANOID_AIR_ACCEL; + vel.0 += Vec2::broadcast(dt.0) * move_dir * HUMANOID_AIR_ACCEL; } // Set direction based on velocity diff --git a/common/src/sys/phys.rs b/common/src/sys/phys.rs index 738b6e667d..ec07a9a478 100644 --- a/common/src/sys/phys.rs +++ b/common/src/sys/phys.rs @@ -14,6 +14,8 @@ use vek::*; pub struct Sys; const GRAVITY: f32 = 9.81 * 4.0; +const FRIC_GROUND: f32 = 0.15; +const FRIC_AIR: f32 = 0.015; // Integrates forces, calculates the new velocity based off of the old velocity // dt = delta time @@ -61,7 +63,7 @@ impl<'a> System<'a> for Sys { // Integrate forces // Friction is assumed to be a constant dependent on location - let friction = 50.0 * if on_ground { 0.15 } else { 0.015 }; + let friction = 50.0 * if on_ground { FRIC_GROUND } else { FRIC_AIR }; vel.0 = integrate_forces(dt.0, vel.0, friction); // Movement