net.minestom.server.coordinate.PositionUtils Maven / Gradle / Ivy
package net.minestom.server.coordinate;
import net.minestom.server.entity.RelativeFlags;
import org.intellij.lang.annotations.MagicConstant;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
@ApiStatus.Internal
public final class PositionUtils {
public static Pos lookAlong(@NotNull Pos position, double dx, double dy, double dz) {
final float yaw = getLookYaw(dx, dz);
final float pitch = getLookPitch(dx, dy, dz);
return position.withView(yaw, pitch);
}
public static float getLookYaw(double dx, double dz) {
final double radians = Math.atan2(dz, dx);
final float degrees = (float) Math.toDegrees(radians) - 90;
if (degrees < -180) return degrees + 360;
if (degrees > 180) return degrees - 360;
return degrees;
}
public static float getLookPitch(double dx, double dy, double dz) {
final double radians = -Math.atan2(dy, Math.max(Math.abs(dx), Math.abs(dz)));
return (float) Math.toDegrees(radians);
}
public static @NotNull Pos getPositionWithRelativeFlags(@NotNull Pos start, @NotNull Pos modifier, @MagicConstant(flagsFromClass = RelativeFlags.class) int flags) {
final double x = (flags & RelativeFlags.X) == 0 ? modifier.x() : start.x() + modifier.x();
final double y = (flags & RelativeFlags.Y) == 0 ? modifier.y() : start.y() + modifier.y();
final double z = (flags & RelativeFlags.Z) == 0 ? modifier.z() : start.z() + modifier.z();
final float yaw = (flags & RelativeFlags.YAW) == 0 ? modifier.yaw() : start.yaw() + modifier.yaw();
final float pitch = (flags & RelativeFlags.PITCH) == 0 ? modifier.pitch() : start.pitch() + modifier.pitch();
return new Pos(x, y, z, yaw, pitch);
}
}