Game Development Reference
In-Depth Information
}
}
3. Now that you have handled the key input, put the control Booleans to be used in
the
update
method. You might recognize the code if you've looked at
TestBetterCharacter
. The first thing it does is get the current direction the
spatial
object is facing in order to move forward and backwards. It also
checks which direction is left for strafing, as follows:
public void update(float tpf) {
super.update(tpf);
Vector3f modelForwardDir =
spatial.getWorldRotation().mult(Vector3f.UNIT_Z);
Vector3f modelLeftDir =
spatial.getWorldRotation().mult(Vector3f.UNIT_X);
walkDirection.set(0, 0, 0);
4. Depending on your Booleans, the following code modifies
walkDirection
.
Normally, you would multiply the result by
tpf
as well, but this is already
handled in the
BetterCharacterControl
class as follows:
if (forward) {
walkDirection.addLocal(modelForwardDir.mult(moveSpeed));
} else if (backward) {
walkDirection.addLocal(modelForwardDir.negate().multLocal(moveSpeed));
}
if (leftStrafe) {
walkDirection.addLocal(modelLeftDir.mult(moveSpeed));
} else if (rightStrafe) {
walkDirection.addLocal(modelLeftDir.negate().multLocal(moveSpeed));
}
5. Finally, in the
setWalkDirection
method, apply
walkDirection
as fol-
lows:
BetterCharacterControl.setWalkDirection(walkDirection);