   DirectX 3D rotation
  I'm trying to do some 3D rotation in directx.I got this code to work perfectly to rotate and move the camera but when I try to use it on a mesh I get weird results. Instead of rotating around it's "own" axis it always rotates around the center axis if you get what I mean.I think it's because the order of the translation, moving then rotation rather than rotating then moving. That works with the camera since when positioned it is always at 0,0,0 while that's not true about world space...I can't figure out how to make these kind of rotation to work for both the camera and world space.// &rotateAroundRight/Up/LookAt is the amount in radians to rotate around that axis// mUp, mRight, mPos is the amount to move in up/right/forward direction// Matrices to store the transformationsD3DXMATRIX total, rotateAroundRight, rotateAroundUp, rotateAroundLookAt;D3DXMatrixRotationAxis(&rotateAroundRight, &mRight, mRotateAroundRight);D3DXMatrixRotationAxis(&rotateAroundUp, &mUp, mRotateAroundUp);D3DXMatrixRotationAxis(&rotateAroundLookAt, &mLookAt, mRotateAroundLookAt);D3DXMatrixMultiply(&total, &rotateAroundUp, &rotateAroundRight);D3DXMatrixMultiply(&total, &rotateAroundLookAt, &total);// Transform two vectors by our matrix and computes the third by// cross productD3DXVec3TransformCoord(&mRight, &mRight, &total);D3DXVec3TransformCoord(&mUp, &mUp, &total);D3DXVec3Cross(&mLookAt, &mRight, &mUp);// Check to ensure vectors are perpendicular (vinkelrät)if (fabs(D3DXVec3Dot(&mUp, &mRight)) > 0.01){    // If they're not    D3DXVec3Cross(&mUp, &mLookAt, &mRight);}// Normalize vectorsD3DXVec3Normalize(&mRight, &mRight);D3DXVec3Normalize(&mUp, &mUp);D3DXVec3Normalize(&mLookAt, &mLookAt);// Compute the bottom row of the view matrixfloat fView41, fView42, fView43;fView41 = -D3DXVec3Dot(&mRight, &mPos);fView42 = -D3DXVec3Dot(&mUp, &mPos);fView43 = -D3DXVec3Dot(&mLookAt, &mPos);// Fill in the view matrixview = D3DXMATRIX(mRight.x, mUp.x, mLookAt.x, 0.0f,mRight.y, mUp.y, mLookAt.y, 0.0f,mRight.z, mUp.z, mLookAt.z, 0.0f,fView41, fView42, fView43, 1.0f);// Done in another function// d3ddev->SetTransform(D3DTS_WORLD, &view);[Edited by - zbind on May 15, 2009 12:40:39 PM]
  This bit of code:float fView41, fView42, fView43;fView41 = -D3DXVec3Dot(&mRight, &mPos);fView42 = -D3DXVec3Dot(&mUp, &mPos);fView43 = -D3DXVec3Dot(&mLookAt, &mPos);view = D3DXMATRIX(mRight.x, mUp.x, mLookAt.x, 0.0f,mRight.y, mUp.y, mLookAt.y, 0.0f,mRight.z, mUp.z, mLookAt.z, 0.0f,fView41, fView42, fView43, 1.0f);Sets up an inverted transform. This is exactly what you want when building a view transform, but usually won't give you the right results when rendering an object.Here is the non-inverted version:view = D3DXMATRIX(    mRight.x, mRight.y, mRight.z, 0.0f,    mUp.x, mUp.y, mUp.z, 0.0f,    mLookAt.x, mLookAt.y, mLookAt.z, 0.0f,    mPos.x, mPos.y, mPos.z, 1.0f);There may be other problems in your code as well, but I'd start by making that change and see if it helps.   ;  Thanks a lot!, it did help, the mesh rotates perfectly around its own axis now.Another problem though, or maybe a feature?It doesn't matter if I set the non-inverted matrix as D3DTS_WORLD or D3DTS_VIEW, both will make the mesh rotate and not the camera even with D3DTS_VIEW.Is this expected behaviour or is it probably caused by something else in my code?If it helps I'm trying to create something like a MovableObject class which both the camera and the model/mesh class will inherit from.   ;Quote:Original post by zbindThanks a lot!, it did help, the mesh rotates perfectly around its own axis now.Another problem though, or maybe a feature?It doesn't matter if I set the non-inverted matrix as D3DTS_WORLD or D3DTS_VIEW, both will make the mesh rotate and not the camera even with D3DTS_VIEW.Is this expected behaviour or is it probably caused by something else in my code?If it helps I'm trying to create something like a MovableObject class which both the camera and the model/mesh class will inherit from.AFAIK, the view and world matrices are kept separate in D3D merely for convenience. I think they're both interpreted in the same way, which means that you could put your model transform in the D3D view matrix or your camera transform in the D3D world matrix if you wanted. So I think what you're seeing is the expected behavior.   ;  I figured this out. It is strange. The Matrix.Rotate... functions say they return void, but they seem to actually return a matrix. For example, this:sprite.Transform = Matrix.Transform2D(a,b,c,d,e,f);works. There are other cases where an assignement won't work, but I used that one in my code and it runs.The directX documentation is so poor.__________________________________________________________________________________disney videosThe Big Brain - Education and Reference