mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-04 23:47:59 +01:00
Reduce allocation in Navigator movement
This commit is contained in:
parent
f3524d4a4f
commit
e72c87f670
@ -58,12 +58,11 @@ public class Navigator {
|
|||||||
final double speedX = Math.cos(radians) * speed;
|
final double speedX = Math.cos(radians) * speed;
|
||||||
final double speedY = dy * speed;
|
final double speedY = dy * speed;
|
||||||
final double speedZ = Math.sin(radians) * speed;
|
final double speedZ = Math.sin(radians) * speed;
|
||||||
// Update 'position' view
|
final float yaw = PositionUtils.getLookYaw(dx, dz);
|
||||||
final var view = PositionUtils.lookAlong(position, dx, direction.y(), dz);
|
final float pitch = PositionUtils.getLookPitch(dx, direction.y(), dz);
|
||||||
this.entity.setView(view.yaw(), view.pitch());
|
|
||||||
// Prevent ghosting
|
// Prevent ghosting
|
||||||
final var physicsResult = CollisionUtils.handlePhysics(entity, new Vec(speedX, speedY, speedZ));
|
final var physicsResult = CollisionUtils.handlePhysics(entity, new Vec(speedX, speedY, speedZ));
|
||||||
this.entity.refreshPosition(physicsResult.newPosition());
|
this.entity.refreshPosition(physicsResult.newPosition().withView(yaw, pitch));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void jump(float height) {
|
public void jump(float height) {
|
||||||
|
@ -1,13 +1,23 @@
|
|||||||
package net.minestom.server.utils.position;
|
package net.minestom.server.utils.position;
|
||||||
|
|
||||||
import net.minestom.server.coordinate.Pos;
|
import net.minestom.server.coordinate.Pos;
|
||||||
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
@ApiStatus.Internal
|
||||||
public final class PositionUtils {
|
public final class PositionUtils {
|
||||||
public static Pos lookAlong(@NotNull Pos position, double dx, double dy, double dz) {
|
public static Pos lookAlong(@NotNull Pos position, double dx, double dy, double dz) {
|
||||||
final double horizontalAngle = Math.atan2(dz, dx);
|
final float yaw = getLookYaw(dx, dz);
|
||||||
final float yaw = (float) (horizontalAngle * (180.0 / Math.PI)) - 90;
|
final float pitch = getLookPitch(dx, dy, dz);
|
||||||
final float pitch = (float) Math.atan2(dy, Math.max(Math.abs(dx), Math.abs(dz)));
|
|
||||||
return position.withView(yaw, pitch);
|
return position.withView(yaw, pitch);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static float getLookYaw(double dx, double dz) {
|
||||||
|
final double horizontalAngle = Math.atan2(dz, dx);
|
||||||
|
return (float) (horizontalAngle * (180.0 / Math.PI)) - 90;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float getLookPitch(double dx, double dy, double dz) {
|
||||||
|
return (float) Math.atan2(dy, Math.max(Math.abs(dx), Math.abs(dz)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user