From 4ebe91150995bed27b33cfb0a965972dccea3d8d Mon Sep 17 00:00:00 2001 From: asofold Date: Sun, 13 Jul 2014 03:19:28 +0200 Subject: [PATCH] Abort ray-tracing if there is no advance. Formatting + numbers. If there is no advance on any axis, loop should be stopped. --- .../nocheatplus/utilities/RayTracing.java | 45 ++++++++++++------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/RayTracing.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/RayTracing.java index f0cbf43e..98947a21 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/RayTracing.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/RayTracing.java @@ -76,7 +76,7 @@ public abstract class RayTracing { private static final double tDiff(final double dTotal, final double offset){ if (dTotal > 0.0){ - return (1 - offset) / dTotal; + return (1.0 - offset) / dTotal; } else if (dTotal < 0.0){ return offset / -dTotal; @@ -101,11 +101,22 @@ public abstract class RayTracing { tY = tDiff(dY, oY); tZ = tDiff(dZ, oZ); tMin = Math.min(tX, Math.min(tY, tZ)); - if (tMin == Double.MAX_VALUE || t + tMin > 1.0) tMin = 1.0 - t; + if (tMin == Double.MAX_VALUE) { + // All differences are 0 (no progress). + break; + } + if (t + tMin > 1.0) { + // Set to the remaining distance (does trigger). + tMin = 1.0 - t; + } // Call step with appropriate arguments. step ++; - if (!step(blockX, blockY, blockZ, oX, oY, oZ, tMin)) break; // || tMin == 0) break; - if (t + tMin >= 1.0 - tol) break; + if (!step(blockX, blockY, blockZ, oX, oY, oZ, tMin)) { + break; // || tMin == 0) break; + } + if (t + tMin >= 1.0 - tol) { + break; + } // Advance (add to t etc.). changed = false; // TODO: Some not handled cases would mean errors. @@ -113,23 +124,23 @@ public abstract class RayTracing { oX += tMin * dX; if (tX == tMin){ if (dX < 0){ - oX = 1; + oX = 1.0; blockX --; changed = true; } else if (dX > 0){ - oX = 0; + oX = 0.0; blockX ++; changed = true; } } - else if (oX >= 1 && dX > 0.0){ - oX -= 1; + else if (oX >= 1.0 && dX > 0.0){ + oX -= 1.0; blockX ++; changed = true; } else if (oX < 0 && dX < 0.0){ - oX += 1; + oX += 1.0; blockX --; changed = true; } @@ -137,23 +148,23 @@ public abstract class RayTracing { oY += tMin * dY; if (tY == tMin){ if (dY < 0){ - oY = 1; + oY = 1.0; blockY --; changed = true; } else if (dY > 0){ - oY = 0; + oY = 0.0; blockY ++; changed = true; } } else if (oY >= 1 && dY > 0.0){ - oY -= 1; + oY -= 1.0; blockY ++; changed = true; } else if (oY < 0 && dY < 0.0){ - oY += 1; + oY += 1.0; blockY --; changed = true; } @@ -161,23 +172,23 @@ public abstract class RayTracing { oZ += tMin * dZ; if (tZ == tMin){ if (dZ < 0){ - oZ = 1; + oZ = 1.0; blockZ --; changed = true; } else if (dZ > 0){ - oZ = 0; + oZ = 0.0; blockZ ++; changed = true; } } else if (oZ >= 1 && dZ > 0.0){ - oZ -= 1; + oZ -= 1.0; blockZ ++; changed = true; } else if (oZ < 0 && dZ < 0.0){ - oZ += 1; + oZ += 1.0; blockZ --; changed = true; }