Game Development Reference
In-Depth Information
9. In the constructor, set the location of the head node. The location is relative to
the spatial object to an appropriate amount. In a normally scaled world, 1.8f
would correspond to 1.8 m (or about 6 feet):
head.setLocalTranslation(0, 1.8f, 0);
10. Next, you need to attach the head node to spatial . You can do this in the
setSpatial method. When a spatial is supplied, first check whether it is a
Node (or you wouldn't be able to add the head). If it is, attach the head as fol-
lows:
public void setSpatial(Spatial spatial) {
super.setSpatial(spatial);
if(spatial instanceof Node){
((Node)spatial).attachChild(head);
}
}
11. Now that you have a head that can rotate freely, you can implement the method
that handles looking up and down. Modify the yaw field with the supplied value.
Then, clamp it so that it can't be rotated more than 90 degrees up or down. Not
doing this might lead to weird results. Then, set the rotation for the head around
the x axis (think ear-to-ear) as follows:
private void lookUpDown(float value){
yaw += value;
yaw = FastMath.clamp(yaw, -FastMath.HALF_PI,
FastMath.HALF_PI);
head.setLocalRotation(new
Quaternion().fromAngles(yaw, 0, 0));
}
12. Now, we have a character that can move and rotate like a standard FPS character.
It still doesn't have a camera tied to it. To solve this, we're going to use the Cam-
eraNode class and hijack the application's camera. CameraNode gives you the
ability to control the camera as if it were a node. With setControlDir , we in-
struct it to use the location and rotation of spatial as follows:
public void setCamera(Camera cam){
CameraNode camNode = new CameraNode("CamNode", cam);
Search WWH ::




Custom Search