Browse Source

- add abstraction level for spatial data creation to AbstractPhysicsControl

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10371 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
nor..67 12 years ago
parent
commit
9514355b82

+ 23 - 0
engine/src/bullet-common/com/jme3/bullet/control/AbstractPhysicsControl.java

@@ -58,6 +58,22 @@ public abstract class AbstractPhysicsControl implements PhysicsControl {
     protected PhysicsSpace space = null;
     protected boolean applyLocal = false;
 
+    /**
+     * Called when the control is added to a new spatial, create any
+     * spatial-dependent data here.
+     *
+     * @param spat The new spatial, guaranteed not to be null
+     */
+    protected abstract void createSpatialData(Spatial spat);
+
+    /**
+     * Called when the control is removed from a spatial, remove any
+     * spatial-dependent data here.
+     *
+     * @param spat The old spatial, guaranteed not to be null
+     */
+    protected abstract void removeSpatialData(Spatial spat);
+
     /**
      * Called when the physics object is supposed to move to the spatial
      * position.
@@ -146,10 +162,17 @@ public abstract class AbstractPhysicsControl implements PhysicsControl {
     }
 
     public void setSpatial(Spatial spatial) {
+        if (this.spatial != null && this.spatial != spatial) {
+            removeSpatialData(this.spatial);
+        }
+        else if (this.spatial == spatial) {
+            return;
+        }
         this.spatial = spatial;
         if (spatial == null) {
             return;
         }
+        createSpatialData(this.spatial);
         setPhysicsLocation(getSpatialTranslation());
         setPhysicsRotation(getSpatialRotation());
     }

+ 10 - 12
engine/src/bullet-common/com/jme3/bullet/control/BetterCharacterControl.java

@@ -602,18 +602,6 @@ public class BetterCharacterControl extends AbstractPhysicsControl implements Ph
         location.set(vec);
     }
 
-    /**
-     * We set the current spatial as UserObject so the user can find his
-     * spatial.
-     *
-     * @param spatial
-     */
-    @Override
-    public void setSpatial(Spatial spatial) {
-        super.setSpatial(spatial);
-        rigidBody.setUserObject(spatial);
-    }
-
     /**
      * This is implemented from AbstractPhysicsControl and called when the
      * spatial is attached for example. We don't set the actual physics rotation
@@ -656,6 +644,16 @@ public class BetterCharacterControl extends AbstractPhysicsControl implements Ph
         space.removeTickListener(this);
     }
 
+    @Override
+    protected void createSpatialData(Spatial spat) {
+        rigidBody.setUserObject(spatial);
+    }
+
+    @Override
+    protected void removeSpatialData(Spatial spat) {
+        rigidBody.setUserObject(null);
+    }
+    
     public Control cloneForSpatial(Spatial spatial) {
         BetterCharacterControl control = new BetterCharacterControl(radius, height, mass);
         control.setJumpForce(jumpForce);

+ 16 - 10
engine/src/bullet-common/com/jme3/bullet/control/KinematicRagdollControl.java

@@ -102,7 +102,7 @@ public class KinematicRagdollControl extends AbstractPhysicsControl implements P
     protected final Map<String, PhysicsBoneLink> boneLinks = new HashMap<String, PhysicsBoneLink>();
     protected final Vector3f modelPosition = new Vector3f();
     protected final Quaternion modelRotation = new Quaternion();
-    protected PhysicsRigidBody baseRigidBody;
+    protected final PhysicsRigidBody baseRigidBody;
     protected Spatial targetModel;
     protected Skeleton skeleton;
     protected RagdollPreset preset = new HumanoidRagdollPreset();
@@ -145,18 +145,23 @@ public class KinematicRagdollControl extends AbstractPhysicsControl implements P
      * contruct a KinematicRagdollControl
      */
     public KinematicRagdollControl() {
+        baseRigidBody = new PhysicsRigidBody(new BoxCollisionShape(Vector3f.UNIT_XYZ.mult(0.1f)), 1);
+        baseRigidBody.setKinematic(mode == Mode.Kinematic);
     }
 
     public KinematicRagdollControl(float weightThreshold) {
+        this();
         this.weightThreshold = weightThreshold;
     }
 
     public KinematicRagdollControl(RagdollPreset preset, float weightThreshold) {
+        this();
         this.preset = preset;
         this.weightThreshold = weightThreshold;
     }
 
     public KinematicRagdollControl(RagdollPreset preset) {
+        this();
         this.preset = preset;
     }
 
@@ -307,16 +312,10 @@ public class KinematicRagdollControl extends AbstractPhysicsControl implements P
     }
 
     @Override
-    public void setSpatial(Spatial model) {
-        super.setSpatial(model);
+    protected void createSpatialData(Spatial model) {
         if (added) {
             removePhysics(space);
         }
-        boneLinks.clear();
-        baseRigidBody = null;
-        if (model == null) {
-            return;
-        }
         targetModel = model;
         Node parent = model.getParent();
 
@@ -355,6 +354,15 @@ public class KinematicRagdollControl extends AbstractPhysicsControl implements P
         logger.log(Level.FINE, "Created physics ragdoll for skeleton {0}", skeleton);
     }
 
+    @Override
+    protected void removeSpatialData(Spatial spat) {
+        if (added) {
+            removePhysics(space);
+            added = false;
+        }
+        boneLinks.clear();
+    }
+
     /**
      * Add a bone name to this control Using this method you can specify which
      * bones of the skeleton will be used to build the collision shapes.
@@ -378,8 +386,6 @@ public class KinematicRagdollControl extends AbstractPhysicsControl implements P
             Bone childBone = skeleton.getRoots()[i];
             if (childBone.getParent() == null) {
                 logger.log(Level.FINE, "Found root bone in skeleton {0}", skeleton);
-                baseRigidBody = new PhysicsRigidBody(new BoxCollisionShape(Vector3f.UNIT_XYZ.mult(0.1f)), 1);
-                baseRigidBody.setKinematic(mode == Mode.Kinematic);
                 boneRecursion(model, childBone, baseRigidBody, 1, pointsMap);
             }
         }