Click or drag to resize
DigitalRuneHow To: Compute World and View Matrix using Pose

This section explains how a Pose can be used to define a World or a View transformation matrix.

This topic contains the following sections:

Add DigitalRune namespaces

The following example will use data types and helper methods from DigitalRune Geometry and Mathematics. The following namespaces must be imported for these examples at the beginning of the source code files:

C#
using DigitalRune.Geometry;
using DigitalRune.Mathematics.Algebra;
World matrix

When a model is created in a 3D modeler, it is modeled relative to the origin. That is, the vertices of the model are defined in local space (also known as model space or object space).

In a game the model needs to be positioned in the world. In XNA you could implement something like this:

C#
// A visual object in a game.
class GameObject
{
  // The XNA Model that represents the object.
  public Model Model { get; set; }

  // The position and orientation of the model in world space.
  public Pose Pose { get; set; }

  ...
}

Pose defines the transformation that converts the Model from local space to world space. When rendering the object the pose can be directly assigned to the world matrix of the Effect used for rendering. (Remember: A pose can be cast to an XNA Matrix.)

C#
basicEffect.World = gameObject.Pose;
View matrix

Similarly a camera in an XNA game could be defined like this:

C#
// A camera in a game.
class Camera
{
  // The position and orientation of the camera in world space.
  public Pose Pose { get; set; }

  // The projection matrix.
  public Matrix44F Projection { get; set; }

  ...
}

Now, to render objects we need the view transformation matrix. The view matrix transforms an object from world space into view space. The view space is the local space of the camera – the camera is in the origin and all objects are relative to the camera.

How is the view matrix related to Camera.Pose? Camera.Pose transforms the camera from its local space to world space. The view matrix transforms objects from world space to the local space of that camera. Therefore: Camera.Pose is the inverse of the view matrix!

To render an object you need to assign the inverse of Pose to the Effect used for rendering:

C#
basicEffect.View = camera.Pose.Inverse;
Controlling a camera

In many games the player is able to control the camera directly. If the player presses "forward", the camera should move forward along the viewing direction.

How can we compute the forward direction? In the local space of the camera the x-axis points to the right, the y-axis points up and the z-axis points towards the viewer. Hence "forward" is (0, 0, –1). To move a camera forward in the world we need to transform this direction into world space.

C#
Vector3F forward = camera.Pose.ToWorldDirection(Vector3F.Forward);

That’s it! Now we can move the camera along the viewing direction.

C#
// Move the camera 10 units in the viewing direction.
Pose pose = camera.Pose;
pose.Position += 10 * forward;
camera.Pose = pose;
See Also