summaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
authorNeroBurner <pyro4hell@gmail.com>2021-08-27 20:24:24 +0200
committerGitHub <noreply@github.com>2021-08-27 20:24:24 +0200
commit1d69a23ba48d99b051dcf2a6be225edd7c644c7b (patch)
treeddc66759956a61e82092937897465313c65c17a0 /src/client
parent149d8fc8d6d92e8e0d6908125ad8d3179695b1ea (diff)
downloadminetest-1d69a23ba48d99b051dcf2a6be225edd7c644c7b.tar.gz
minetest-1d69a23ba48d99b051dcf2a6be225edd7c644c7b.tar.bz2
minetest-1d69a23ba48d99b051dcf2a6be225edd7c644c7b.zip
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>
Diffstat (limited to 'src/client')
-rw-r--r--src/client/content_cao.cpp7
-rw-r--r--src/client/game.cpp77
-rw-r--r--src/client/inputhandler.h43
-rw-r--r--src/client/joystick_controller.cpp24
-rw-r--r--src/client/joystick_controller.h5
-rw-r--r--src/client/localplayer.cpp24
6 files changed, 115 insertions, 65 deletions
diff --git a/src/client/content_cao.cpp b/src/client/content_cao.cpp
index 83c8e15d4..da78cae7c 100644
--- a/src/client/content_cao.cpp
+++ b/src/client/content_cao.cpp
@@ -998,9 +998,7 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
const PlayerControl &controls = player->getPlayerControl();
bool walking = false;
- if (controls.up || controls.down || controls.left || controls.right ||
- controls.forw_move_joystick_axis != 0.f ||
- controls.sidew_move_joystick_axis != 0.f)
+ if (controls.movement_speed > 0.001f)
walking = true;
f32 new_speed = player->local_animation_speed;
@@ -1015,9 +1013,10 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
g_settings->getBool("free_move") &&
m_client->checkLocalPrivilege("fly"))))
new_speed *= 1.5;
- // slowdown speed if sneeking
+ // slowdown speed if sneaking
if (controls.sneak && walking)
new_speed /= 2;
+ new_speed *= controls.movement_speed;
if (walking && (controls.dig || controls.place)) {
new_anim = player->local_animations[3];
diff --git a/src/client/game.cpp b/src/client/game.cpp
index 011875e4a..18df5cc58 100644
--- a/src/client/game.cpp
+++ b/src/client/game.cpp
@@ -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)
{
- //TimeTaker tt("update player control", NULL, PRECISION_NANO);
+ LocalPlayer *player = client->getEnv().getLocalPlayer();
- // 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.
+ //TimeTaker tt("update player control", NULL, PRECISION_NANO);
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();
-
- // autojump if set: simulate "jump" key
- if (player->getAutojump()) {
- control.jump = true;
- keypress_bits |= 1U << 4;
- }
+ 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)
+ );
- // autoforward if set: simulate "up" key
- if (player->getPlayerSettings().continuous_forward &&
- client->activeObjectsReceived() && !player->isDead()) {
- control.up = true;
- keypress_bits |= 1U << 0;
+ // Set direction keys to ensure mod compatibility
+ if (control.movement_speed > 0.001f) {
+ float absolute_direction;
+
+ // 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);
diff --git a/src/client/inputhandler.h b/src/client/inputhandler.h
index 1fb4cf0ec..76e3c7b5b 100644
--- a/src/client/inputhandler.h
+++ b/src/client/inputhandler.h
@@ -240,6 +240,9 @@ public:
virtual bool wasKeyReleased(GameKeyType k) = 0;
virtual bool cancelPressed() = 0;
+ virtual float getMovementSpeed() = 0;
+ virtual float getMovementDirection() = 0;
+
virtual void clearWasKeyPressed() {}
virtual void clearWasKeyReleased() {}
@@ -285,6 +288,44 @@ public:
{
return m_receiver->WasKeyReleased(keycache.key[k]) || joystick.wasKeyReleased(k);
}
+ virtual float getMovementSpeed()
+ {
+ bool f = m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]),
+ b = m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]),
+ l = m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]),
+ r = m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]);
+ if (f || b || l || r)
+ {
+ // if contradictory keys pressed, stay still
+ if (f && b && l && r)
+ return 0.0f;
+ else if (f && b && !l && !r)
+ return 0.0f;
+ else if (!f && !b && l && r)
+ return 0.0f;
+ return 1.0f; // If there is a keyboard event, assume maximum speed
+ }
+ return joystick.getMovementSpeed();
+ }
+ virtual float getMovementDirection()
+ {
+ float x = 0, z = 0;
+
+ /* Check keyboard for input */
+ if (m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]))
+ z += 1;
+ if (m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]))
+ z -= 1;
+ if (m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]))
+ x += 1;
+ if (m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]))
+ x -= 1;
+
+ if (x != 0 || z != 0) /* If there is a keyboard event, it takes priority */
+ return atan2(x, z);
+ else
+ return joystick.getMovementDirection();
+ }
virtual bool cancelPressed()
{
return wasKeyDown(KeyType::ESC) || m_receiver->WasKeyDown(CancelKey);
@@ -352,6 +393,8 @@ public:
virtual bool wasKeyPressed(GameKeyType k) { return false; }
virtual bool wasKeyReleased(GameKeyType k) { return false; }
virtual bool cancelPressed() { return false; }
+ virtual float getMovementSpeed() {return 0.0f;}
+ virtual float getMovementDirection() {return 0.0f;}
virtual v2s32 getMousePos() { return mousepos; }
virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
diff --git a/src/client/joystick_controller.cpp b/src/client/joystick_controller.cpp
index 919db5315..630565d8d 100644
--- a/src/client/joystick_controller.cpp
+++ b/src/client/joystick_controller.cpp
@@ -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;
}
diff --git a/src/client/joystick_controller.h b/src/client/joystick_controller.h
index 3f361e4ef..cbc60886c 100644
--- a/src/client/joystick_controller.h
+++ b/src/client/joystick_controller.h
@@ -144,7 +144,10 @@ public:
return m_axes_vals[axis];
}
- s16 getAxisWithoutDead(JoystickAxis axis);
+ float getAxisWithoutDead(JoystickAxis axis);
+
+ float getMovementDirection();
+ float getMovementSpeed();
f32 doubling_dtime;
diff --git a/src/client/localplayer.cpp b/src/client/localplayer.cpp
index f3eb1a2dd..2d4f7305a 100644
--- a/src/client/localplayer.cpp
+++ b/src/client/localplayer.cpp
@@ -566,23 +566,7 @@ void LocalPlayer::applyControl(float dtime, Environment *env)
}
}
- if (control.up)
- speedH += v3f(0.0f, 0.0f, 1.0f);
-
- if (control.down)
- speedH -= v3f(0.0f, 0.0f, 1.0f);
-
- if (!control.up && !control.down)
- speedH -= v3f(0.0f, 0.0f, 1.0f) * (control.forw_move_joystick_axis / 32767.f);
-
- if (control.left)
- speedH += v3f(-1.0f, 0.0f, 0.0f);
-
- if (control.right)
- speedH += v3f(1.0f, 0.0f, 0.0f);
-
- if (!control.left && !control.right)
- speedH += v3f(1.0f, 0.0f, 0.0f) * (control.sidew_move_joystick_axis / 32767.f);
+ speedH = v3f(sin(control.movement_direction), 0.0f, cos(control.movement_direction));
if (m_autojump) {
// release autojump after a given time
@@ -639,6 +623,8 @@ void LocalPlayer::applyControl(float dtime, Environment *env)
else
speedH = speedH.normalize() * movement_speed_walk;
+ speedH *= control.movement_speed; /* Apply analog input */
+
// Acceleration increase
f32 incH = 0.0f; // Horizontal (X, Z)
f32 incV = 0.0f; // Vertical (Y)
@@ -1106,9 +1092,7 @@ void LocalPlayer::handleAutojump(f32 dtime, Environment *env,
if (m_autojump)
return;
- bool control_forward = control.up ||
- (!control.up && !control.down &&
- control.forw_move_joystick_axis < -0.05f);
+ bool control_forward = keyPressed & (1 << 0);
bool could_autojump =
m_can_jump && !control.jump && !control.sneak && control_forward;