From 3c72c065ad4a2dbfcad6b0ea452eb8640513f208 Mon Sep 17 00:00:00 2001 From: David Bernard Date: Fri, 15 Aug 2014 13:42:26 +0200 Subject: [PATCH] bullet: refactor PhysiscSpace.addAll/removeAll - only add joint with current PhysicNode is BodyA - => avoid logging warning - => fix a previous bug in test about getBodyA when adding/removing BodyB - remove optimisation that by-pass routing made by add()/remove() --- .../java/com/jme3/bullet/PhysicsSpace.java | 57 +++++++------------ .../java/com/jme3/bullet/PhysicsSpace.java | 1 + 2 files changed, 20 insertions(+), 38 deletions(-) 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 769ad554b..49fda33c5 100644 --- a/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java +++ b/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java @@ -415,6 +415,7 @@ 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) {