Work in progress.
In other code samples we have seen how to create collidable landscapes and walk around in a first-person perspective, by enclosing the camera with a collision shape.
Many games however require a third-person perspective of the character. If you load a character model, create a PhysicsControl for it, and use forces to push it around, you may not get the desired effect: Phyical objects often fall over when pushed, and that is not what you expect of a walking character.
This is why jME3 offers a special CharacterControl to implement walking characters.
The full code sample can be found here:
// initialze physical character behaviour, including collision shape CapsuleCollisionShape capsule = new CapsuleCollisionShape(3f, 4f); CharacterControl character = new CharacterControl(capsule, 0.01f); // load the visible character model and add the physical behaviour to it Node model = (Node) assetManager.loadModel("Models/Oto/Oto.mesh.xml"); model.addControl(character); // Make character visible and physical rootNode.attachChild(model); // make it visible getPhysicsSpace().add(character); // make it physical
We create two AninChannels, for example one for walking, one for shooting. The shootingChannel only controls one arm, while the walking channels controls the whole animation.
AnimControl animationControl = model.getControl(AnimControl.class); animationControl.addListener(this); AnimChannel animationChannel = animationControl.createChannel(); AnimChannel shootingChannel = animationControl.createChannel(); shootingChannel.addBone(animationControl.getSkeleton().getBone("uparm.right")); shootingChannel.addBone(animationControl.getSkeleton().getBone("arm.right")); shootingChannel.addBone(animationControl.getSkeleton().getBone("hand.right"));
The extra shooting channel exists so the character can lift an arm to shoot and walk at the same time.
Work in progress (this is being updated for the new physics and chase cam.)