From cce7e762116bffeed217439b5a6531387fe8659e Mon Sep 17 00:00:00 2001 From: asofold Date: Fri, 24 Jun 2016 21:22:29 +0200 Subject: [PATCH] The idea of choosing a simple implementation was to skip debugging. --- .../checks/moving/location/LocUtil.java | 11 ++++ .../utilities/collision/CollideRayVsAABB.java | 4 +- .../utilities/collision/CollisionUtil.java | 4 +- .../collision/ICollideRayVsAABB.java | 6 +- .../nocheatplus/TestICollideRayVsAABB.java | 64 +++++++++++++++++++ 5 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 NCPCore/src/test/java/fr/neatmonster/nocheatplus/TestICollideRayVsAABB.java diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/moving/location/LocUtil.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/moving/location/LocUtil.java index f1b213ae..cf1da7bf 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/moving/location/LocUtil.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/moving/location/LocUtil.java @@ -20,6 +20,7 @@ import org.bukkit.block.Block; import fr.neatmonster.nocheatplus.components.location.IGetBlockPosition; import fr.neatmonster.nocheatplus.components.location.IGetLocationWithLook; +import fr.neatmonster.nocheatplus.components.location.IGetPosition; import fr.neatmonster.nocheatplus.components.location.IGetPositionWithLook; import fr.neatmonster.nocheatplus.components.location.ISetPositionWithLook; import fr.neatmonster.nocheatplus.utilities.location.RichBoundsLocation; @@ -365,6 +366,16 @@ public class LocUtil { return "x=" + loc.getX() + ",y=" + loc.getY() + ",z=" + loc.getZ() + ",pitch=" + loc.getPitch() + ",yaw=" + loc.getYaw(); } + /** + * Format like Location.toString, but without extras like the world name. + * + * @param loc + * @return + */ + public static String simpleFormat(final IGetPosition loc) { + return "x=" + loc.getX() + ",y=" + loc.getY() + ",z=" + loc.getZ(); + } + /** * Just the coordinates, no world/yaw/pitch. * @param loc diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/collision/CollideRayVsAABB.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/collision/CollideRayVsAABB.java index da0a45d7..2c76e9a3 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/collision/CollideRayVsAABB.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/collision/CollideRayVsAABB.java @@ -80,6 +80,7 @@ public class CollideRayVsAABB implements ICollideRayVsAABB { final double tMaxX = CollisionUtil.getMaxTimeIncludeEdges(startX, dirX, minX, maxX, tMinX); final double tMaxY = CollisionUtil.getMaxTimeIncludeEdges(startY, dirY, minY, maxY, tMinY); final double tMaxZ = CollisionUtil.getMaxTimeIncludeEdges(startZ, dirZ, minZ, maxZ, tMinZ); + //System.out.println("TIMING: " + tMinX + " " + tMinY + " " + tMinZ + " " + tMaxX + " " + tMaxY + " " + tMaxZ); if (tMaxX != Double.NaN && tMaxY != Double.NaN && tMaxZ != Double.NaN) { // (Excludes any tMin value to be Double.MAX_VALUE.) // Determine if there is overlapping intervals. @@ -90,6 +91,7 @@ public class CollideRayVsAABB implements ICollideRayVsAABB { closestX = startX + dirX * tMin; closestY = startY + dirY * tMin; closestZ = startZ + dirZ * tMin; + closestTime = tMin; } else if (findNearestPointIfNotCollide) { findNearestPoint(tMinX, tMinY, tMinZ, tMaxX, tMaxY, tMaxZ); @@ -122,7 +124,7 @@ public class CollideRayVsAABB implements ICollideRayVsAABB { for (int i = 0; i < timeValues.length; i++) { final double time = timeValues[i]; if (time == Double.NaN || time == Double.POSITIVE_INFINITY) { - // Note that Double.POSITIVE_INFINITY means that we are colliding forever. + // Note that Double.POSITIVE_INFINITY could mean that we are either colliding forever, or never. continue; } final double x = startX + dirX * time; diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/collision/CollisionUtil.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/collision/CollisionUtil.java index 786ba8df..82b85761 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/collision/CollisionUtil.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/collision/CollisionUtil.java @@ -375,10 +375,10 @@ public class CollisionUtil { return Double.NaN; } else if (dir == 0.0) { - return pos < maxPos || pos > maxPos ? Double.NaN : Double.POSITIVE_INFINITY; + return (pos < minPos || pos > maxPos) ? Double.NaN : Double.POSITIVE_INFINITY; } else if (dir < 0.0) { - return pos < minPos ? Double.NaN : (Math.abs(pos - minPos) / dir); + return pos < minPos ? Double.NaN : (Math.abs(pos - minPos) / Math.abs(dir)); } else { // dir > 0.0 diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/collision/ICollideRayVsAABB.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/collision/ICollideRayVsAABB.java index 38204728..987f3b53 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/collision/ICollideRayVsAABB.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/collision/ICollideRayVsAABB.java @@ -127,10 +127,10 @@ public interface ICollideRayVsAABB extends IGetPosition { public boolean collides(); /** - * Get some kind of squared distance for the nearest point, in case of not - * colliding and findNearestPointIfNotCollid being set. + * Get some kind of squared distance from the nearest point towards the + * AABB, in case of not colliding and findNearestPointIfNotCollid being set. * - * @return 0.0 if not applicable. + * @return 0.0 if colliding. */ public double getClosestDistanceSquared(); diff --git a/NCPCore/src/test/java/fr/neatmonster/nocheatplus/TestICollideRayVsAABB.java b/NCPCore/src/test/java/fr/neatmonster/nocheatplus/TestICollideRayVsAABB.java new file mode 100644 index 00000000..ed9fbe2d --- /dev/null +++ b/NCPCore/src/test/java/fr/neatmonster/nocheatplus/TestICollideRayVsAABB.java @@ -0,0 +1,64 @@ +package fr.neatmonster.nocheatplus; + +import static org.junit.Assert.fail; + +import org.junit.Test; + +import fr.neatmonster.nocheatplus.checks.moving.location.LocUtil; +import fr.neatmonster.nocheatplus.utilities.collision.CollideRayVsAABB; +import fr.neatmonster.nocheatplus.utilities.collision.ICollideRayVsAABB; + +public class TestICollideRayVsAABB { + + @Test + public void testBlocks() { + + ICollideRayVsAABB boulder = new CollideRayVsAABB(); + boulder.setFindNearestPointIfNotCollide(true); // Prefer to have the option. + + // Simple x. + if (!boulder.setRay(0.5, 1.75, 0.5, 1.0, 0.0, 0.0) + .setAABB(3, 1, 0, 0.0) + .loop() + .collides()) { + doFail(boulder); + } + if (!boulder.setRay(0.5, 1.75, 0.5, -1.0, 0.0, 0.0) + .setAABB(-3, 1, 0, 0.0) + .loop() + .collides()) { + doFail(boulder); + } + // Simple y. + if (!boulder.setRay(0.5, 1.75, 0.5, 0.0, 1.0, 0.0) + .setAABB(0, 3, 0, 0.0) + .loop() + .collides()) { + doFail(boulder); + } + if (!boulder.setRay(0.5, 1.75, 0.5, 0.0, -1.0, 0.0) + .setAABB(0, -3, 0, 0.0) + .loop() + .collides()) { + doFail(boulder); + } + // Simple z. + if (!boulder.setRay(0.5, 1.75, 0.5, 0.0, 0.0, 1.0) + .setAABB(0, 1, 3, 0.0) + .loop() + .collides()) { + doFail(boulder); + } + if (!boulder.setRay(0.5, 1.75, 0.5, 0.0, 0.0, -1.0) + .setAABB(0, 1, -3, 0.0) + .loop() + .collides()) { + doFail(boulder); + } + } + + private void doFail(ICollideRayVsAABB boulder) { + fail("Failed: collides: " + boulder.collides() + " , distSq: " + boulder.getClosestDistanceSquared() + " , pos: " + LocUtil.simpleFormat(boulder)); + } + +}