Game Development Reference
In-Depth Information
break;
}
}
6. Let's have a look at the
update
method, where we will put these Booleans to
use. An
update
method is called automatically every frame, and we also get to
know how much time (in seconds) has passed since the last update, in
tpf
. We
start by storing the camera's current location and initialize a
Vector3f
object,
which we'll use for our movement delta as follows:
public void update(float tpf) {
super.update(tpf);
camLocation = cam.getLocation();
Vector3f tempVector = new Vector3f();
7. Next, we look to see if any of our
movement
Booleans are
true
and apply this
to
tempVector
as follows:
if(moveUp){
tempVector.addLocal(0, 0, 1f);
} else if(moveDown){
tempVector.addLocal(0, 0, -1f);
}
if(moveLeft){
tempVector.addLocal(1f, 0, 0);
} else if (moveRight){
tempVector.addLocal(-1f, 0, 0);
}
8. To keep the movement speed constant, regardless of the frame rate, we multiply
tempVector
by the
tpf
, and then we also multiply it by our
moveSpeed
variable. Then, we add it to
camLocation
as follows:
tempVector.multLocal(tpf).multLocal(moveSpeed);
camLocation.addLocal(tempVector);
9. At the end of the method, we set the camera's location to the modified stored loc-
ation as follows:
cam.setLocation(camLocation);