Skip to content

Commit

Permalink
Fixed joystick axis scaling (#71)
Browse files Browse the repository at this point in the history
The old code did `* 1.05`, which is *not* equivalent to `* 100 / 95`.

That effectively capped the axis to 99.75, instead of being to reach 100.

fixes daid/EmptyEpsilon#1035
  • Loading branch information
gcask authored Mar 23, 2021
1 parent 9e92823 commit 49a6b27
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,19 @@ void InputHandler::handleEvent(sf::Event& event)
}
else if (event.type == sf::Event::JoystickMoved)
{
float axis_pos;
static constexpr float scale_factor = 100.f / (100.f - joystick_axis_snap_to_0_range);

float axis_pos = 0.f;

if (event.joystickMove.position > joystick_axis_snap_to_0_range) {
axis_pos = (event.joystickMove.position - joystick_axis_snap_to_0_range) * ((joystick_axis_snap_to_0_range / 100) + 1);
axis_pos = (event.joystickMove.position - joystick_axis_snap_to_0_range) * scale_factor;
} else if (event.joystickMove.position < -joystick_axis_snap_to_0_range) {
axis_pos = (event.joystickMove.position + joystick_axis_snap_to_0_range) * ((joystick_axis_snap_to_0_range / 100) + 1);
} else {
axis_pos = 0.0;
axis_pos = (event.joystickMove.position + joystick_axis_snap_to_0_range) * scale_factor;
}

// Clamp axis_pos within SFML range.
axis_pos = std::min(std::max(-100.f, axis_pos), 100.f);

if (joystick_axis_pos[event.joystickMove.joystickId][event.joystickMove.axis] != axis_pos){
joystick_axis_changed[event.joystickMove.joystickId][event.joystickMove.axis] = true;
}
Expand Down

0 comments on commit 49a6b27

Please sign in to comment.