programming4us
programming4us
MULTIMEDIA

DirectX 10 Game Programming : 3D Primer - Matrix Rotation, Multiply Matrices

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
1/12/2013 3:28:41 AM

1. Matrix Rotation

The final effect I’m going to describe is rotation. Rotation is the act of turning an object around a certain axis. This allows objects to change their orientation within space. For instance, if you wanted to rotate a planet, you would create a rotation matrix and apply it to the planet.

Rotation matrices take a little more explanation because the matrix needs to change based on the axis you’re trying to rotate around.

Rotate around the X Axis

When rotating around any axis, it will require you to convert the angle of rotation into both a sine and cosine value. These values are then plugged into the matrix to cause the proper amount of rotation.

When rotating around the X axis, the sine and cosine values are placed into the matrix in the following manner.

float RotateXMatrix [4][4] = {
    1.0f, 0.0f,       0.0f,     0.0f,
    0.0f, cosAngle,   sinAngle, 0.0f,
    0.0f, -sinAngle,  cosAngle, 0.0f,
    0.0f, 0.0f,       0.0f,     1.0f
};

The Direct3D function D3DXMatrixRotationX will create a rotation matrix along the X axis.

D3DXMATRIX * D3DXMatrixRotationX(
  D3DXMATRIX * pOut,
  FLOAT Angle
);

Rotate around the Y Axis

Rotating around the Y axis (see Figure 1) requires only that the sine and cosine values be moved to different positions within the matrix. In this instance, the values are moved to affect the X and Z axes.

Figure 1. A cube rotated 45 degrees around the Y axis.


float RotateYMatrix [4][4] = {
    cosAngle, 0.0f, -sinAngle, 0.0f,
    0.0f,     1.0f,  0.0f,      0.0f,
    sinAngle, 0.0f, cosAngle,  0.0f,
    0.0f,     0.0f,  0.0f,      1.0f
};

The D3DXMatrixRotationY function will generate a rotation matrix around the Y axis.

D3DXMATRIX * D3DXMatrixRotationY(
  D3DXMATRIX * pOut,
  FLOAT Angle
);

Rotate around the Z Axis

The final rotation matrix allows for rotation around the Z axis. In this instance, the sin and cosine values affect the X and Y axes.

float RotateZMatrix [4][4] = {
    cosAngle,  sinAngle,  0.0f, 0.0f,
    -sinAngle, cosAngle,  0.0f, 0.0f,
    0.0f,      0.0f,      1.0f, 0.0f,
    0.0f,      0.0f,      0.0f, 1.0f
};

The D3DXMatrixRotationZ function will generate a rotation matrix around the Z axis.

D3DXMATRIX * D3DXMatrixRotationZ(
  D3DXMATRIX * pOut,
  FLOAT Angle
);

2. Multiply Matrices

The last thing you need to learn about matrices is how to combine them. Most of the time, you won’t need to only translate an object, you’ll need to scale it or maybe rotate it all in one go. Multiple matrices can be multiplied, or concatenated, together to create a single matrix that is capable of containing the previously single calculations. This means that instead of needing a matrix for rotation as well as one for translation, these two can be combined into one.

Because the D3DXMatrix type overrides the multiplication operator, the act of multiplying two D3DXMatrix objects together is easy. The following single line of code demonstrates this.

D3DXMatrix finalMatrix = rotationMatrix * translationMatrix;

A new matrix called finalMatrix is created, which now contains both the rotation and translation. This new matrix can then be applied to any object going through the pipeline. One thing to mention though is to watch out for the order in which you multiply matrices. Using the previous line of code, objects will be rotated first and then translated away from that position. For example, rotating the object 90 degrees around the Y axis and then translating it four units along the X axis would cause the object to be rotated in place at the origin and then moved four units to the right.

If the translation came first, the object would be translated four units to the right of the origin first and then rotated 90 degrees. Think what would happen if you extended your hand to your right and then turned your body 90 degrees—where would your hand end up? Not exactly in the same place as before; you need to make sure that the order in which you multiply the matrices is the order you really want them applied.

If you don’t like using the multiplication operator to combine matrices, you can use the Direct3D function D3DXMatrixMultiply instead.

D3DXMATRIX * D3DXMatrixMultiply(
  D3DXMATRIX * pOut,
  CONST D3DXMATRIX *pM1,
  CONST D3DXMATRIX *pM2
);

The D3DXMatrixMultiply function takes three parameters. The first parameter is a pointer to the output matrix to be created. The second and third parameters are the matrices you want to multiply. Using this function may make your code slightly easier to follow.

Note

Matrix multiplication is not cumulative. Multiplying Matrix A by Matrix B does not result in the same output matrix as multiplying Matrix B by Matrix A. The order in which matrices are multiplied is important.

Other  
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
programming4us
 
 
programming4us