From dd49906ccebe306a748060df4a3cc7716aa76288 Mon Sep 17 00:00:00 2001 From: hut Date: Fri, 29 Mar 2024 02:41:05 +0100 Subject: [PATCH] clamp input vector, allowing slow motion while holding space In typical games we would normalize the input vector so that diagonal movement is as fast as forward or sideways movement. But here, we merely clamp each direction to an absolute maximum of 1, since every thruster can be used separately. If the forward thrusters and the leftward thrusters are active at the same time, then of course the total diagonal acceleration is faster than the forward acceleration alone. --- src/camera.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index ab204c5..700c3fb 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -101,10 +101,16 @@ fn run_camera_controller( if key_input.pressed(settings.key_stop) { let stop_direction = -actor.v.normalize(); if stop_direction.length_squared() > 0.3 { - axis_input += 0.7 * (transform.rotation.inverse() * stop_direction); + axis_input += 0.8 * (transform.rotation.inverse() * stop_direction); } } } + // In typical games we would normalize the input vector so that diagonal movement is as + // fast as forward or sideways movement. But here, we merely clamp each direction to an + // absolute maximum of 1, since every thruster can be used separately. If the forward + // thrusters and the leftward thrusters are active at the same time, then of course the + // total diagonal acceleration is faster than the forward acceleration alone. + axis_input = axis_input.clamp(Vec3::splat(-1.0), Vec3::splat(1.0)); let mut engine = if let Ok(engine) = q_engine.get_single_mut() { engine } else { player_engine }; @@ -119,7 +125,7 @@ fn run_camera_controller( let factor = Vec3::new(right_factor, up_factor, forward_factor); if axis_input.length_squared() > 0.003 { - let acceleration_global = transform.rotation * (axis_input.normalize() * factor); + let acceleration_global = transform.rotation * (axis_input * factor); actor.v += actor::ENGINE_SPEED_FACTOR * dt * acceleration_global; engine.current_warmup = (engine.current_warmup + dt / engine.warmup_seconds).clamp(0.0, 1.0);