Jump to content
  • Advertisement


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 5564 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

  // Clear device backbuffer
                      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

  // 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 this post

Link to post
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.


Share this post

Link to post
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 this post

Link to post
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.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!