From 53cd523c3ac1fec39b8aa209b284251987668ffe Mon Sep 17 00:00:00 2001 From: mahfuzinam0045-spec Date: Tue, 24 Nov 2026 17:02:06 +0600 Subject: [PATCH] kbm: calculate mouse velocity over real dt to prevent camera whip (#963) When a title paused polling the gamepad for over 100 ms (during loading screens or frame hitches), mouse counts accumulated over the entire pause window. In Velocity::update, dt was clamped to 0.1 s before dividing, which artificially multiplied the apparent mouse rate by up to 20x and caused stick deflection to peg at 100%, violently whipping the camera. Compute the velocity rate using the actual elapsed time (with dt.max(0.001) to prevent division by zero). The exponential smoothing factor (1 - exp(-dt / SMOOTHING)) naturally decays without overshooting. Fixes #963. Assisted-by: Antigravity (Gemini 3.8 Flash) --- crates/xcl/src/hle/kbm.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/hle/kbm.rs b/crates/xcl/src/hle/kbm.rs index 6d18b1cfbc..0dcbbeff91 100644 --- a/crates/xcl/src/hle/kbm.rs +++ b/crates/xcl/src/hle/kbm.rs @@ -72,7 +72,7 @@ pub struct Velocity { impl Velocity { pub fn update(&mut self, dx: i32, dy: i32, dt: f32) { - let dt = dt.clamp(0.001, 0.1); + let dt = dt.max(0.001); let a = 1.0 - (-dt / SMOOTHING).exp(); self.x += a * (dx as f32 / dt - self.x); self.y += a * (dy as f32 / dt - self.y); @@ -490,6 +490,24 @@ mod tests { assert!(v.x.abs() < REST_SPEED && v.y.abs() < REST_SPEED); } + #[test] + fn frame_hitch_does_not_artificially_accelerate_velocity() { + let mut v = Velocity::default(); + // 300 counts over 300 ms is 1000 counts/s + v.update(300, -150, 0.3); + assert!((v.x - 1000.0).abs() < 10.0, "v.x should be ~1000, got {}", v.x); + assert!((v.y + 500.0).abs() < 10.0, "v.y should be ~-500, got {}", v.y); + let (rx, _) = stick_from_mouse(v.x, v.y, 1.0); + assert!(rx < 0.5, "stick deflection must remain moderate, got {}", rx); + + // 2000 counts over 2 seconds is 1000 counts/s; must not whip to full deflection + let mut v2 = Velocity::default(); + v2.update(2000, 0, 2.0); + assert!((v2.x - 1000.0).abs() < 10.0, "v2.x should be ~1000, got {}", v2.x); + let (rx2, _) = stick_from_mouse(v2.x, v2.y, 1.0); + assert!(rx2 < 0.5, "2s hitch must not cause full stick deflection, got {}", rx2); + } + #[test] fn wheel_notches_press_bumpers_one_by_one() { let mut w = Wheel::default();