Walking Character

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.

Sample Code

The full code sample can be found here:

Overview of this Physics Application

  1. Create a SimpleApplication with a BulletAppState
    • This gives us a Physics Space
  2. Load any physical game level model, terrain, or floor
  3. Load an animated character model
  4. Set up animation channel and controller
  5. Add a CharacterControl to the model

Creating the Character

  1. Initialze physical character behaviour, including collision shape
    1. Create CapsuleCollisionShape of the right size for the model.
      • A Capsule is a cylinder with rounded top and bottom: A good collision shape for a character since it reduces the risk of getting stuck on obstacles.
    2. Create a CharacterControl from the collision shape
  2. Load the visible character model, and add the physical behaviour to it
    1. Load an animated model (e.g. "Models/Oto/Oto.mesh.xml").
    2. Add the CharacterControl to the character model.
  3. Make character visible and physical
    1. Attach the model to the rootNode to make it appear in the scene.
    2. Add the CharacterControl to the PhysicsSpace to make it physical.
// 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

Setting Up the Animation Controller

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.

Walking

Work in progress (this is being updated for the new physics and chase cam.)

view online version