#### Archived

This topic is now archived and is closed to further replies.

# how to rotate along x AND z axis (direct 3d and matrix...)

This topic is 5383 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

ok here''s some sample code...
BOOL DoFrame()
{
D3DXMATRIX matWorld;

// Clear device backbuffer
g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_RGBA(0,64,128,255), 1.0f, 0);

// Begin scene
if(SUCCEEDED(g_pD3DDevice->BeginScene())) {

// Create and set the world transformation matrix
// Rotate object along Z-axis

D3DXMatrixRotationZ(&matWorld, (float)timeGetTime() / 1000.0f);

g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);

// Set the vertex stream, shader, and texture
g_pD3DDevice->SetStreamSource(0, g_pVB, sizeof(sVertex));
g_pD3DDevice->SetTexture(0, g_pTexture);

// Draw the vertex buffer
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

// Release texture
g_pD3DDevice->SetTexture(0, NULL);

// End the scene
g_pD3DDevice->EndScene();
}

// Display the scene
g_pD3DDevice->Present(NULL, NULL, NULL, NULL);

return TRUE;
}

im trying to modify it to rotate on the x axis as well (im helping my learning process by messing with the authors code as he goes along) im trying to get a grip on matrix''s... the author doesnt explain very well on when to use matrix''s. Do you set it once and forget it? Or do you set it every frame or what... Well from what i can tell, you set a matrix when something in it has changed. Judging from this example, i think im pretty close. He updates matworld (which is empty before setting it) to rotate on the z axis, then applies this to the rendering process by setting the transform to this matrix. This then (i think) modifies everything in the world to rotate on the Z axis (in this case, its just a flat square with a texture on it, 1 sided) So i tried to modify the code, and it didnt work. which now makes me think that i know NOTHING about matrix''s... and i just read the section on them 3 times i figured you could apply it like this... D3DXMatrixRotationX(&matWorld, (float)timeGetTime() / 1000.0f); D3DXMatrixRotationZ(&matWorld, (float)timeGetTime() / 1000.0f); g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld); that didnt work, so i tried this D3DXMatrixRotationX(&matWorld, (float)timeGetTime() / 1000.0f); g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld); D3DXMatrixRotationZ(&matWorld, (float)timeGetTime() / 1000.0f); g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld); then i thought about identity matrix (i think it kind of clears the matrix out) so i did that and it didnt work either D3DXMatrixRotationX(&matWorld, (float)timeGetTime() / 1000.0f); g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld); D3DXMatrixIdentity(&matWorld); D3DXMatrixRotationZ(&matWorld, (float)timeGetTime() / 1000.0f); g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld); that didnt work either can anyone help me? maybe point me in the right direction?

##### Share on other sites
You need to multiply your rotation matrices into a single matrix and set that as your world transformation.

D3DXMATRIX matRotX, matRotZ, matWorld ;
float fThisTime = timeGetTime () / 1000.0f ;

D3DXMatrixRotationX (&matRotX, fThisTime);
D3DXMatrixRotationZ (&matRotZ, fThisTime);

D3DXMatrixMultiply (&matWorld, &matRotZ, &matRotX) ;
g_pD3DDevice->SetTransform (D3DTS_WORLD, &matWorld) ;

This should get you what you''re trying to achieve. You should check out the 3D math and matrices tutorials on the gamedev.net itself.

-UdayK

##### Share on other sites
ok i see, the author mentions this, but doesnt explain why. he just tells you that you can apply multiple matrix''s into one by doing the multiply (in a specific order). i guess the people that reviewed this book were right, he does leave out the obvious.

##### Share on other sites
This is basically why we use Matrices to represent or hold our transformation information. Several matrices holding different transformations can be merged together by multiplication, the resultant matrix then represents the overall effect of applying all the multiplied transforms individually. Therefore instead of applying each transformation separately, you can merge them into one matrix and use it instead.

Like for example, in your case, to rotate the object around the x and z axis. You can either individually apply the transformations to the object, or multiply them into one and use it instead.

• 35
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631358
• Total Posts
2999530
×