home | login | register | DMCA | contacts | help | donate |   A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
А Б В Г Д Е Ж З И Й К Л М Н О П Р С Т У Ф Х Ц Ч Ш Щ Э Ю Я

 fantasy space fantasy fantasy is horrors heroic prose   military   child   russian detective   action   child   ironical   historical   political western adventure adventure (child) child's stories love religion antique Scientific literature biography business home pets animals art history computers linguistics mathematics religion home_garden sport technique publicism philosophy chemistry close

## How do I use transformation matrices in DirectX?

In the code for this tutorial, we will have 5 cubes in different positions all rotating differently. Here is a walkthtough of the code and how it works.

Step 1: Create objects

The first thing we need to do is create and define our five cubes. I have defined 5 cubes as member variables and modified the InitialiseGame method of CGame to create and set their centre position. Their default size is 10x10x10 which I haven't changed. The InitialiseGame method is now as follows:

`bool CGame::InitialiseGame() {`

` //Setup games objects here`

` m_pCube1 = new CCuboid(m_pD3DDevice);`

` m_pCube1->SetPosition(-27.0, 0.0, 0.0);`

` m_pCube2 = new CCuboid(m_pD3DDevice);`

` m_pCube2->SetPosition(-9.0, 0.0, 0.0);`

` m_pCube3 = new CCuboid(m_pD3DDevice);`

` m_pCube3->SetPosition(9.0, 0.0, 0.0);`

` m_pCube4 = new CCuboid(m_pD3DDevice);`

` m_pCube4->SetPosition(27.0, 0.0, 0.0);`

` m_pCube5 = new CCuboid(m_pD3DDevice);`

` m_pCube5->SetPosition(0.0, 15.0, 0.0);`

` return true;`

`}`

Fig 5.4 below shows the initial positions of our five cubes. We would normally create all of these objects at the origin, and the translate them. But for the purpose of this tutorial, we'll create them in the positions show below. The centre coordinates are shown with each cube. Fig 5.4

Step 2: Create Transformation Matrices

The next step is to create our transformation matrices. We want our cubes to all rotate differently, so we need a different transformation matrix for each one. Cube 1, 2 and 3 will rotate around the x, y, and z axis respectively. Cube 4 will rotate around a user defined axis and cube 5 will rotate around the x, y and z axis, and it will be enlarged (scaled).

To create the x, y and z rotation matrices, we will use the DirectX functions D3DXMatrixRotationX, D3DXMatrixRotationY and D3DXMatrixRotationZ. The first parameter is a pointer to a D3DXMATRIX structure that will hold the rotation matrix, the second parameter is the angle to rotate in radians. The code snippet below, taken from our new Render method, shows these calls.

`//Create the rotation transformation matrices around the x, y and z axis`

`D3DXMatrixRotationX(&matRotationX, timeGetTime()/400.0f);`

`D3DXMatrixRotationY(&matRotationY, timeGetTime()/400.0f);`

`D3DXMatrixRotationZ(&matRotationZ, timeGetTime()/400.0f);`

The next thing to do is create the rotation matrix around a user define axis. To do this, we will use the D3DXMatrixRotationAxis function of DirectX. The first parameter is a pointer to a D3DXMATRIX structure that will hold the rotation matrix. The second parameter is a pointer to a D3DXVECTOR3 structure that defines our user defined axis. We want our axis to be a 45 degree angle between the x and y axis, so we define our access by using the following vector (1, 1, 0). Fig 5.5 below shows how to define our axis. The third parameter is the angle to rotate in radians.

`//Create the rotation transformation matrices around our user defined axis`

`D3DXMatrixRotationAxis(&matRotationUser1, &D3DXVECTOR3(1.0f, 1.0f, 0.0f), timeGetTime()/400.0f);` Fig 5.5

In addition to rotation, we will need to create some other matrices. We will need a matrix to scale cube 5 so that it is 50% larger. We will also need some matrices to translate (move) our cube to the origin and back again (I'll explain why later). So, we use the following code to create these matrices:

`//Create the translation (move) matrices`

`D3DXMatrixTranslation(&matMoveRight27, 27.0, 0.0, 0.0);`

`D3DXMatrixTranslation(&matMoveLeft27, –27.0, 0.0, 0.0);`

`D3DXMatrixTranslation(&matMoveRight9, 9.0, 0.0, 0.0);`

`D3DXMatrixTranslation(&matMoveLeft9, –9.0, 0.0, 0.0);`

`D3DXMatrixTranslation(&matMoveDown15, 0.0, –15.0, 0.0);`

`D3DXMatrixTranslation(&matMoveUp15, 0.0, 15.0, 0.0);`

`//Create a scale transformation`

`D3DXMatrixScaling(&matScaleUp1p5, 1.5, 1.5, 1.5);`

What are matrices and how do they work? | DirectX 8 Programming Tutorial | Step 3: Multiply Matrices