Game Development Reference
In-Depth Information
The other three attributes define the position and orientation of the camera in our world. We will
construct a matrix from this as outlined in Chapter 10.
We also want to be able to move the camera in the direction that it is heading. For this, we need
a unit-length direction vector, and we can add this to the position vector of the camera. We can
create this type of a vector with the help of the
Matrix
class that the Android API offers us. Let's
think about this for a moment.
In its default configuration, our camera will look down the negative z axis, giving it a direction
vector of (0,0,-1). When we specify a yaw or pitch angle, this direction vector will be rotated
accordingly. To figure out the direction vector, we just need to multiply it with a matrix that will
rotate the default direction vector, just as OpenGL ES will rotate the vertices of our models.
Let's have a look at how all this works in code. Listing 11-9 shows the
EulerCamera
class.
Listing 11-9. EulerCamera.java, a Simple First-Person Camera Based on Euler Angles Around the x and y Axes
package
com.badlogic.androidgames.framework.gl;
import
javax.microedition.khronos.opengles.GL10;
import
android.opengl.GLU;
import
android.opengl.Matrix;
import
com.badlogic.androidgames.framework.math.Vector3;
public class
EulerCamera {
final
Vector3 position =
new
Vector3();
float
yaw;
float
pitch;
float
fieldOfView;
float
aspectRatio;
float
near;
float
far;
The first three members hold the position and rotation angles of the camera. The other four
members hold the parameters used for calculating the perspective projection matrix. By default,
our camera is located at the origin of the world, looking down the negative z axis.
public
EulerCamera(
float
fieldOfView,
float
aspectRatio,
float
near,
float
far){
this
.fieldOfView = fieldOfView;
this
.aspectRatio = aspectRatio;
this
.near = near;
this
.far = far;
}
The constructor takes four parameters that define the perspective projection. We leave the
camera position and rotation angles as they are.
public
Vector3 getPosition() {
return
position;
}