浏览代码

bounding: properly implement collideWith against other bounds

Kirill Vainer 10 年之前
父节点
当前提交
45f8893f13

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

@@ -790,6 +790,7 @@ public class BoundingBox extends BoundingVolume {
         }
     }
     
+    @Override
     public int collideWith(Collidable other, CollisionResults results) {
         if (other instanceof Ray) {
             Ray ray = (Ray) other;
@@ -802,6 +803,13 @@ public class BoundingBox extends BoundingVolume {
                 return 1;
             }
             return 0;
+        } else if (other instanceof BoundingVolume) {
+            if (intersects((BoundingVolume) other)) {
+                CollisionResult r = new CollisionResult();
+                results.addCollision(r);
+                return 1;
+            }
+            return 0;
         } else {
             throw new UnsupportedCollisionException("With: " + other.getClass().getSimpleName());
         }
@@ -818,6 +826,8 @@ public class BoundingBox extends BoundingVolume {
                 return 1;
             }
             return 0;
+        } else if (other instanceof BoundingVolume) {
+            return intersects((BoundingVolume) other) ? 1 : 0;
         } else {
             throw new UnsupportedCollisionException("With: " + other.getClass().getSimpleName());
         }

+ 11 - 1
jme3-core/src/main/java/com/jme3/bounding/BoundingSphere.java

@@ -1013,17 +1013,27 @@ public class BoundingSphere extends BoundingVolume {
         } else if (other instanceof Triangle){
             Triangle t = (Triangle) other;
             return collideWithTri(t, results);
+        } else if (other instanceof BoundingVolume) {
+            if (intersects((BoundingVolume)other)) {
+                CollisionResult result = new CollisionResult();
+                results.addCollision(result);
+                return 1;
+            }
+            return 0;
         } else {
             throw new UnsupportedCollisionException();
         }
     }
 
-    @Override public int collideWith(Collidable other) {
+    @Override
+    public int collideWith(Collidable other) {
         if (other instanceof Ray) {
             Ray ray = (Ray) other;
             return collideWithRay(ray);
         } else if (other instanceof Triangle){
             return super.collideWith(other);
+        } else if (other instanceof BoundingVolume) {
+            return intersects((BoundingVolume)other) ? 1 : 0;
         } else {
             throw new UnsupportedCollisionException();
         }

+ 7 - 6
jme3-core/src/main/java/com/jme3/bounding/BoundingVolume.java

@@ -327,12 +327,13 @@ public abstract class BoundingVolume implements Savable, Cloneable, Collidable {
     
     public int collideWith(Collidable other) {
         TempVars tempVars = TempVars.get();
-        CollisionResults tempResults = tempVars.collisionResults;
-        tempResults.clear();
-        int retval = collideWith(other, tempResults);
-        tempVars.release();
-        return retval;
+        try {
+            CollisionResults tempResults = tempVars.collisionResults;
+            tempResults.clear();
+            return collideWith(other, tempResults);
+        } finally {
+            tempVars.release();
+        }
     }
- 
 }