mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Joystick sensitivity for player movement (#11262)
This commit deprecates the forward, backward, left, and right binary inputs currently used for player movement in the PlayerControl struct. In their place, it adds the movement_speed and movement_direction values, which represents the player movement is a polar coordinate system. movement_speed is a scalar from 0.0 to 1.0. movement_direction is an angle from 0 to +-Pi: FWD 0 _ LFT / \ RGT -Pi/2 | | +Pi/2 \_/ +-Pi BCK Boolean movement bits will still be set for server telegrams and Lua script invocations to provide full backward compatibility. When generating these values from an analog input, a direction is considered active when it is 22.5 degrees away from either orthogonal axis. Co-authored-by: Markus Koch <markus@notsyncing.net> Co-authored-by: sfan5 <sfan5@live.de>
This commit is contained in:
parent
149d8fc8d6
commit
1d69a23ba4
10 changed files with 137 additions and 98 deletions
|
@ -160,6 +160,7 @@ JoystickController::JoystickController() :
|
|||
for (float &i : m_past_pressed_time) {
|
||||
i = 0;
|
||||
}
|
||||
m_layout.axes_deadzone = 0;
|
||||
clear();
|
||||
}
|
||||
|
||||
|
@ -251,10 +252,27 @@ void JoystickController::clear()
|
|||
memset(m_axes_vals, 0, sizeof(m_axes_vals));
|
||||
}
|
||||
|
||||
s16 JoystickController::getAxisWithoutDead(JoystickAxis axis)
|
||||
float JoystickController::getAxisWithoutDead(JoystickAxis axis)
|
||||
{
|
||||
s16 v = m_axes_vals[axis];
|
||||
|
||||
if (abs(v) < m_layout.axes_deadzone)
|
||||
return 0;
|
||||
return v;
|
||||
return 0.0f;
|
||||
|
||||
v += (v < 0 ? m_layout.axes_deadzone : -m_layout.axes_deadzone);
|
||||
|
||||
return (float)v / ((float)(INT16_MAX - m_layout.axes_deadzone));
|
||||
}
|
||||
|
||||
float JoystickController::getMovementDirection()
|
||||
{
|
||||
return atan2(getAxisWithoutDead(JA_SIDEWARD_MOVE), -getAxisWithoutDead(JA_FORWARD_MOVE));
|
||||
}
|
||||
|
||||
float JoystickController::getMovementSpeed()
|
||||
{
|
||||
float speed = sqrt(pow(getAxisWithoutDead(JA_FORWARD_MOVE), 2) + pow(getAxisWithoutDead(JA_SIDEWARD_MOVE), 2));
|
||||
if (speed > 1.0f)
|
||||
speed = 1.0f;
|
||||
return speed;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue