@Test public void testSphereSphereCollision() { BoundingSphere sphere1 = new BoundingSphere(1, Vector3f.ZERO); BoundingSphere sphere2 = new BoundingSphere(1, Vector3f.ZERO); checkCollision(sphere1, sphere2, 1); // Put it at the very edge - should still intersect. sphere2.setCenter(new Vector3f(2f, 0f, 0f)); checkCollision(sphere1, sphere2, 1); // Put it a wee bit farther - no intersection expected sphere2.setCenter(new Vector3f(2f + FastMath.ZERO_TOLERANCE, 0, 0)); checkCollision(sphere1, sphere2, 0); }
@Test public void testSphereTriangleCollision() { BoundingSphere sphere = new BoundingSphere(1, Vector3f.ZERO); Geometry geom = new Geometry("geom", new Quad(1, 1)); checkCollision(sphere, geom, 2); // The box touches the edges of the triangles. sphere.setCenter(new Vector3f(-1f + FastMath.ZERO_TOLERANCE, 0, 0)); checkCollision(sphere, geom, 2); // Move it slightly farther.. sphere.setCenter(new Vector3f(-1f - FastMath.ZERO_TOLERANCE, 0, 0)); checkCollision(sphere, geom, 0); // Parallel triangle / box side, touching sphere.setCenter(new Vector3f(0, 0, -1f)); checkCollision(sphere, geom, 2); // Not touching sphere.setCenter(new Vector3f(0, 0, -1f - FastMath.ZERO_TOLERANCE)); checkCollision(sphere, geom, 0); // Test collisions only against one of the triangles sphere.setCenter(new Vector3f(-0.9f, 1.2f, 0f)); checkCollision(sphere, geom, 1); sphere.setCenter(new Vector3f(1.2f, -0.9f, 0f)); checkCollision(sphere, geom, 1); } }
@Test public void testBoxSphereCollision() { BoundingBox box1 = new BoundingBox(Vector3f.ZERO, 1, 1, 1); BoundingSphere sphere2 = new BoundingSphere(1, Vector3f.ZERO); checkCollision(box1, sphere2, 1); // Put it at the very edge - for sphere vs. box, it will not intersect sphere2.setCenter(new Vector3f(2f, 0f, 0f)); checkCollision(box1, sphere2, 0); // Put it a wee bit closer - should intersect. sphere2.setCenter(new Vector3f(2f - FastMath.ZERO_TOLERANCE, 0, 0)); checkCollision(box1, sphere2, 1); // Test if the algorithm converts the sphere // to a box before testing the collision (incorrect) float sqrt3 = FastMath.sqrt(3); sphere2.setCenter(Vector3f.UNIT_XYZ.mult(2)); sphere2.setRadius(sqrt3); checkCollision(box1, sphere2, 0); // Make it a wee bit larger. sphere2.setRadius(sqrt3 + FastMath.ZERO_TOLERANCE); checkCollision(box1, sphere2, 1); }