Game Development Reference
In-Depth Information
6. The preceding code handles moving forward, backward, and to the side. The turn-
ing and looking up and down actions of a character is normally handled by mov-
ing the mouse (or game controller), which is instead an analog input. This is
handled by the
onAnalog
method. From here, we take the name of the input and
apply its value to two new methods,
rotate
and
lookUpDown
, as follows:
public void onAnalog(String name, float value, float
tpf) {
if (name.equals("RotateLeft")) {
rotate(tpf * value * sensitivity);
} else if (name.equals("RotateRight")) {
rotate(-tpf * value * sensitivity);
} else if(name.equals("LookUp")){
lookUpDown(value * tpf * sensitivity);
} else if (name.equals("LookDown")){
lookUpDown(-value * tpf * sensitivity);
}
}
7. Now, start by handling the process of turning the character left and right. The
BetterCharacterControl
class already has nice support for turning the
character (which, in this case, is the same thing as looking left or right), and you
can access its
viewDirection
field directly. You should only modify the
y
ax-
is, which is the axis that goes from head to toe, by a small amount as follows:
private void rotate(float value){
Quaternion rotate = new
Quaternion().fromAngleAxis(FastMath.PI * value,
Vector3f.UNIT_Y);
rotate.multLocal(viewDirection);
setViewDirection(viewDirection);
}
8. In order to handle looking up and down, you have to do some more work. The
idea is to let the
spatial
object handle this. For this, you need to step back to
the top of the class and add two more fields: a
Node
field called
head
and a float
field called
yaw
. The
yaw
field will be the value with which you will control the
rotation of the head up and down.