Preserve speed when moving diagonally

The original algorithm squares both X and Y separately, which cause the movement speed to drop by up to half when not moving perfectly along the axes. The new algorithm converts the XY speed vector into a speed and a moving angle vector and only square the speed instead. This prevents the slowdown issue present in the original algorithm.
This commit is contained in:
dogtopus
2022-10-12 18:55:53 -03:00
parent f5d5d93c19
commit c70da21df6

View File

@@ -40,6 +40,7 @@ public final class MovementHandler implements PressAction {
private boolean pressingBack = false; private boolean pressingBack = false;
private boolean pressingLeft = false; private boolean pressingLeft = false;
private boolean pressingRight = false; private boolean pressingRight = false;
private float slowdownFactor = 1.f;
private float movementForward = 0.f; private float movementForward = 0.f;
private float movementSideways = 0.f; private float movementSideways = 0.f;
@@ -52,14 +53,21 @@ public final class MovementHandler implements PressAction {
* @param player The client player. * @param player The client player.
*/ */
public void applyMovement(@NotNull ClientPlayerEntity player) { public void applyMovement(@NotNull ClientPlayerEntity player) {
double movementR, movementTheta;
if (!this.shouldOverrideMovement) if (!this.shouldOverrideMovement)
return; return;
player.input.pressingForward = this.pressingForward; player.input.pressingForward = this.pressingForward;
player.input.pressingBack = this.pressingBack; player.input.pressingBack = this.pressingBack;
player.input.pressingLeft = this.pressingLeft; player.input.pressingLeft = this.pressingLeft;
player.input.pressingRight = this.pressingRight; player.input.pressingRight = this.pressingRight;
player.input.movementForward = this.movementForward;
player.input.movementSideways = this.movementSideways; // Do a implicit square here
movementR = this.slowdownFactor * (Math.pow(this.movementSideways, 2) + Math.pow(this.movementForward, 2));
movementTheta = Math.atan2(this.movementForward, this.movementSideways);
player.input.movementForward = (float) (movementR * Math.sin(movementTheta));
player.input.movementSideways = (float) (movementR * Math.cos(movementTheta));
this.shouldOverrideMovement = false; this.shouldOverrideMovement = false;
} }
@@ -79,30 +87,26 @@ public final class MovementHandler implements PressAction {
this.shouldOverrideMovement = direction != 0; this.shouldOverrideMovement = direction != 0;
if (MidnightControlsConfig.analogMovement) { if (!MidnightControlsConfig.analogMovement) {
value = (float) Math.pow(value, 2); value = 1.f;
} else value = 1.f; }
this.slowdownFactor = client.player.shouldSlowDown() ? (MathHelper.clamp(
0.3F + EnchantmentHelper.getSwiftSneakSpeedBoost(client.player),
0.0F,
1.0F
)) : 1.f;
if (button == ButtonBinding.FORWARD || button == ButtonBinding.BACK) { if (button == ButtonBinding.FORWARD || button == ButtonBinding.BACK) {
// Handle forward movement. // Handle forward movement.
this.pressingForward = direction > 0; this.pressingForward = direction > 0;
this.pressingBack = direction < 0; this.pressingBack = direction < 0;
this.movementForward = direction * value; this.movementForward = direction * value;
// Slowing down if sneaking or crawling.
if (client.player.shouldSlowDown()) {
this.movementForward *= MathHelper.clamp(0.3F + EnchantmentHelper.getSwiftSneakSpeedBoost(client.player), 0.0F, 1.0F);
}
} else { } else {
// Handle sideways movement. // Handle sideways movement.
this.pressingLeft = direction > 0; this.pressingLeft = direction > 0;
this.pressingRight = direction < 0; this.pressingRight = direction < 0;
this.movementSideways = direction * value; this.movementSideways = direction * value;
// Slowing down if sneaking or crawling.
if (client.player.shouldSlowDown()) {
this.movementSideways *= MathHelper.clamp(0.3F + EnchantmentHelper.getSwiftSneakSpeedBoost(client.player), 0.0F, 1.0F);
}
} }
return this.shouldOverrideMovement; return this.shouldOverrideMovement;