From b2b0580f3056f9d0da7256a75fa043a62ef53abb Mon Sep 17 00:00:00 2001 From: terencehill Date: Sun, 2 Jun 2024 16:17:08 +0200 Subject: [PATCH] Weapon bob effect: smooth speed changes to get rid of weapon stuttering on direction changes; reduce bob frequency when player walks crouching --- qcsrc/client/view.qc | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/qcsrc/client/view.qc b/qcsrc/client/view.qc index cb72f4ebb..19f3c0a2c 100644 --- a/qcsrc/client/view.qc +++ b/qcsrc/client/view.qc @@ -217,6 +217,7 @@ vector bobmodel_ofs(entity view) vector gunorg = '0 0 0'; static float bobmodel_scale = 0; static float time_ofs = 0; // makes the effect always restart in the same way + static float avg_xyspeed = 0; // makes the effect always restart in the same way if (clonground) { if (time - hitgroundtime > 0.05) @@ -225,13 +226,27 @@ vector bobmodel_ofs(entity view) else bobmodel_scale = max(0, bobmodel_scale - frametime * 5); - float xyspeed = bound(0, vlen(vec2(view.velocity)), 400); - if (bobmodel_scale && xyspeed) - { - float bspeed = xyspeed * 0.01 * bobmodel_scale; - float s = (time - time_ofs) * autocvar_cl_bobmodel_speed; - gunorg.y = bspeed * autocvar_cl_bobmodel_side * sin(s); - gunorg.z = bspeed * autocvar_cl_bobmodel_up * cos(s * 2); + if (bobmodel_scale) + { + float xyspeed = bound(0, vlen(vec2(view.velocity)), 400); + // smooth speed changes to get rid of weapon stuttering on direction changes + const float avg_time = 0.1; + float frac = 1 - exp(-frametime / max(0.001, avg_time)); + avg_xyspeed = frac * xyspeed + (1 - frac) * avg_xyspeed; + // stop bobbing at a very low value of avg_xyspeed instead of 0 + // since the averaging formula takes a very long time to reach exactly 0 + if (avg_xyspeed < 1) + time_ofs = time; + else + { + if (avg_xyspeed < 400) // reduce bob frequency too when player walks crouching + time_ofs += frametime * map_bound_ranges(avg_xyspeed, 150, 400, 0.08, 0); + + float bspeed = avg_xyspeed * 0.01 * bobmodel_scale; + float s = (time - time_ofs) * autocvar_cl_bobmodel_speed; + gunorg.y = bspeed * autocvar_cl_bobmodel_side * sin(s); + gunorg.z = bspeed * autocvar_cl_bobmodel_up * cos(s * 2); + } } else time_ofs = time; -- 2.39.2