diff --git a/jme3-core/src/main/java/com/jme3/bounding/BoundingBox.java b/jme3-core/src/main/java/com/jme3/bounding/BoundingBox.java index 11ee8b2ea..06f78154e 100644 --- a/jme3-core/src/main/java/com/jme3/bounding/BoundingBox.java +++ b/jme3-core/src/main/java/com/jme3/bounding/BoundingBox.java @@ -593,18 +593,7 @@ public class BoundingBox extends BoundingVolume { * @see BoundingVolume#intersectsSphere(com.jme3.bounding.BoundingSphere) */ public boolean intersectsSphere(BoundingSphere bs) { - assert Vector3f.isValidVector(center) && Vector3f.isValidVector(bs.center); - - if (FastMath.abs(center.x - bs.center.x) < bs.getRadius() - + xExtent - && FastMath.abs(center.y - bs.center.y) < bs.getRadius() - + yExtent - && FastMath.abs(center.z - bs.center.z) < bs.getRadius() - + zExtent) { - return true; - } - - return false; + return bs.intersectsBoundingBox(this); } /** diff --git a/jme3-core/src/main/java/com/jme3/bounding/BoundingSphere.java b/jme3-core/src/main/java/com/jme3/bounding/BoundingSphere.java index adb763f83..5eecf040a 100644 --- a/jme3-core/src/main/java/com/jme3/bounding/BoundingSphere.java +++ b/jme3-core/src/main/java/com/jme3/bounding/BoundingSphere.java @@ -670,15 +670,7 @@ public class BoundingSphere extends BoundingVolume { * @see com.jme.bounding.BoundingVolume#intersectsSphere(com.jme.bounding.BoundingSphere) */ public boolean intersectsSphere(BoundingSphere bs) { - assert Vector3f.isValidVector(center) && Vector3f.isValidVector(bs.center); - - TempVars vars = TempVars.get(); - - Vector3f diff = center.subtract(bs.center, vars.vect1); - float rsum = getRadius() + bs.getRadius(); - boolean eq = (diff.dot(diff) <= rsum * rsum); - vars.release(); - return eq; + return Intersection.intersect(bs, center, radius); } /* @@ -687,18 +679,7 @@ public class BoundingSphere extends BoundingVolume { * @see com.jme.bounding.BoundingVolume#intersectsBoundingBox(com.jme.bounding.BoundingBox) */ public boolean intersectsBoundingBox(BoundingBox bb) { - assert Vector3f.isValidVector(center) && Vector3f.isValidVector(bb.center); - - if (FastMath.abs(bb.center.x - center.x) < getRadius() - + bb.xExtent - && FastMath.abs(bb.center.y - center.y) < getRadius() - + bb.yExtent - && FastMath.abs(bb.center.z - center.z) < getRadius() - + bb.zExtent) { - return true; - } - - return false; + return Intersection.intersect(bb, center, radius); } /* diff --git a/jme3-core/src/main/java/com/jme3/bounding/Intersection.java b/jme3-core/src/main/java/com/jme3/bounding/Intersection.java index 9deb62abc..c627e23e5 100644 --- a/jme3-core/src/main/java/com/jme3/bounding/Intersection.java +++ b/jme3-core/src/main/java/com/jme3/bounding/Intersection.java @@ -41,10 +41,56 @@ import static java.lang.Math.min; /** * This class includes some utility methods for computing intersection * between bounding volumes and triangles. + * * @author Kirill */ -public class Intersection { +public final class Intersection { + private Intersection() { + } + + public static boolean intersect(BoundingSphere sphere, Vector3f center, float radius) { + assert Vector3f.isValidVector(center) && Vector3f.isValidVector(sphere.center); + + TempVars vars = TempVars.get(); + try { + Vector3f diff = center.subtract(sphere.center, vars.vect1); + float rsum = sphere.getRadius() + radius; + return (diff.dot(diff) <= rsum * rsum); + } finally { + vars.release(); + } + } + + public static boolean intersect(BoundingBox bbox, Vector3f center, float radius) { + assert Vector3f.isValidVector(center) && Vector3f.isValidVector(bbox.center); + + // Arvo's algorithm + float distSqr = radius * radius; + + float minX = bbox.center.x - bbox.xExtent; + float maxX = bbox.center.x + bbox.xExtent; + + float minY = bbox.center.y - bbox.yExtent; + float maxY = bbox.center.y + bbox.yExtent; + + float minZ = bbox.center.z - bbox.zExtent; + float maxZ = bbox.center.z + bbox.zExtent; + + if (center.x < minX) distSqr -= FastMath.sqr(center.x - minX); + else if (center.x > maxX) distSqr -= FastMath.sqr(center.x - maxX); + + + if (center.y < minY) distSqr -= FastMath.sqr(center.y - minY); + else if (center.y > maxY) distSqr -= FastMath.sqr(center.y - maxY); + + + if (center.z < minZ) distSqr -= FastMath.sqr(center.z - minZ); + else if (center.z > maxZ) distSqr -= FastMath.sqr(center.z - maxZ); + + return distSqr > 0; + } + private static final void findMinMax(float x0, float x1, float x2, Vector3f minMax) { minMax.set(x0, x0, 0); if (x1 < minMax.x) {