Game Development Reference
In-Depth Information
public float
getYaw() {
return
yaw;
}
public float
getPitch() {
return
pitch;
}
The getter methods just return the camera orientation and position.
public void
setAngles(
float
yaw,
float
pitch) {
if
(pitch < -90)
pitch = -90;
if
(pitch > 90)
pitch = 90;
this
.yaw = yaw;
this
.pitch = pitch;
}
public void
rotate(
float
yawInc,
float
pitchInc) {
this
.yaw += yawInc;
this
.pitch += pitchInc;
if
(pitch < -90)
pitch = -90;
if
(pitch > 90)
pitch = 90;
}
The
setAngles()
method allows us to specify the yaw and pitch of the camera directly. Note that
we limit the pitch to be in the range of -90 to 90. We can't rotate our own head further than that,
so our camera shouldn't be able to do that either.
The
rotate()
method is nearly identical to the
setAngles()
method. Instead of setting the
angles, it increases them by the parameters. This will be useful when we implement a little
touchscreen-based control scheme in the next example.
public void
setMatrices(GL10 gl) {
gl.glMatrixMode(GL10.
GL_PROJECTION
);
gl.glLoadIdentity();
GLU.
gluPerspective
(gl, fieldOfView, aspectRatio, near, far);
gl.glMatrixMode(GL10.
GL_MODELVIEW
);
gl.glLoadIdentity();
gl.glRotatef(-pitch, 1, 0, 0);
gl.glRotatef(-yaw, 0, 1, 0);
gl.glTranslatef(-position.x, -position.y, -position.z);
}
The
setMatrices()
method just sets the projection and model-view matrices as discussed
earlier. The projection matrix is set via
gluPerspective()
based on the parameters given to the
camera in the constructor. The model-view matrix performs the world-moving trick we alked
about in Chapter 10 by applying a rotation around the x and y axes as well as a translation. All
involved factors are negated to achieve the effect where the camera remains at the origin of the