Switch from CharacterControl to Kinematic Ragdoll

In my game i would like to implement, on death, that physics bound characters fall to ragdolls. Everything works but there is some lag in the transition from charactercontrol to kinematicragdoll control. The app will run around 400 fps on average hardware but when the transition happens it drops to 15 for a brief moment. Does anyone know a better way to do this?



[java]

public class AI extends AbstractAppState implements AnimEventListener, PhysicsCollisionListener {



private SimpleApplication app;

private BulletAppState bullet;

private AppStateManager stateManager;

private AssetManager asset;

private Node root;



private KinematicRagdollControl ragdoll;

private CharacterControl physicsCharacter;

private Node Model;



@Override

public void initialize(AppStateManager manager, Application app) {

super.initialize(manager, app);

this.app = (SimpleApplication) app;

root = this.app.getRootNode();

asset = this.app.getAssetManager();

stateManager = this.app.getStateManager();

bullet = this.stateManager.getState(BulletAppState.class);

}



public void setUpSinBad(Node charModel) {

Model = charModel;



physicsCharacter = new CharacterControl(new CapsuleCollisionShape(1.5f, 6f, 1), 1f);

ragdoll = new KinematicRagdollControl(.5f);

Model.addControl(physicsCharacter);



AnimControl control = Model.getControl(AnimControl.class);

SkeletonDebugger skeletonDebug = new SkeletonDebugger("skeleton", control.getSkeleton());

Material mat2 = new Material(asset, "Common/MatDefs/Misc/Unshaded.j3md");

mat2.getAdditionalRenderState().setWireframe(true);

mat2.setColor("Color", ColorRGBA.Green);

mat2.getAdditionalRenderState().setDepthTest(false);

skeletonDebug.setMaterial(mat2);

skeletonDebug.setLocalTranslation(Model.getLocalTranslation());

setupSinbad(ragdoll);

float eighth_pi = FastMath.PI * 0.125f;

ragdoll.setJointLimit("Waist", eighth_pi, eighth_pi, eighth_pi, eighth_pi, eighth_pi, eighth_pi);

ragdoll.setJointLimit("Chest", eighth_pi, eighth_pi, 0, 0, eighth_pi, eighth_pi);

ragdoll.setEnabled(false);



physicsCharacter.setJumpSpeed(40);

physicsCharacter.setFallSpeed(30);

physicsCharacter.setGravity(50);

physicsCharacter.setPhysicsLocation(new Vector3f(0, 20, 0));

physicsCharacter.setEnabled(true);



root.attachChild(Model);

bullet.getPhysicsSpace().add(Model);

bullet.getPhysicsSpace().add(physicsCharacter);

bullet.getPhysicsSpace().add(ragdoll);



bullet.getPhysicsSpace().addCollisionListener(this);



}



private void setupSinbad(KinematicRagdollControl ragdoll) {



ragdoll.addBoneName("Ulna.L");

ragdoll.addBoneName("Ulna.R");

ragdoll.addBoneName("Chest");

ragdoll.addBoneName("Foot.L");

ragdoll.addBoneName("Foot.R");

ragdoll.addBoneName("Hand.R");

ragdoll.addBoneName("Hand.L");

ragdoll.addBoneName("Neck");

ragdoll.addBoneName("Root");

ragdoll.addBoneName("Stomach");

ragdoll.addBoneName("Waist");

ragdoll.addBoneName("Humerus.L");

ragdoll.addBoneName("Humerus.R");

ragdoll.addBoneName("Thigh.L");

ragdoll.addBoneName("Thigh.R");

ragdoll.addBoneName("Calf.L");

ragdoll.addBoneName("Calf.R");

ragdoll.addBoneName("Clavicle.L");

ragdoll.addBoneName("Clavicle.R");



}



public void collision(PhysicsCollisionEvent event) {



if (Model.equals(event.getNodeA()) || Model.equals(event.getNodeB())) {

if ("bullet".equals(event.getNodeA().getName()) || "bullet".equals(event.getNodeB().getName())) {



if (physicsCharacter.isEnabled()) {



physicsCharacter.setEnabled(false);

Model.removeControl(physicsCharacter);



Model.addControl(ragdoll);

ragdoll.setEnabled(true);

ragdoll.setRagdollMode();



}



}

}

}

}



[/java]

Add the control on the model during initialization, and just enable/disable it on switching.

Some initialization is done when you attach the model to the control, basically computing all the collision shapes according to the model mesh.

This is probably what slows down your app.

That’s how i initially had done it, add ragdoll and character control and disabled ragdoll, but for some reason it would bring the performance down to under 10 fps as if ragdoll and physics character, even tho one was disabled, were interfering with each other.

I found out why having both controls before didn’t work for me, you cannot disable something before the Physicsspace.add() call. So i have it where both Charactercontrol and ragdollcontrol are on the model and it only disables character and enables ragdoll on collision. It has about the same amount of lag. I notice in the output screen that the skeleton binds to the mesh again on collision and i’m sure that’s what causes the lag. Why would that be?