From 6444ba1933a8e8ec098a25e31dc8dfc590fd7543 Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Thu, 27 Jun 2019 15:21:41 +0100 Subject: [PATCH] Made collision detection cheaper, fixed snapping bug, somewhat nerfed rolling --- common/src/state.rs | 2 +- common/src/sys/phys.rs | 20 +++++++------------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/common/src/state.rs b/common/src/state.rs index d04e814fe7..7b7f0bd46a 100644 --- a/common/src/state.rs +++ b/common/src/state.rs @@ -38,7 +38,7 @@ pub struct DeltaTime(pub f32); /// too fast, we'd skip important physics events like collisions. This constant determines the /// upper limit. If delta time exceeds this value, the game's physics will begin to produce time /// lag. Ideally, we'd avoid such a situation. -const MAX_DELTA_TIME: f32 = 0.1; +const MAX_DELTA_TIME: f32 = 1.0; pub struct Changes { pub new_chunks: HashSet>, diff --git a/common/src/sys/phys.rs b/common/src/sys/phys.rs index fb03a430bf..cd51d6ae32 100644 --- a/common/src/sys/phys.rs +++ b/common/src/sys/phys.rs @@ -15,7 +15,7 @@ const HUMANOID_SPEED: f32 = 120.0; const HUMANOID_AIR_ACCEL: f32 = 10.0; const HUMANOID_AIR_SPEED: f32 = 100.0; const HUMANOID_JUMP_ACCEL: f32 = 16.0; -const ROLL_ACCEL: f32 = 160.0; +const ROLL_ACCEL: f32 = 120.0; const ROLL_SPEED: f32 = 550.0; const GLIDE_ACCEL: f32 = 15.0; const GLIDE_SPEED: f32 = 45.0; @@ -28,19 +28,11 @@ const GLIDE_ANTIGRAV: f32 = 9.81 * 3.95; // damp = linear damping // Friction is a type of damping. fn integrate_forces(dt: f32, mut lv: Vec3, damp: f32) -> Vec3 { - lv.z -= (GRAVITY * dt).max(-50.0); + lv.z = (lv.z - GRAVITY * dt).max(-50.0); - let mut linear_damp = 1.0 - dt * damp; + let linear_damp = (1.0 - dt * damp).max(0.0); - if linear_damp < 0.0 - // reached zero in the given time - { - linear_damp = 0.0; - } - - lv *= linear_damp; - - lv + lv * linear_damp } /// This system applies forces and calculates new positions and velocities. @@ -189,6 +181,7 @@ impl<'a> System<'a> for Sys { false }; + let was_on_ground = on_grounds.get(entity).is_some(); on_grounds.remove(entity); // Assume we're in the air - unless we can prove otherwise pos.0.z -= 0.0001; // To force collision with the floor @@ -251,7 +244,7 @@ impl<'a> System<'a> for Sys { let resolve_dir = -dir.map(|e| if e.abs() == max_axis { e } else { 0.0 }); // When the resolution direction is pointing upwards, we must be on the ground - if resolve_dir.z > 0.0 { + if resolve_dir.z > 0.0 && vel.0.z <= 0.0 { on_ground = true; } @@ -282,6 +275,7 @@ impl<'a> System<'a> for Sys { } else if collision_with(pos.0 - Vec3::unit_z() * 1.0, near_iter.clone()) && vel.0.z < 0.0 && vel.0.z > -1.0 + && was_on_ground { pos.0.z = (pos.0.z - 0.05).floor(); on_grounds.insert(entity, OnGround);