Skip to content

Commit

Permalink
Fix SmoothDelta modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Oct 25, 2024
1 parent 841aa92 commit 00f9055
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 8 additions & 15 deletions src/input_context/input_modifier/smooth_delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -29,8 +27,7 @@ impl SmoothDelta {
Self {
kind: kind.into(),
speed,
old_value: Default::default(),
value_delta: Default::default(),
prev_value: Default::default(),
}
}
}
Expand All @@ -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())
}
}

Expand Down

0 comments on commit 00f9055

Please sign in to comment.