Skip to content

Commit

Permalink
fix repeated gamepad events (bevyengine#1221)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jakobhellermann authored and rparrett committed Jan 27, 2021
1 parent 61b58c4 commit 894a6ef
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions crates/bevy_input/src/gamepad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,22 @@ impl Default for AxisSettings {

impl AxisSettings {
fn filter(&self, new_value: f32, old_value: Option<f32>) -> Option<f32> {
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)
}
}
Expand Down

0 comments on commit 894a6ef

Please sign in to comment.