Game Development Reference
In-Depth Information
Using the basic camera
Let's now write our class for the basic camera. What have we learned so far? There
are two transformation matrices: the view matrix and the projection matrix. To
calculate the view matrix, we need four parameters: up, direction, left, and position.
These parameters define the camera's orientation and position in space.
To compute the projection matrix, we need four parameters as well: field of view
(radians), aspect ratio of the 2D screen, and the near and far clip planes.
Hence, in our basic camera class, we will need getters/setters for our four parameters
and a function to calculate the view and projection matrices. Although the camera
class in games would need more functions such as pitch and yaw, we will first create
a base class.
Implementing the basic camera
Our base camera class has variables and basic functionality defined, which will be
used in all our different camera implementations. In different types of 3D games, we
generally require three types of cameras: free, target, and orbit.
• A free camera can rotate and move freely in any direction. This camera type
is used in games where you want to look at the whole scene without any
particular context or target. It is mostly used as a first-person camera where
you want to use the camera to view the scene as if the player was viewing it.
• A target camera, though, can move freely in any direction but it will always
face a target. The target is generally defined by a vector. This camera is
generally used as a third-person camera. We want to see the player moving
in the scene; hence, the camera follows the person.
• An orbit camera is a specific type of target camera that can rotate in any
direction around the target and allows for limited rolling.
We have implemented our base class in camera.js present in the primitive
directory. Open the file in your favorite text editor and let's walk through the
code to understand it:
Camera =function ()
{
// Raw Position Values
this.left = vec3.fromValues(1.0, 0.0, 0.0); // Camera Left
vector
this.up = vec3.fromValues(0.0, 1.0, 0.0); // Camera Up vector
this.dir = vec3.fromValues(0.0, 0.0, 1.0); // The direction its
looking at
 
Search WWH ::




Custom Search