Simple Camera Movement

This example demonstrates how to rotate and move the camera by creating a view matrix with CreateLookAt.

The camera's position and orientation are controlled by setting a view Matrix for the camera to use. In this example, it is assumed the camera will move frequently, so the view Matrix is created and set every time Game.Draw() is called. Similarly, it is assumed that the projection Matrix may change from frame to frame for effects such as zooming. This will be a free camera is restricted to one plane. It will be able to yaw(rotate) and pan(side-to-side) and move forward/back. Later we will develop a free camera will move in the 3rd dimension.

 

Initial Setup of a free camera

A camera is like any other game entity, in that it has a position, orientation (up/down etc) and, usually, limits on its rate of movement.

In principle a camera should have complete freedom of movement, that is it can move along any axis or rotate about any axis.

In three dimensions, the six degrees of freedom (DOFs) of a rigid body are sometimes described using these nautical names:

  1. Moving up and down (heaving);
  2. Moving left and right (swaying);
  3. Moving forward and backward (surging);
  4. Tilting forward and backward (pitching);
  5. Turning left and right (yawing);
  6. Tilting side to side (rolling).

but in practice it can be better in some games for user experience to restrict motion to a subset of possible DOFs. In this example there will be 3 DOFs;

  1. Moving left and right (swaying);
  2. Moving forward and backward (surging);
  3. Turning left and right (yawing);

In order to control this motion our camera will need these three properties ;

  1. position
  2. vertical axis (about which to yaw) [normalised]
  3. forward axis (along which to surge)[normalised]

Note: the swaying/panning motion will be along a vector at right angles to the vertical and forward axes.

In addition to these vectors, a camera will typically have maximum velocity and rates of rotaton

Surging (forward/backward motion)

Moving a camera forward is simple ($t$ is time since last update);

$\vec{position}=\vec{position}+\vec{dir}.forward\_speed.t$

Similarly backwards is

$\vec{position}=\vec{position}-\vec{dir}.forward\_speed.t$

Swaying (panning left/right)

Again this is a linear movement along a vector so is implemented as a vector displacement. this time the displacement vector is at right angles to the current position and vertical vectors;

$\begin{eqnarray*} \vec{right}&=&\vec{dir}\times\vec{up}\\ \vec{position}&=&\vec{position}+\vec{right}.sideways\_speed.t \end{eqnarray*}$

Yawing (rotating left/right)

Finally for this camera, we need to yaw. to do this, the direction vector will be rotated by a suitable rotation matrix;

$$\begin{eqnarray*} \mathbf{M_r}&=&Matrix(\textrm{rotate about }\vec{up} )\\ \vec{dir}&=&\mathbf{M_r}\vec{dir} \end{eqnarray*}$$ 

Free Camera class

To build a camera class, the methods needed are

Camera.Init(...) to initialise position, up and direction vectors among other things.

Camera.Update(gametime) to modify the camera's properties based on keyboard input.

Camera.View() to construct and return a vew matrix based on the current camera state

Camera.Project() to construct and return a projection matrix based on the current camera state

 

1st & 2nd Person Camera

A 1st person camera persents a view from the "eyes" of the player's game character. Usually the avatar is not rendered, except for maybe the hands or weapon. The camera is fixed at the position of the character's head and the camera's' motion follows the motion of the game character..

First person view from COD MW3

An over-the-shoulder camera, sometimes called a 2nd person view refers to a graphical perspective rendered from a fixed distance behind and slightly above the player character. (This is sometimes incorrectly? refered to as a third person view).

The 2nd person camera gives a better view of the characters immediate surroundings and of the avatar.

2nd Person View

 

The implementation of the 1st and 2nd person cameras is very similar the main difference being the position of the camera and the rendering of the avatar. A free camera can be converted into a 1st/2nd person camera by linking it's position to the game character's position and adding a cameraOffset vector to position the camera relative to the character. This cameraOffset vector is effectively a bone and should rotate with the character.

 
Rotate cameraOffset by player.yaw;
cameraPosition=player.position+cameraOffset;
cameraDirection=player.direction;

A simple 1st/2nd person camera can be very jerky and unpleasnt to use for long periods. A solution is to introduce some lag in to the camera movement. This has the effect of camera responding a little slowly to the character's movement.

Lag can be implemented by not turning the camera by the required player.Yaw, rather we calculate the difference between the required yaw and the current yaw (actualYaw)and we turn the camera a fraction of this value. The effect is that the camera takes a little while to match the speed of the character when it starts to rotate and the camera continues to move after the character has rotating in order to catch up.

A similar mehtod can be used fro lateral movement also.