Search
Multimedia

Programming with DirectX : World Transformations

Published 2 min readBy Programming4us Editorial Team
In modern video games many different 3D objects can be found throughout a scene. Some of these objects are dynamic, such as character models and vehicles, while others are static. Dynamic objects are objects that move either by player control, game physics, or artificial control. Static objects cannot move at all. A static object can be a building, the terrain, or some other object that the designer does not mean to be dynamic.

When objects are created in 3D modeling and animation applications, they are saved out and imported by the game. The vertex positions of these objects are not necessarily the positions they need to be in the game. For example, if a box is created around the origin (0, 0, 0) and that box needs to be in the second story of a virtual building, unless that position just happens to be around the origin, the data needs to be altered. If complex models are created and need to be rendered more than once—for example, having the same box appear 50 times in a level—then it would be inefficient to create the exact same geometry but at different positions again and again so that the boxes load in the correct spots in a game. To further complicate things, dynamic objects need to be moved on the fly. When it comes to character models, how and where these objects move are unpredictable.

The purpose of a world matrix transformation is to position and rotate objects in a 3D scene. This allows characters to move through the world, allows objects to be rendered at multiple locations, and so on. The world matrix represents an object’s position in the game world. This means objects can be created in a modeling package such as 3D Studio Max and moved, rotated, and scaled as necessary so that they appear how the designer requires in a game. This is often done in a level editor, where objects can be positioned, rotated, and scaled. A visual of using world positions to place objects is shown in Figure 1.

Figure 1. Different world positions for the same object.


Scaling refers to changing an object’s size from its original size. To scale a matrix you call the function D3DXMatrixScaling(), which has the following prototype.

D3DXMATRIX * D3DXMatrixScaling(
D3DXMATRIX *pOut,
FLOAT sx,
FLOAT sy,
FLOAT sz
);

The D3DXMatrixScaling() function parameters are the matrix that is being scaled and the X, Y, and Z axis factors. To set the position, also known as translation, of a matrix, you use the function D3DXMatrixTranslation(). The function takes the matrix and the positions X, Y, and Z value of the scaling factors. The function prototype for the D3DXMatrixTranslation() can be seen as follows.

D3DXMATRIX* D3DXMatrixTranslation(
D3DXMATRIX *pOut,
FLOAT x,
FLOAT y,
FLOAT z
);