Skip to content

Commit

Permalink
PlayerUtils distance math
Browse files Browse the repository at this point in the history
  • Loading branch information
RacoonDog authored and arlomcwalter committed Feb 2, 2023
1 parent 8df696f commit 77d2df1
Showing 1 changed file with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,14 @@ public static double squaredDistanceTo(BlockPos blockPos) {
}

public static double squaredDistanceTo(double x, double y, double z) {
float f = (float) (mc.player.getX() - x);
float g = (float) (mc.player.getY() - y);
float h = (float) (mc.player.getZ() - z);
return f * f + g * g + h * h;
return squaredDistance(mc.player.getX(), mc.player.getY(), mc.player.getZ(), x, y, z);
}

public static double squaredDistance(double x1, double y1, double z1, double x2, double y2, double z2) {
float f = (float) (x1 - x2);
float g = (float) (y1 - y2);
float h = (float) (z1 - z2);
return org.joml.Math.fma(f, f, org.joml.Math.fma(g, g, h * h));
}

public static boolean isWithin(Entity entity, double r) {
Expand Down Expand Up @@ -287,7 +291,8 @@ public static double distanceToCamera(Entity entity) {
}

public static double squaredDistanceToCamera(double x, double y, double z) {
return mc.gameRenderer.getCamera().getPos().squaredDistanceTo(x, y, z);
Vec3d cameraPos = mc.gameRenderer.getCamera().getPos();
return squaredDistance(cameraPos.x, cameraPos.y, cameraPos.z, x, y, z);
}

public static double squaredDistanceToCamera(Entity entity) {
Expand Down Expand Up @@ -323,10 +328,7 @@ public static boolean isWithinReach(BlockPos blockPos) {
}

public static boolean isWithinReach(double x, double y, double z) {
float f = (float) (mc.player.getX() - x);
float g = (float) (mc.player.getEyeY() - y);
float h = (float) (mc.player.getZ() - z);
return (f * f + g * g + h * h) <= mc.interactionManager.getReachDistance() * mc.interactionManager.getReachDistance();
return squaredDistanceTo(x, y, z) <= mc.interactionManager.getReachDistance() * mc.interactionManager.getReachDistance();
}

public static Dimension getDimension() {
Expand Down

0 comments on commit 77d2df1

Please sign in to comment.