Game Development Reference
In-Depth Information
10. If we try
AppState
now, we would be able to scroll across the scene with our
keys. We still have mouse controls and rotation to take care of.
11. Let's begin with rotation. We will handle it through a method called
rotate
.
The supplied value is our
rotateSpeed
variable, from which we'll extract a
Quaternion
rotated around the
y
axis. We then multiply the Quaternion with
the camera's rotation as follows:
private void rotate(float value){
Quaternion rotate = new
Quaternion().fromAngleAxis(FastMath.PI * value,
Vector3f.UNIT_Y);
rotate.multLocal(cam.getRotation());
cam.setRotation(rotate);
}
12. Furthermore, we need to make a few alterations to the
update
method. First, we
look to see whether the user has pressed any of the rotation keys and call the
ro-
tate
method:
if(rotateLeft){
rotate(rotateSpeed);
} else if (rotateRight){
rotate(-rotateSpeed);
}
13. The next piece is a bit trickier, and we perform it just above the line where we
multiply
tempVector
by
moveSpeed
(highlighted). We multiply
tem-
pVector
by the camera's rotation to make sure that we get the movement across
the correct axes. Then, since the camera is slightly tilted, we negate any move-
ment along the
y
axis. The best way to understand what would happen is to prob-
ably remove this line and try it out as follows:
cam.getRotation().multLocal(tempVector);
tempVector.multLocal(1, 0, 1).normalizeLocal();
tempVector.multLocal(tpf).multLocal(moveSpeed);
14. That's rotation taken care of! It's pretty common in RTS or top-down games to
scroll by moving the mouse to the extremes of the screen. So, let's add functional-
ities for that. The following code snippet should be added in the
update
method
above the rotation checks: