Skip to content

Commit c59a0fe

Browse files
fix repeated gamepad events
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.
1 parent c69aa98 commit c59a0fe

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

crates/bevy_input/src/gamepad.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,19 @@ impl Default for AxisSettings {
147147

148148
impl AxisSettings {
149149
fn filter(&self, new_value: f32, old_value: Option<f32>) -> Option<f32> {
150+
let new_value = match new_value {
151+
value if value <= self.positive_low && value >= self.negative_low => 0.0,
152+
value if value >= self.positive_high => 1.0,
153+
value if value <= self.negative_high => -1.0,
154+
other => other,
155+
};
156+
150157
if let Some(old_value) = old_value {
151158
if (new_value - old_value).abs() <= self.threshold {
152159
return None;
153160
}
154161
}
155-
if new_value <= self.positive_low && new_value >= self.negative_low {
156-
return Some(0.0);
157-
}
158-
if new_value >= self.positive_high {
159-
return Some(1.0);
160-
}
161-
if new_value <= self.negative_high {
162-
return Some(-1.0);
163-
}
162+
164163
Some(new_value)
165164
}
166165
}

0 commit comments

Comments
 (0)