Procházet zdrojové kódy

bounding: add bound vs. spatial, also improve unit test

Kirill Vainer před 10 roky
rodič
revize
f32d92ef30

+ 3 - 0
jme3-core/src/main/java/com/jme3/bounding/BoundingBox.java

@@ -41,6 +41,7 @@ import com.jme3.export.JmeImporter;
 import com.jme3.export.OutputCapsule;
 import com.jme3.math.*;
 import com.jme3.scene.Mesh;
+import com.jme3.scene.Spatial;
 import com.jme3.util.TempVars;
 import java.io.IOException;
 import java.nio.FloatBuffer;
@@ -799,6 +800,8 @@ public class BoundingBox extends BoundingVolume {
                 return 1;
             }
             return 0;
+        } else if (other instanceof Spatial) {
+            return ((Spatial)other).collideWith(this, results);
         } else {
             throw new UnsupportedCollisionException("With: " + other.getClass().getSimpleName());
         }

+ 3 - 0
jme3-core/src/main/java/com/jme3/bounding/BoundingSphere.java

@@ -38,6 +38,7 @@ import com.jme3.collision.UnsupportedCollisionException;
 import com.jme3.export.JmeExporter;
 import com.jme3.export.JmeImporter;
 import com.jme3.math.*;
+import com.jme3.scene.Spatial;
 import com.jme3.util.BufferUtils;
 import com.jme3.util.TempVars;
 import java.io.IOException;
@@ -1001,6 +1002,8 @@ public class BoundingSphere extends BoundingVolume {
                 return 1;
             }
             return 0;
+        } else if (other instanceof Spatial) {
+            return ((Spatial)other).collideWith(this, results);
         } else {
             throw new UnsupportedCollisionException();
         }

+ 37 - 0
jme3-core/src/test/java/com/jme3/collision/BoundingCollisionTest.java

@@ -162,5 +162,42 @@ public class BoundingCollisionTest {
         // Not touching
         box.setCenter(new Vector3f(0, 0, -1f - FastMath.ZERO_TOLERANCE));
         checkCollision(box, geom, 0);
+        
+        // Test collisions only against one of the triangles
+        box.setCenter(new Vector3f(-1f, 1.5f, 0f));
+        checkCollision(box, geom, 1);
+        
+        box.setCenter(new Vector3f(1.5f, -1f, 0f));
+        checkCollision(box, geom, 1);
+    }
+    
+    @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);
     }
 }