World axis-aligned Matrix Rotations

Started by
3 comments, last by Maddibob 18 years, 11 months ago
Has anyone got any ideas on how best to carry out world-axis aligned matrix rotations whilst transforming objects? By this I mean rotate a cube 45 degrees on the world Y axis and then 45 degrees on the world Z axis. At the moment, if I do this, after the first Y rotation, the cube rotates about it's 'new' local Z axis rather than the world's. When I say 'after the first Y rotation', I mean that the Y rotation is multiplied into the object's world transform matrix before the Z axis is. The reason I'm asking is because I'm trying to rotate the object in projection views (e.g. front, side and top) and whilst rotating on the object's local axis is okay, I'd prefer it to rotate around the world axis which are obviously aligned to my views. Does anyone have any thoughts? By the way, I'm using DirectX9 with vc++. Thanks
Advertisement
The short answer is, don't re-build your matrix from scratch each frame. Instead, maintain the object's orientation matrix from frame to frame. To rotate around a world axis (or any axis), simply multiply with the appropriate matrix. Also, you will need to re-orthogonalize occasionally to prevent drift (I don't know if DirectX has a function for this).
Thanks jyk, that would work well. Is there a way to extract the orientation from a Matrix once it needs to be saved? Let's say for example 3 rotations have taken place in 3 different views, each view's rotation looks correct and the object's maintained Matrix represents it's current orientation.

When I come to save it, can I extract the _CURRENT_ x, y and z rotations so that when I reload the object I can apply the x, y and z and see the object in exactly the correct orientation - assuming I am multiplying Rotation x then y then z?

Thanks
Quote:Is there a way to extract the orientation from a Matrix once it needs to be saved? Let's say for example 3 rotations have taken place in 3 different views, each view's rotation looks correct and the object's maintained Matrix represents it's current orientation.

When I come to save it, can I extract the _CURRENT_ x, y and z rotations so that when I reload the object I can apply the x, y and z and see the object in exactly the correct orientation - assuming I am multiplying Rotation x then y then z?
There are several different ways to represent a rotation or orientation. Commonly used are Euler angles, axis-angle pairs, matrices, and quaternions. There are methods for converting from any of these representations to any other. They can also all be written to a file. If you're worried about memory, Euler angles and (compressed) quaternions require only three entries, while matrices can be stored with six.

Some of the conversions are easier than others. Is there any particular reason you don't want to just write the matrix itself to the file? It would certainly be easier than decomposing it into Euler angles and then reconstructing it.
I hadn't thought about writing out and reloading the matrix with the object. At the moment, I'm just saving the position, scale and xyz rotation, which works fine for local-aligned rotations as I can just save this data and reconstruct the matrix upon loading it. As I want to be able to rotate on either, maintaining a matrix and saving/reloading it is the way forward.

I'll give it a go.

Thanks for your help

This topic is closed to new replies.

Advertisement