From 2e35f90a1949dfbf64702cd76722e6ce94be1fd4 Mon Sep 17 00:00:00 2001 From: yuni Date: Sun, 17 Nov 2024 00:25:43 +0100 Subject: [PATCH] WIP crisper camera controls (prevent overshooting when braking) --- src/actor.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/actor.rs b/src/actor.rs index 551bf39..be89640 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -822,6 +822,7 @@ fn handle_wants_acceleration( let mut thruster_on = false; if let (Some(mut engine), Some(accel)) = (engine, accel) { let mut delta_v = DVec3::ZERO; + let mut allow_fullstop = false; let boost = engine.current_boost_factor; engine.currently_matching_velocity = false; @@ -836,7 +837,11 @@ fn handle_wants_acceleration( } if accel.direction != DVec3::ZERO { + // Player is pressing AWSD keys delta_v += (trans.rotation * accel.direction.as_vec3()).as_dvec3(); + } else if accel.brake { + // Player is only pressing space + allow_fullstop = true; } if delta_v.length_squared() > 0.003 { @@ -858,8 +863,25 @@ fn handle_wants_acceleration( * ENGINE_SPEED_FACTOR as f64 * engine.current_boost_factor; + let final_accel = delta_v.normalize() * engine_factor * dt as f64; + // Apply acceleration to velocity - **v += delta_v.normalize() * engine_factor * dt as f64; + if allow_fullstop { + if let Some(target_v) = closest_map.get(&entity) { + // Prevent overshooting when matching velocity, which + // would result in oscillating acceleration back and forth + for axis in 0..3 { + let original = v[axis]; + let target = target_v[axis]; + v[axis] += final_accel[axis]; + if (original - target).signum() != (v[axis] - target).signum() { + v[axis] = target; + } + } + } + } else { + **v += final_accel; + } // Visual effect if engine.engine_type == EngineType::Monopropellant {