From 9514355b8258203de550006803e77b2cc67bf980 Mon Sep 17 00:00:00 2001 From: "nor..67" Date: Sat, 9 Feb 2013 09:24:04 +0000 Subject: [PATCH] - add abstraction level for spatial data creation to AbstractPhysicsControl git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10371 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../control/AbstractPhysicsControl.java | 23 ++++++++++++++++ .../control/BetterCharacterControl.java | 22 +++++++--------- .../control/KinematicRagdollControl.java | 26 ++++++++++++------- 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/engine/src/bullet-common/com/jme3/bullet/control/AbstractPhysicsControl.java b/engine/src/bullet-common/com/jme3/bullet/control/AbstractPhysicsControl.java index bf2976144..71526d0a6 100644 --- a/engine/src/bullet-common/com/jme3/bullet/control/AbstractPhysicsControl.java +++ b/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()); } diff --git a/engine/src/bullet-common/com/jme3/bullet/control/BetterCharacterControl.java b/engine/src/bullet-common/com/jme3/bullet/control/BetterCharacterControl.java index 35fe0f311..beca4f321 100644 --- a/engine/src/bullet-common/com/jme3/bullet/control/BetterCharacterControl.java +++ b/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); diff --git a/engine/src/bullet-common/com/jme3/bullet/control/KinematicRagdollControl.java b/engine/src/bullet-common/com/jme3/bullet/control/KinematicRagdollControl.java index 63ddf10a7..b87c69a96 100644 --- a/engine/src/bullet-common/com/jme3/bullet/control/KinematicRagdollControl.java +++ b/engine/src/bullet-common/com/jme3/bullet/control/KinematicRagdollControl.java @@ -102,7 +102,7 @@ public class KinematicRagdollControl extends AbstractPhysicsControl implements P protected final Map boneLinks = new HashMap(); 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); } }