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()
experimental
David Bernard 10 years ago
parent 42ae669c6d
commit 3c72c065ad
  1. 57
      jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java
  2. 1
      jme3-jbullet/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 * @param obj the PhysicsControl or Spatial with PhysicsControl to remove
*/ */
public void remove(Object obj) { public void remove(Object obj) {
if (obj == null) return;
if (obj instanceof PhysicsControl) { if (obj instanceof PhysicsControl) {
((PhysicsControl) obj).setPhysicsSpace(null); ((PhysicsControl) obj).setPhysicsSpace(null);
} else if (obj instanceof Spatial) { } else if (obj instanceof Spatial) {
Spatial node = (Spatial) obj; remove(((Spatial) obj).getControl(PhysicsControl.class));
PhysicsControl control = node.getControl(PhysicsControl.class);
control.setPhysicsSpace(null);
} else if (obj instanceof PhysicsCollisionObject) { } else if (obj instanceof PhysicsCollisionObject) {
removeCollisionObject((PhysicsCollisionObject) obj); removeCollisionObject((PhysicsCollisionObject) obj);
} else if (obj instanceof PhysicsJoint) { } 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 * adds all physics controls and joints in the given spatial node to the physics space
* physics space (e.g. after loading from disk) - recursive if node * (e.g. after loading from disk) - recursive if node
*
* @param spatial the rootnode containing the physics objects * @param spatial the rootnode containing the physics objects
*/ */
public void addAll(Spatial spatial) { public void addAll(Spatial spatial) {
if (spatial.getControl(RigidBodyControl.class) != null) { if (spatial.getControl(RigidBodyControl.class) != null) {
RigidBodyControl physicsNode = spatial.getControl(RigidBodyControl.class); RigidBodyControl physicsNode = spatial.getControl(RigidBodyControl.class);
physicsNode.setPhysicsSpace(this); add(physicsNode);
//add joints //add joints with physicsNode as BodyA
List<PhysicsJoint> joints = physicsNode.getJoints(); List<PhysicsJoint> joints = physicsNode.getJoints();
for (Iterator<PhysicsJoint> it1 = joints.iterator(); it1.hasNext();) { for (Iterator<PhysicsJoint> it1 = joints.iterator(); it1.hasNext();) {
PhysicsJoint physicsJoint = it1.next(); PhysicsJoint physicsJoint = it1.next();
//add connected physicsnodes if they are not already added if (physicsNode.equals(physicsJoint.getBodyA())) {
if (physicsJoint.getBodyA() instanceof PhysicsControl) { //add(physicsJoint.getBodyB());
add(physicsJoint.getBodyA()); add(physicsJoint);
} else {
addRigidBody(physicsJoint.getBodyA());
}
if (physicsJoint.getBodyA() instanceof PhysicsControl) {
add(physicsJoint.getBodyB());
} else {
addRigidBody(physicsJoint.getBodyB());
} }
addJoint(physicsJoint);
} }
} else if (spatial.getControl(PhysicsControl.class) != null) { } else {
spatial.getControl(PhysicsControl.class).setPhysicsSpace(this); add(spatial);
} }
//recursion //recursion
if (spatial instanceof Node) { if (spatial instanceof Node) {
@ -500,34 +490,25 @@ public class PhysicsSpace {
} }
/** /**
* Removes all physics controls and joints in the given spatial from the * Removes all physics controls and joints in the given spatial from the physics space
* physics space (e.g. before saving to disk) - recursive if node * (e.g. before saving to disk) - recursive if node
*
* @param spatial the rootnode containing the physics objects * @param spatial the rootnode containing the physics objects
*/ */
public void removeAll(Spatial spatial) { public void removeAll(Spatial spatial) {
if (spatial.getControl(RigidBodyControl.class) != null) { if (spatial.getControl(RigidBodyControl.class) != null) {
RigidBodyControl physicsNode = spatial.getControl(RigidBodyControl.class); RigidBodyControl physicsNode = spatial.getControl(RigidBodyControl.class);
physicsNode.setPhysicsSpace(null); //remove joints with physicsNode as BodyA
//remove joints
List<PhysicsJoint> joints = physicsNode.getJoints(); List<PhysicsJoint> joints = physicsNode.getJoints();
for (Iterator<PhysicsJoint> it1 = joints.iterator(); it1.hasNext();) { for (Iterator<PhysicsJoint> it1 = joints.iterator(); it1.hasNext();) {
PhysicsJoint physicsJoint = it1.next(); PhysicsJoint physicsJoint = it1.next();
//add connected physicsnodes if they are not already added if (physicsNode.equals(physicsJoint.getBodyA())) {
if (physicsJoint.getBodyA() instanceof PhysicsControl) { removeJoint(physicsJoint);
remove(physicsJoint.getBodyA()); //remove(physicsJoint.getBodyB());
} else {
removeRigidBody(physicsJoint.getBodyA());
}
if (physicsJoint.getBodyA() instanceof PhysicsControl) {
remove(physicsJoint.getBodyB());
} else {
removeRigidBody(physicsJoint.getBodyB());
} }
removeJoint(physicsJoint);
} }
remove(physicsNode);
} else if (spatial.getControl(PhysicsControl.class) != null) { } else if (spatial.getControl(PhysicsControl.class) != null) {
spatial.getControl(PhysicsControl.class).setPhysicsSpace(null); remove(spatial);
} }
//recursion //recursion
if (spatial instanceof Node) { if (spatial instanceof Node) {

@ -415,6 +415,7 @@ public class PhysicsSpace {
/** /**
* removes an object from the physics space * removes an object from the physics space
*
* @param obj the PhysicsControl or Spatial with PhysicsControl to remove * @param obj the PhysicsControl or Spatial with PhysicsControl to remove
*/ */
public void remove(Object obj) { public void remove(Object obj) {

Loading…
Cancel
Save