diff --git a/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java b/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java index 06210c388..f1dc9a536 100644 --- a/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java +++ b/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java @@ -434,12 +434,11 @@ public class PhysicsSpace { * @param obj the PhysicsControl or Spatial with PhysicsControl to remove */ public void remove(Object obj) { + if (obj == null) return; if (obj instanceof PhysicsControl) { ((PhysicsControl) obj).setPhysicsSpace(null); } else if (obj instanceof Spatial) { - Spatial node = (Spatial) obj; - PhysicsControl control = node.getControl(PhysicsControl.class); - control.setPhysicsSpace(null); + remove(((Spatial) obj).getControl(PhysicsControl.class)); } else if (obj instanceof PhysicsCollisionObject) { removeCollisionObject((PhysicsCollisionObject) obj); } else if (obj instanceof PhysicsJoint) { @@ -460,34 +459,25 @@ public class PhysicsSpace { } /** - * adds all physics controls and joints in the given spatial node to the - * physics space (e.g. after loading from disk) - recursive if node - * + * adds all physics controls and joints in the given spatial node to the physics space + * (e.g. after loading from disk) - recursive if node * @param spatial the rootnode containing the physics objects */ public void addAll(Spatial spatial) { if (spatial.getControl(RigidBodyControl.class) != null) { RigidBodyControl physicsNode = spatial.getControl(RigidBodyControl.class); - physicsNode.setPhysicsSpace(this); - //add joints + add(physicsNode); + //add joints with physicsNode as BodyA List joints = physicsNode.getJoints(); for (Iterator it1 = joints.iterator(); it1.hasNext();) { PhysicsJoint physicsJoint = it1.next(); - //add connected physicsnodes if they are not already added - if (physicsJoint.getBodyA() instanceof PhysicsControl) { - add(physicsJoint.getBodyA()); - } else { - addRigidBody(physicsJoint.getBodyA()); - } - if (physicsJoint.getBodyA() instanceof PhysicsControl) { - add(physicsJoint.getBodyB()); - } else { - addRigidBody(physicsJoint.getBodyB()); + if (physicsNode.equals(physicsJoint.getBodyA())) { + //add(physicsJoint.getBodyB()); + add(physicsJoint); } - addJoint(physicsJoint); } - } else if (spatial.getControl(PhysicsControl.class) != null) { - spatial.getControl(PhysicsControl.class).setPhysicsSpace(this); + } else { + add(spatial); } //recursion if (spatial instanceof Node) { @@ -500,34 +490,25 @@ public class PhysicsSpace { } /** - * Removes all physics controls and joints in the given spatial from the - * physics space (e.g. before saving to disk) - recursive if node - * + * Removes all physics controls and joints in the given spatial from the physics space + * (e.g. before saving to disk) - recursive if node * @param spatial the rootnode containing the physics objects */ public void removeAll(Spatial spatial) { if (spatial.getControl(RigidBodyControl.class) != null) { RigidBodyControl physicsNode = spatial.getControl(RigidBodyControl.class); - physicsNode.setPhysicsSpace(null); - //remove joints + //remove joints with physicsNode as BodyA List joints = physicsNode.getJoints(); for (Iterator it1 = joints.iterator(); it1.hasNext();) { PhysicsJoint physicsJoint = it1.next(); - //add connected physicsnodes if they are not already added - if (physicsJoint.getBodyA() instanceof PhysicsControl) { - remove(physicsJoint.getBodyA()); - } else { - removeRigidBody(physicsJoint.getBodyA()); - } - if (physicsJoint.getBodyA() instanceof PhysicsControl) { - remove(physicsJoint.getBodyB()); - } else { - removeRigidBody(physicsJoint.getBodyB()); + if (physicsNode.equals(physicsJoint.getBodyA())) { + removeJoint(physicsJoint); + //remove(physicsJoint.getBodyB()); } - removeJoint(physicsJoint); } + remove(physicsNode); } else if (spatial.getControl(PhysicsControl.class) != null) { - spatial.getControl(PhysicsControl.class).setPhysicsSpace(null); + remove(spatial); } //recursion if (spatial instanceof Node) { diff --git a/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java b/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java index 2d48bcb2f..49fda33c5 100644 --- a/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java +++ b/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java @@ -35,10 +35,22 @@ import com.bulletphysics.BulletGlobals; import com.bulletphysics.ContactAddedCallback; import com.bulletphysics.ContactDestroyedCallback; import com.bulletphysics.ContactProcessedCallback; -import com.bulletphysics.collision.broadphase.*; -import com.bulletphysics.collision.dispatch.*; +import com.bulletphysics.collision.broadphase.AxisSweep3; +import com.bulletphysics.collision.broadphase.AxisSweep3_32; +import com.bulletphysics.collision.broadphase.BroadphaseInterface; +import com.bulletphysics.collision.broadphase.BroadphaseProxy; +import com.bulletphysics.collision.broadphase.CollisionFilterGroups; +import com.bulletphysics.collision.broadphase.DbvtBroadphase; +import com.bulletphysics.collision.broadphase.OverlapFilterCallback; +import com.bulletphysics.collision.broadphase.SimpleBroadphase; +import com.bulletphysics.collision.dispatch.CollisionDispatcher; +import com.bulletphysics.collision.dispatch.CollisionObject; +import com.bulletphysics.collision.dispatch.CollisionWorld; import com.bulletphysics.collision.dispatch.CollisionWorld.LocalConvexResult; import com.bulletphysics.collision.dispatch.CollisionWorld.LocalRayResult; +import com.bulletphysics.collision.dispatch.DefaultCollisionConfiguration; +import com.bulletphysics.collision.dispatch.GhostPairCallback; +import com.bulletphysics.collision.dispatch.PairCachingGhostObject; import com.bulletphysics.collision.narrowphase.ManifoldPoint; import com.bulletphysics.collision.shapes.ConvexShape; import com.bulletphysics.dynamics.DiscreteDynamicsWorld; @@ -52,7 +64,13 @@ import com.bulletphysics.dynamics.vehicle.RaycastVehicle; import com.bulletphysics.extras.gimpact.GImpactCollisionAlgorithm; import com.jme3.app.AppTask; import com.jme3.asset.AssetManager; -import com.jme3.bullet.collision.*; +import com.jme3.bullet.collision.PhysicsCollisionEvent; +import com.jme3.bullet.collision.PhysicsCollisionEventFactory; +import com.jme3.bullet.collision.PhysicsCollisionGroupListener; +import com.jme3.bullet.collision.PhysicsCollisionListener; +import com.jme3.bullet.collision.PhysicsCollisionObject; +import com.jme3.bullet.collision.PhysicsRayTestResult; +import com.jme3.bullet.collision.PhysicsSweepTestResult; import com.jme3.bullet.collision.shapes.CollisionShape; import com.jme3.bullet.control.PhysicsControl; import com.jme3.bullet.control.RigidBodyControl; @@ -369,12 +387,11 @@ public class PhysicsSpace { * @param obj the PhysicsControl or Spatial with PhysicsControl to add */ public void add(Object obj) { + if (obj == null) return; if (obj instanceof PhysicsControl) { ((PhysicsControl) obj).setPhysicsSpace(this); } else if (obj instanceof Spatial) { - Spatial node = (Spatial) obj; - PhysicsControl control = node.getControl(PhysicsControl.class); - control.setPhysicsSpace(this); + add(((Spatial) obj).getControl(PhysicsControl.class)); } else if (obj instanceof PhysicsCollisionObject) { addCollisionObject((PhysicsCollisionObject) obj); } else if (obj instanceof PhysicsJoint) { @@ -398,15 +415,15 @@ public class PhysicsSpace { /** * removes an object from the physics space + * * @param obj the PhysicsControl or Spatial with PhysicsControl to remove */ public void remove(Object obj) { + if (obj == null) return; if (obj instanceof PhysicsControl) { ((PhysicsControl) obj).setPhysicsSpace(null); } else if (obj instanceof Spatial) { - Spatial node = (Spatial) obj; - PhysicsControl control = node.getControl(PhysicsControl.class); - control.setPhysicsSpace(null); + remove(((Spatial) obj).getControl(PhysicsControl.class)); } else if (obj instanceof PhysicsCollisionObject) { removeCollisionObject((PhysicsCollisionObject) obj); } else if (obj instanceof PhysicsJoint) { @@ -434,26 +451,18 @@ public class PhysicsSpace { public void addAll(Spatial spatial) { if (spatial.getControl(RigidBodyControl.class) != null) { RigidBodyControl physicsNode = spatial.getControl(RigidBodyControl.class); - physicsNode.setPhysicsSpace(this); - //add joints + add(physicsNode); + //add joints with physicsNode as BodyA List joints = physicsNode.getJoints(); for (Iterator it1 = joints.iterator(); it1.hasNext();) { PhysicsJoint physicsJoint = it1.next(); - //add connected physicsnodes if they are not already added - if (physicsJoint.getBodyA() instanceof PhysicsControl) { - add(physicsJoint.getBodyA()); - } else { - addRigidBody(physicsJoint.getBodyA()); - } - if (physicsJoint.getBodyA() instanceof PhysicsControl) { - add(physicsJoint.getBodyB()); - } else { - addRigidBody(physicsJoint.getBodyB()); + if (physicsNode.equals(physicsJoint.getBodyA())) { + //add(physicsJoint.getBodyB()); + add(physicsJoint); } - addJoint(physicsJoint); } - } else if (spatial.getControl(PhysicsControl.class) != null) { - spatial.getControl(PhysicsControl.class).setPhysicsSpace(this); + } else { + add(spatial); } //recursion if (spatial instanceof Node) { @@ -473,26 +482,18 @@ public class PhysicsSpace { public void removeAll(Spatial spatial) { if (spatial.getControl(RigidBodyControl.class) != null) { RigidBodyControl physicsNode = spatial.getControl(RigidBodyControl.class); - physicsNode.setPhysicsSpace(null); - //remove joints + //remove joints with physicsNode as BodyA List joints = physicsNode.getJoints(); for (Iterator it1 = joints.iterator(); it1.hasNext();) { PhysicsJoint physicsJoint = it1.next(); - //add connected physicsnodes if they are not already added - if (physicsJoint.getBodyA() instanceof PhysicsControl) { - remove(physicsJoint.getBodyA()); - } else { - removeRigidBody(physicsJoint.getBodyA()); - } - if (physicsJoint.getBodyA() instanceof PhysicsControl) { - remove(physicsJoint.getBodyB()); - } else { - removeRigidBody(physicsJoint.getBodyB()); + if (physicsNode.equals(physicsJoint.getBodyA())) { + removeJoint(physicsJoint); + //remove(physicsJoint.getBodyB()); } - removeJoint(physicsJoint); } + remove(physicsNode); } else if (spatial.getControl(PhysicsControl.class) != null) { - spatial.getControl(PhysicsControl.class).setPhysicsSpace(null); + remove(spatial); } //recursion if (spatial instanceof Node) {