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
|
@ -2460,7 +2460,7 @@ void Game::updateCameraOrientation(CameraOrientation *cam, float dtime)
|
|||
|
||||
if (m_cache_enable_joysticks) {
|
||||
f32 sens_scale = getSensitivityScaleFactor();
|
||||
f32 c = m_cache_joystick_frustum_sensitivity * (1.f / 32767.f) * dtime * sens_scale;
|
||||
f32 c = m_cache_joystick_frustum_sensitivity * dtime * sens_scale;
|
||||
cam->camera_yaw -= input->joystick.getAxisWithoutDead(JA_FRUSTUM_HORIZONTAL) * c;
|
||||
cam->camera_pitch += input->joystick.getAxisWithoutDead(JA_FRUSTUM_VERTICAL) * c;
|
||||
}
|
||||
|
@ -2471,18 +2471,12 @@ void Game::updateCameraOrientation(CameraOrientation *cam, float dtime)
|
|||
|
||||
void Game::updatePlayerControl(const CameraOrientation &cam)
|
||||
{
|
||||
LocalPlayer *player = client->getEnv().getLocalPlayer();
|
||||
|
||||
//TimeTaker tt("update player control", NULL, PRECISION_NANO);
|
||||
|
||||
// DO NOT use the isKeyDown method for the forward, backward, left, right
|
||||
// buttons, as the code that uses the controls needs to be able to
|
||||
// distinguish between the two in order to know when to use joysticks.
|
||||
|
||||
PlayerControl control(
|
||||
input->isKeyDown(KeyType::FORWARD),
|
||||
input->isKeyDown(KeyType::BACKWARD),
|
||||
input->isKeyDown(KeyType::LEFT),
|
||||
input->isKeyDown(KeyType::RIGHT),
|
||||
isKeyDown(KeyType::JUMP),
|
||||
isKeyDown(KeyType::JUMP) || player->getAutojump(),
|
||||
isKeyDown(KeyType::AUX1),
|
||||
isKeyDown(KeyType::SNEAK),
|
||||
isKeyDown(KeyType::ZOOM),
|
||||
|
@ -2490,22 +2484,16 @@ void Game::updatePlayerControl(const CameraOrientation &cam)
|
|||
isKeyDown(KeyType::PLACE),
|
||||
cam.camera_pitch,
|
||||
cam.camera_yaw,
|
||||
input->joystick.getAxisWithoutDead(JA_SIDEWARD_MOVE),
|
||||
input->joystick.getAxisWithoutDead(JA_FORWARD_MOVE)
|
||||
input->getMovementSpeed(),
|
||||
input->getMovementDirection()
|
||||
);
|
||||
|
||||
u32 keypress_bits = (
|
||||
( (u32)(isKeyDown(KeyType::FORWARD) & 0x1) << 0) |
|
||||
( (u32)(isKeyDown(KeyType::BACKWARD) & 0x1) << 1) |
|
||||
( (u32)(isKeyDown(KeyType::LEFT) & 0x1) << 2) |
|
||||
( (u32)(isKeyDown(KeyType::RIGHT) & 0x1) << 3) |
|
||||
( (u32)(isKeyDown(KeyType::JUMP) & 0x1) << 4) |
|
||||
( (u32)(isKeyDown(KeyType::AUX1) & 0x1) << 5) |
|
||||
( (u32)(isKeyDown(KeyType::SNEAK) & 0x1) << 6) |
|
||||
( (u32)(isKeyDown(KeyType::DIG) & 0x1) << 7) |
|
||||
( (u32)(isKeyDown(KeyType::PLACE) & 0x1) << 8) |
|
||||
( (u32)(isKeyDown(KeyType::ZOOM) & 0x1) << 9)
|
||||
);
|
||||
// autoforward if set: move towards pointed position at maximum speed
|
||||
if (player->getPlayerSettings().continuous_forward &&
|
||||
client->activeObjectsReceived() && !player->isDead()) {
|
||||
control.movement_speed = 1.0f;
|
||||
control.movement_direction = 0.0f;
|
||||
}
|
||||
|
||||
#ifdef ANDROID
|
||||
/* For Android, simulate holding down AUX1 (fast move) if the user has
|
||||
|
@ -2515,23 +2503,38 @@ void Game::updatePlayerControl(const CameraOrientation &cam)
|
|||
*/
|
||||
if (m_cache_hold_aux1) {
|
||||
control.aux1 = control.aux1 ^ true;
|
||||
keypress_bits ^= ((u32)(1U << 5));
|
||||
}
|
||||
#endif
|
||||
|
||||
LocalPlayer *player = client->getEnv().getLocalPlayer();
|
||||
u32 keypress_bits = (
|
||||
( (u32)(control.jump & 0x1) << 4) |
|
||||
( (u32)(control.aux1 & 0x1) << 5) |
|
||||
( (u32)(control.sneak & 0x1) << 6) |
|
||||
( (u32)(control.dig & 0x1) << 7) |
|
||||
( (u32)(control.place & 0x1) << 8) |
|
||||
( (u32)(control.zoom & 0x1) << 9)
|
||||
);
|
||||
|
||||
// autojump if set: simulate "jump" key
|
||||
if (player->getAutojump()) {
|
||||
control.jump = true;
|
||||
keypress_bits |= 1U << 4;
|
||||
}
|
||||
// Set direction keys to ensure mod compatibility
|
||||
if (control.movement_speed > 0.001f) {
|
||||
float absolute_direction;
|
||||
|
||||
// autoforward if set: simulate "up" key
|
||||
if (player->getPlayerSettings().continuous_forward &&
|
||||
client->activeObjectsReceived() && !player->isDead()) {
|
||||
control.up = true;
|
||||
keypress_bits |= 1U << 0;
|
||||
// Check in original orientation (absolute value indicates forward / backward)
|
||||
absolute_direction = abs(control.movement_direction);
|
||||
if (absolute_direction < (3.0f / 8.0f * M_PI))
|
||||
keypress_bits |= (u32)(0x1 << 0); // Forward
|
||||
if (absolute_direction > (5.0f / 8.0f * M_PI))
|
||||
keypress_bits |= (u32)(0x1 << 1); // Backward
|
||||
|
||||
// Rotate entire coordinate system by 90 degrees (absolute value indicates left / right)
|
||||
absolute_direction = control.movement_direction + M_PI_2;
|
||||
if (absolute_direction >= M_PI)
|
||||
absolute_direction -= 2 * M_PI;
|
||||
absolute_direction = abs(absolute_direction);
|
||||
if (absolute_direction < (3.0f / 8.0f * M_PI))
|
||||
keypress_bits |= (u32)(0x1 << 2); // Left
|
||||
if (absolute_direction > (5.0f / 8.0f * M_PI))
|
||||
keypress_bits |= (u32)(0x1 << 3); // Right
|
||||
}
|
||||
|
||||
client->setPlayerControl(control);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue