# D3D9 - Rotation on a specific local axis

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

## Recommended Posts

Hello,

I have a boat on my planet,
and I want to control this boat (it's a first person view where the player/camera is the boat),

but I have difficulties with the rotation of the planet.

https://s9.postimg.org/8u4qavbov/112.png :

So I would like to use the "Roll Axis" (yellow arrows) and the "Pitch Axis" (orange arrow) on the planet
on the axis that goes between the camera position and the center of the planet,
so not using the global axis like the red arrows.

Is that possible with D3D9 matrices functions and without quaternions?

Thanks.

Edited by User1975_9559

##### Share on other sites
Yes, is possible without quaternions.

You want to rotate the planet about the world x/y-axis of the ship.

Matrix33 rotation_x = MakeRotationFromAngleAxis(ship.rotation.x, angle_x);
planet.rotation = rotation_x * planet.rotation;

Matrix33 rotation_y = MakeRotationFromAngleAxis(ship.rotation.y, angle_y);
planet.rotation = rotation_y * planet.rotation;

MakeRotationFromAngleAxis returns a rotation matrix given an axis of rotation and angle of rotation about the axis. IIRC in DirectX there is a function called D3DXMatrixRotationAxis that computes this.

In DirectX you might need to swap the multiplication order. E.g:

planet.rotation = rotation_x * planet.rotation;

becomes

planet.rotation = planet.rotation * rotation_x;

Edited by Irlan Robson

##### Share on other sites

I was going to post a code snippet from my DX11 engine, but I don't like the way I coded it after I looked at the code. That was a few years back and I've learned a thing or two since then. It's also DX11 and you were asking about DX9, and the syntax is significantly different.

In my code, I moved the object to the origin, rotated it and moved it back. That's pretty certain to work and a simple modification, for the most part, to the code you have. It all happens between frames, and so the user never sees it.

How I would code it today is reverse the order of multiplication like Irian said. The problem is that you are using a function to do the rotation and so you have to rewrite the function, use a different function, or not use the function.

But, what I do today is create a rotation matrix, and multiply the rotation matrix by the object's matrix and store the result in the object's matrix. If you swap the order of multiplication it will change between orbiting the origin and rotating around the local axis. That's what the Matrix Rotation function is doing. Except you would have to rewrite the function to change the order of multiplication.

I'm working in OpenGL these days and the rotation function wants to do the rotation for me, which is unacceptable in my opinion. So, I force it to build a rotation matrix for me by feeding it an identity matrix. A rotated identity matrix is a rotation matrix that you can then multiply by a give object's matrix which when the two are multiplied together will return a matrix for the object that has been rotated. And if that orbits instead of rotates, you simply reverse the order of multiplication. The same thing works for quaternions except for some reason the order of multiplication with them seems opposite of what it is for matrices.

##### Share on other sites

Thank you I succeeded.

Edited by User1975_9559

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633694
• Total Posts
3013372
×