Direct X: D3DXMatrixRotationAxis?

Started by
12 comments, last by adriano_usp 19 years, 2 months ago
Hi Im a begginer, and am wanting, just to start with, to roll a ball around the screen. Im using direct X (i recently did some online tutorials on it). Should I use D3DXMatrixRotationAxis? If so, how? What would I put in the D3DXVECTOR3 to make it work? Or should I be using D3DXMatrixRotationQuaternion? Or something else? Thanks in advance Andy
Advertisement
The D3DXMatrixRotationAxis function is used for creating a matrix that rotates the vectors around an arbitray axis. So, if you call something like this: D3DXMatrixRotationAxis ( &RotMat, D3DXVECTOR3( 0.0f, 1.0f, 0.0f ), D3DX_PI * 0.5f ) you would create a matrix for rotating objects 90 degrees around the y axis.
There are also another rotation functions, such as D3DXMatrixRotationX, D3DXMatrixRotationY and D3DXMatrixRotationZ that creates a matrix for rotating around the X, Y and Z axis, that may be concatenated using a matrix multiplication. I believe these three functions are the most useful for beginners, since they are the simplest to understand.
About the D3DXMatrixRotationQuaternion, it creates a rotation matrix from a D3DXQUATERNION, quaternions are very useful to represent rotations, and are computationally cheaper for concatenation, although they are very complex mathematically.
If you are beginning, I would recommend you to stick with the D3DXMatrixRotationX, Y and Z functions for now, but when you get a bit more used to the transformations don't hesitate in learning about the other functions, since they are very useful.
Thanks Xpyman

Ive tried using D3DXMatrixRotationZ & D3DXMatrixRotationX. At first, it seemed like it worked, but as I messed around with my ball a bit more, sometimes the ball wasn't rotating in the right direction. It especially seemed so when I moved the ball left and right (meaning the ball would be ROTATING on the Z axis, and MOVING left and right ALONG the X axis). Here is my code.
Here is the relevant bits of code for rotating the ball assuming this is the bit where my problem lies). Any ideas for what ive done wrong?


//Work out how to roll the ball over the X and Z axisD3DXMatrixRotationZ(&matRotateZ, D3DXToRadian(m_rAngleZ));D3DXMatrixRotationX(&matRotateX, D3DXToRadian(m_rAngleX));//Multiply the rotation matrices, and then the matrix that physically moves the ballD3DXMatrixMultiply(&matBallRoll, &matRotateZ, &matRotateX);D3DXMatrixMultiply(&matBall, &matBallRoll, &matBallMove);//Render the ballm_pD3DDevice->SetTransform(D3DTS_WORLD, &matBall);m_dwTotalPolygons += m_pSphere->Render();
Hmmm, I see no problem in your code, you are concatenating the rotations and then the translation, just the way it should be done. You would experience problems if you were doing the multiplications in the reverse order. I'm pretty sure the mistake is not in this part of the code.
Correct me if I'm wrong, but is the problem that after rotating around the z axis, the x axis changes. For example, if you rotate around the z axis 90 degrees, the X axis for your ball will be what appears to be the Y axis on the screen.

I had this problem when I was creating my 3d sphere to rotate, and was able to solve it using D3DXMatrixRotationAxis.

If you're interested, I have an object class that simplifies everything
AND gives you absolute freedom, AND rids of gimbal lock, which
StephenB mentioned.
It basically has a set of functions that transform around the object's
local axes, and then a set of functions that transform relative
to any set of axes / position you pass in.
It can do first person shooter rotations, flight simulator rotations,
arcball camera rotations, anything. None of them interfere with another.
HERE
is a simulation of the metroid prime morphball I made with this code.
I think it's similar to what you're aiming for?
function(prototype);
Quote:Correct me if I'm wrong, but is the problem that after rotating around the z axis, the x axis changes. For example, if you rotate around the z axis 90 degrees, the X axis for your ball will be what appears to be the Y axis on the screen.

I had this problem when I was creating my 3d sphere to rotate, and was able to solve it using D3DXMatrixRotationAxis.

Keep in mind that D3DXMatrixRotationX/Y/Z function supply you a rotation only around the axes from the world coordinate system (these axes can't be transformed).
Well, even if you create axes for your object, just to apply D3DXMatrixRotationX/Y/Z/Axis will not rotate them. SetTransform(D3DTS_WORLD, &matWorld) method passes a matrix to D3D to transform the vertices of the object, but not its axes.
To transform vectors you could use D3DXVec3TransformCoord function.

[Edited by - adriano_usp on February 13, 2005 2:27:18 AM]
adriano_usp, I don't know what you mean by creating axes?
SetTransform doesn't transform vertices.
SetTransform sets the current Direct3D world, view, or projection transform
to the matrix that is passed in.
And the axes ARE changed after the first rotation, by the way.
If you have D3DXMatrixRotationYawPitchRoll, the axes stay the same.
However multiplying an X rotation matrix with a Z rotation matrix
means first rotating the Y and Z axes around the X, then rotating the new
X and Y axes around the Z. You ARE rotating around your object's local axes.
That's what's causing your problem. When you want to roll a ball in a certain
direction, you rotate around the axis that is the cross product between the
direction vector, and the vector that describes the up direction. You
never ever rotate around the object's own axes, because the axes themselves
are transformed by rotations. And so the rotation would be different everytime.
If you are unclear about this stuff, go read about matrix transformations.
function(prototype);
Quote:adriano_usp, I don't know what you mean by creating axes?

You can create local axes (vectors) for your object.

Quote:SetTransform doesn't transform vertices.
SetTransform sets the current Direct3D world, view, or projection transform
to the matrix that is passed in.

...but what do you think that happens behind the setting Direct3D world? When Direct3D sets the world, all vertices will be transformed by the world matrix.

Quote:And the axes ARE changed after the first rotation, by the way.

Axes from the world coordinate system are not changed.

Quote:And the axes ARE changed after the first rotation, by the way.
If you have D3DXMatrixRotationYawPitchRoll, the axes stay the same.
However multiplying an X rotation matrix with a Z rotation matrix
means first rotating the Y and Z axes around the X, then rotating the new
X and Y axes around the Z. You ARE rotating around your object's local axes.
That's what's causing your problem. When you want to roll a ball in a certain
direction, you rotate around the axis that is the cross product between the
direction vector, and the vector that describes the up direction. You
never ever rotate around the object's own axes, because the axes themselves
are transformed by rotations. And so the rotation would be different everytime.

Just make a test:
1) Create a cube with center on the origin
2) Rotate it around Y by 20 degrees (using D3DXMatrixRotationY)
3) Now rotate it arount X by 20 degrees (using D3DXMatrixRotationX)

Analyze the results.

Quote:If you are unclear about this stuff, go read about matrix transformations

I'm sure about what I said.

[Edited by - adriano_usp on February 13, 2005 2:54:29 AM]
humm... I read my post again. My English is not good... you are right, fproto. I should not have said that SetTransform transforms vertices directly. The most correct would be to say that it passes a world matrix to Direct3D that will transforme geometrically all the vertices (before transforming the vertices from the world space to the view/camera space).

This topic is closed to new replies.

Advertisement