How To: Rotate and Move a Camera
This sample demonstrates how to rotate and move a first-person camera in a 3D environment. You can rotate the camera about its y-axis, and move it forward and backward. You control the camera's position and orientation by using the directional keys on your keyboard or by using the D-pad of your Xbox 360 gamepad.
|This sample is the base for a more advanced sample that implements three camera types: first-person, first-person (offset), and third-person. For more information about this sample, see How To: Make a Third-Person Camera.|
The Complete Sample
The code in this tutorial illustrates the technique described in the text. A complete code sample for this tutorial is available for you to download, including full source code and any additional supporting files required by the sample.
This sample is based on several assumptions.
- The camera will move frequently, so the camera view Matrix is created and set every time Game.Update is called.
- The projection Matrix may also change frequently for effects such as zooming.
- You have added a model to the project as described in How To: Render a Model.
For the sake of simplicity, the sample limits the camera object to an axis of rotation (y-axis) and forward and backward movement. This simplifies the process (and resultant code) for rendering the sample 3D scene. The following procedure outlines the steps for rendering the sample scene.
- Determine the location and orientation of the camera object.
- Create a view matrix (as part of the standard world-view-projection matrix trilogy of 3D rendering) using the camera position, orientation (also referred to as the "look at point"), and the world space's up vector. This is accomplished with a call to CreateLookAt.
- Create a perspective matrix that determines the near and far clipping planes and the aspect of the projection with a call to CreatePerspectiveFieldOfView.
- In the Draw method of your game, initialize a BasicEffect object with the transformational matrices created earlier (world, view projection) and then render all existing 3D models.
The following text discusses this procedure in greater detail, with related code samples.
To rotate and move the camera
- Determine the camera's position in world coordinates.
Determine the reference Vector3 that rotation of the camera is relative to. The direction should not change during the game and will usually be (0, 0, 1) or (0, 0, −1).
Create a rotation Matrix determining the current amount of rotation for the camera object. Because the camera is limited to one axis of rotation, this matrix represents the rotation of the camera around its own y-axis. A rotation Matrix representing rotation around the y-axis is created with CreateRotationY.
Add the camera's current position to the transformed direction vector. The result is the position that the camera is looking at.
Create a new view Matrix using CreateLookAt. Pass CreateLookAt the camera's current position and the transformed direction vector. The third parameter of CreateLookAt is the up direction of the camera and is usually Vector3.Up (0, 1, 0).
This matrix Matrix controls how world coordinates are transformed to camera coordinates.
The first parameter is the field of view of the projection Matrix expressed in radians. A typical field of view of 45 degrees would be expressed as pi/4 radians. The second parameter is the aspect ratio of the projection Matrix and corrects for the difference in width and height of a viewspace. The third and fourth parameters specify the near and far distances at which the objects will be visible.
// Set distance from the camera of the near and far clipping planes. static float nearClip = 1.0f; static float farClip = 2000.0f; Viewport viewport = graphics.GraphicsDevice.Viewport; float aspectRatio = (float)viewport.Width / (float)viewport.Height; proj = Matrix.CreatePerspectiveFieldOfView(viewAngle, aspectRatio, nearClip, farClip);
Loop through each 3D model to be rendered as described in How To: Render a Model using the projection matrix and view matrix created above. An identity matrix is used for the world matrix as a means of simplifying the code.