From 599f381a6a98eded6c96c1d2225a1a2b30384aaf Mon Sep 17 00:00:00 2001 From: Jakob Hellermann Date: Thu, 7 Jan 2021 21:35:40 +0100 Subject: [PATCH] fix repeated gamepad events (#1221) Previously, if the actual value of LeftStickX was e.g. 0.034 and fluctuated a little bit (less than the threshold) it would repeatedly send out events, because it compared the value to the *filtered* old one - 0.0 - which is more then `0.01` (the threshold) away. The is fixed by first doing the deadzone and then comparing to the old value. Another possible solution would be to store both the actual old value and the filtered one, but that would add complexity. --- crates/bevy_input/src/gamepad.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index c3707e6642..9106efe872 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -147,20 +147,22 @@ impl Default for AxisSettings { impl AxisSettings { fn filter(&self, new_value: f32, old_value: Option) -> Option { + let new_value = if new_value <= self.positive_low && new_value >= self.negative_low { + 0.0 + } else if new_value >= self.positive_high { + 1.0 + } else if new_value <= self.negative_high { + -1.0 + } else { + new_value + }; + if let Some(old_value) = old_value { if (new_value - old_value).abs() <= self.threshold { return None; } } - if new_value <= self.positive_low && new_value >= self.negative_low { - return Some(0.0); - } - if new_value >= self.positive_high { - return Some(1.0); - } - if new_value <= self.negative_high { - return Some(-1.0); - } + Some(new_value) } }