# Direct X: D3DXMatrixRotationAxis?

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

## Recommended Posts

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

##### Share on other sites
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.

##### Share on other sites
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();

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites
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]

##### Share on other sites
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.

##### Share on other sites
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 matrixmeans first rotating the Y and Z axes around the X, then rotating the newX 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 certaindirection, you rotate around the axis that is the cross product between thedirection vector, and the vector that describes the up direction. You never ever rotate around the object's own axes, because the axes themselvesare 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:

I'm sure about what I said.

[Edited by - adriano_usp on February 13, 2005 2:54:29 AM]

##### Share on other sites
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).

##### Share on other sites
Haha, sorry i was a bit unclear as well.
I meant if yaakovdov was unclear about matrix rotation stuff,
he should go read about it some. I didn't mean you. =)

##### Share on other sites
Thanks to all!
Being a begginner, I thought that simply making a rolling ball would be a nice easy thing to start learning programming games with ... but either Ive chosen something slightly harder than expected, or programming 3D games in general is a lot more complicated than I expected! especially considering Im not very mathematical!

I'll now check out fproto's metroid Prime ball code (which, if its like the game, should be exactly what I want), and also info on matrix rotation stuff.

Thanks again.

p.s Can anyone reccomend any good tutorials to do (ive started off on some at andypike.com, but I dunno if there's any other excellent ones to do next), or alternatively, is trying to make my own games a better way to learn?

##### Share on other sites
Thanks to Fproto for your ball, but unfortunatly I get the message 'Failed to set up Direct3D'. Similarly, when I try to use some example code that comes with the directX SDK, I get messages such as "The reference rendering device was selected, but your computer only has a reduced functionality reference device installed. Install the DirectX SDK to get the fill reference device". Are these errors related? IS the SDK ive got not installed properly? Could it be to do with my Graphics card not being good enough?

Incidently Fproto, could I see the code for that ball? Thanks.

And On a seperate topic - StephenB, (or anyone else who knows), How did you rotate your sphere using D3DXMatrixRotationAxis? As I asked at the start of this thread, what do I put in the vector to make it work?

Thanks

##### Share on other sites
fproto, no problem! [smile]... sorry too, for not having understood you.

Quote:
 p.s Can anyone reccomend any good tutorials to do (ive started off on some at andypike.com, but I dunno if there's any other excellent ones to do next), or alternatively, is trying to make my own games a better way to learn?

yaakovdov, I would recommend a vectorial algebra book, but not a complex algebra. Just common operations, like add, subtraction and product of vectors and matrices. Do you dominate trigonometric functions (sin, cos, tan, etc)? A good way to understand that is studing 2D transformation, and after 3D transformation. Try to rotate a 2D vector without using matrices, that is, just using sin and cos functions. You will notice that the linear system obtained could be rearranged in a matricial form. So, you will understand rotation transformations completely [smile]. I will see if I have some material about that in my computer. I send you a message later.

##### Share on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628702
• Total Posts
2984298

• 23
• 10
• 9
• 13
• 13