Table Of Contents

Previous topic

21.8. The Canvas Object

Next topic

21.11. The DebuggingTools Object

This Page

21.10. The CharacterController Object

Implements a physics based FPS like character with keyboard, mouse and joypad input.

The physics based model of the character is created using a character shape supplied by the physics device with a height provided at construction time. The character also supports a crouching property which reduces the height of the character shape to a given crouch height. The CharacterController also supports a ‘god’ mode which is treated as a fly camera for development purposes.

Note

The character shape is implemented via a multisphere shape see the Character Object.

Required scripts

The CharacterController object requires:

/*{{ javascript("jslib/charactercontroller.js") }}*/

It also requires that a MathDevice has been created before calling the CharacterController constructor.

21.10.1. Constructor

21.10.1.1. create

Summary

Creates the a character shape physics representation given a set of predefined parameters such as height and crouching height.

Syntax

var parameters = {
        radius: 30,
        halfHeight: 64,
        crouchHalfHeight: 45,
        rotateSpeed: 2,
        mouseRotateFactor: 2,
        collisionMargin: 1,
        maxSpeed: 120,
        maxStepHeight: 5,
        maxJumpHeight: 30
    };
var characterController = CharacterController.create(graphicsDevice,
                                                     inputDevice,
                                                     physicsDevice,
                                                     dynamicsWorld,
                                                     matrix,
                                                     parameters);
matrix
A m43 matrix representing the position and orientation of the character.

The shape representing the character for the PhysicsDevice is a capsule with radius radius. The character controller is initialized in the scene using given transformation matrix. The character controller also registers hooks with the InputDevice supporting input from the keyboard, mouse and gamepads. The rotateSpeed, mouseRotateFactor, maxSpeed and maxJumpHeight control the limits of how the input devices will affect the character. The maxStepHeight represents the largest increase in height the character will treat as a climbable step, i.e. the maximum height which doesn’t require jumping to traverse.

21.10.2. Method

21.10.2.1. update

Summary

Applies any input device data captured since the last call to update to the physics based character. Update also applies any velocity limits and determines whether the character is jumping or crouching and also whether the character should currently be treated as on the ground or not. At the end of all physics and input based calculations update will also recalculate the extents of the character useful for checking interaction with things like trigger volumes.

Syntax

characterController.update(deltaTime);

21.10.2.2. setPosition

Summary

Allows the character controller to be repositioned in the scene. This method takes into account states like crouching to correctly set the character controller matrix and position of the physics representation of the character.

Syntax

characterController.setPosition(position);
position
A 3D Vector representing the new position of the character.

21.10.2.3. setDead

Summary

Allows the character to be treated as dead or alive. When dead a characters height will be reduced to twice the characters radius, and the character will no longer be moved via any of the input device hooks. The boolean sets whether to make the character dead or not.

Syntax

characterController.setDead(true);

21.10.3. Properties

21.10.3.1. version

Summary

The version number of the CharacterController implementation.

Syntax

var versionNumber = characterController.version;

21.10.3.2. god

Summary

Whether the CharacterController is currently in ‘god’ mode.

Syntax

if (characterController.god)
{
    // Remove from AI target lists
}

21.10.3.3. jumped

Summary

Whether the CharacterController processed a jump request in the last update. This is useful to be able to detect when a player jumps to play a oneshot sound as the player initiates a jump or process other jump related events.

Syntax

if (characterController.jumped)
{
    PlaySound(jumpSoundEffect);
}

21.10.3.4. crouch

Summary

Whether the CharacterController is currently in crouch mode.

Syntax

var aimingStability = 0.5;
if (characterController.crouch)
{
    aimingStability *= 2;
}

21.10.3.5. walkDirection

Summary

What direction is the character currently walking in, i.e. character orientation. Could be used to determine if the character is moving in the direction of the goal.

Syntax

var characterDirection = characterController.walkDirection;
if (mathDevice.v3Dot(characterDirection, directionToGoal) < 0)
{
    SendPlayerMessage("Heading in wrong direction");
}