This documentation is archived and is not being maintained.

How to: Move and Target the Camera

.NET Framework 3.5

You can move and target the camera in a Direct3D scene by using view transformations.


Managed Direct3D mobile applications require Windows Mobile version 5.0 software for Pocket PCs and Smartphones. See External Resources for the .NET Compact Framework for information about Windows Mobile software and SDKs.

View transformations translate Direct3D objects from world space to screen space. When you perform a view transformation, your awareness of the camera (or viewer) position, the camera target, and the coordinate system enables you to manipulate the result on the screen, and ensures that your world space coordinates are transformed correctly into screen space coordinates.

World space is based on a left-handed coordinate system, in which positive y-axis values move up, positive x-axis values move right, and positive z-axis values move away from a hypothetical viewer. Conversely, in a right-handed coordinate system, the positive z-axis values move toward the viewer.

The device’s Transform property returns a Matrix structure that describes a transformation state. To move and target the camera, pass a view transformation matrix to the device object by using the LookAtLH method to handle the transformation.

In the following example, the camera initially begins behind the scene, at a positive point on the z-axis, and targets a point near the origin. Because the camera is located behind the scene, positive z-axis values place objects closer to the camera instead of farther away. In addition, positive x-axis values place objects farther to the left instead of right.

The following code example shows a view transformation for an application that animates a primitive box mesh representing a ship. For the complete example code, see How to: Transform Direct3D Objects.

This code example has several objects, including the following:

  • A primitive Mesh object that represents a ship.

  • A Device object.

// Set up the view matrix. You can define a view matrix with a camera position, 
// a point to look at (camera target), and an "up" direction.
// First vector passed to LookAtLH is the camera position. 
// Second vector passed to LookAtLH is the camera target. 
// Third vector passed to LookAtLH defines the "up" direction.
// Here, you set the camera seven units up along the z-axis ("behind" 
// the scene), down one unit, and left two units. You then point the camera 
// just above the origin and define "up" to be in the y-direction.
if (!isShipDeparted)
    device.Transform.View = Matrix.LookAtLH(new Vector3(-2, -1, 7),
        new Vector3(0, 1, 0), new Vector3(0, 1, 0));
    // Handles movement of camera after  
    // the ship "fires" the main engines.
    device.Transform.View = Matrix.LookAtLH(new Vector3(xCameraPosition, 
        yCameraPosition, 7), new Vector3(0, 1, 0), new Vector3(0, 1, 0));
    xCameraPosition += 0.01f;
    yCameraPosition += 0.01f;

This example requires the complete sample code in How to: Transform Direct3D Objects.