From 00f90559d46644989d44337e8c80bbcfa9ef9d34 Mon Sep 17 00:00:00 2001 From: Hennadii Chernyshchyk Date: Fri, 25 Oct 2024 03:03:42 +0300 Subject: [PATCH] Fix `SmoothDelta` modifier --- CHANGELOG.md | 4 ++++ .../input_modifier/smooth_delta.rs | 23 +++++++------------ 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf1f542..a195d23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Logging for binding. +### Fixed + +- `SmoothDelta` modifier. + ### Changed - Remove world access from conditions and modifiers. This means that you no longer can write game-specific conditions or modifiers. But it's much nicer (and faster) to just do it in observers instead. diff --git a/src/input_context/input_modifier/smooth_delta.rs b/src/input_context/input_modifier/smooth_delta.rs index 8f71587..a322f71 100644 --- a/src/input_context/input_modifier/smooth_delta.rs +++ b/src/input_context/input_modifier/smooth_delta.rs @@ -18,9 +18,7 @@ pub struct SmoothDelta { /// Multiplier for delta time. pub speed: f32, - old_value: Vec3, - - value_delta: Vec3, + prev_value: Vec3, } impl SmoothDelta { @@ -29,8 +27,7 @@ impl SmoothDelta { Self { kind: kind.into(), speed, - old_value: Default::default(), - value_delta: Default::default(), + prev_value: Default::default(), } } } @@ -42,21 +39,17 @@ impl InputModifier for SmoothDelta { return self.apply(time, value.into()); } - let dim = value.dim(); - let value = value.as_axis3d(); - let target_value_delta = (value - self.old_value).normalize_or_zero(); - self.old_value = value; - - let alpha = (time.delta_seconds() * self.speed).min(1.0); - self.value_delta = match self.kind { + let alpha = time.delta_seconds() * self.speed; + let smoothed = match self.kind { SmoothKind::EaseFunction(ease_function) => { let ease_alpha = alpha.calc(ease_function); - self.value_delta.lerp(target_value_delta, ease_alpha) + self.prev_value.lerp(value.as_axis3d(), ease_alpha) } - SmoothKind::Linear => self.value_delta.lerp(target_value_delta, alpha), + SmoothKind::Linear => self.prev_value.lerp(value.as_axis3d(), alpha), }; + self.prev_value = smoothed; - ActionValue::Axis3D(self.value_delta).convert(dim) + ActionValue::Axis3D(smoothed).convert(value.dim()) } }