Skip to content

Commit 599f381

Browse files
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.
1 parent 9f2410a commit 599f381

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

crates/bevy_input/src/gamepad.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,22 @@ 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 = if new_value <= self.positive_low && new_value >= self.negative_low {
151+
0.0
152+
} else if new_value >= self.positive_high {
153+
1.0
154+
} else if new_value <= self.negative_high {
155+
-1.0
156+
} else {
157+
new_value
158+
};
159+
150160
if let Some(old_value) = old_value {
151161
if (new_value - old_value).abs() <= self.threshold {
152162
return None;
153163
}
154164
}
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-
}
165+
164166
Some(new_value)
165167
}
166168
}

0 commit comments

Comments
 (0)