# How to apply two rotations "independently"?

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

## Recommended Posts

If first I rotate along X axis, Y and Z axis will change, how can I perform a rotation on the "original" Z axis? Just like we rotate a object in 3dsmax, many thanks!!

quaternions

##### Share on other sites
Simple. Rotate around the Z axis first. That's probably not the answer you are looking for, but it is closer than you might think. If your current rotation matrix is M and your rotation around the Z axis is the matrix RZ, you do it like this:

You currently have
v' = Mv
You want
v' = RZMv
so you do this:
v' = MM-1RZMv = M(M-1RZM)v
Which means 1. rotate by the inverse of the original rotation, 2. rotate around Z axis, 3. rotate by the original rotation.

Another way might be easier. Instead of rotating around the Z axis, first transform the Z axis by the inverse of the current rotation and then rotate around that axis. In other words, rotate around the axis M-1z.

##### Share on other sites
I have tried this but it is not what I want, I want to
rotate an object just like in 3dsmax. It seems that I have
to calculate the tangent of the rotate circle, I wonder how
they implement it in 3dmax?

##### Share on other sites
Quote:
 Original post by lancekingI have tried this but it is not what I want, I want to rotate an object just like in 3dsmax. It seems that I haveto calculate the tangent of the rotate circle, I wonder howthey implement it in 3dmax?

In 3dsmax you have 3 ways to rotate the objects using Arc Rotate tool:
1) If you click inside the circle you get a free rotation. The weird aspect of this rotation is the reference system used to perform such rotations. Let's suppose to use a LH reference system. Max rotates the scene around X and Z axis using the camera space but it uses the world space to rotate around Y
2) Constrained rotation if you click inside an handle
3) Rotation around the Z axis if you click and move the mouse outside the circle. In this case you can use the tangent as you correctly said.

Quaternions. (2)

##### Share on other sites
You don't need quaternions. Every time you perform a rotation, store the resulting matrix and the next time you perform another rotation, simply multiply the two matrices together and restore it.

Here's how you would do it for rotating around the 'World' Axis.

glRotatef(...)
glMultMatrixd(world_matrix);
glGetDoublev( GL_MODELVIEW_MATRIX, world_matrix);

And here's how you do it for local rotations, where you rotate around the object's 'Local' Axis.

glRotatef(...)
glGetDoublev( GL_MODELVIEW_MATRIX, model_view);
glMultMatrixd(model_view);
glGetDoublev( GL_MODELVIEW_MATRIX, local_matrix);

And when you draw your geometry, to apply these rotations call the following functions in this order:

glTranslatef(posX, posY, posZ);
glScalef(world_scaleX, world_scaleY, world_scaleZ);
glMultMatrixd( world_matrix );
glMultMatrixd( local_matrix );
glScalef(local_scaleX, local_scaleY, local_scaleZ);
RenderGeometry();

And that's it, its a lot better than having to do all the math yourself and as you can see, its very simple. Hope this helps.

##### Share on other sites
I am probably doing this in a nastier fashion - it's maybe too much math, but it's rock solid.

That said, I'm using a rotation schema that uses Quaternions, Matrices and Axis-Angle notation.

Quaternion is the most solid of these (as far as I have determined, it's very stable, and converts to the other formats very easily), so it's the format my rotations are in. To add a rotation to the quaternion in the manner described here (effectively doing two independent transforms as one), I convert the quaternion to a matrix, then extract the axis values I want to change - ie, I get the transformed Z-Axis (the local axis, in worldspace representation? Are these the correct terms to use?).

Anyway, once you have the Axis values, you can plug those into an AxisAngle to Quaternion conversion and then multiply your new quaternion in to your original quaternion to get your new '2-2-2-transforms-in-one!' quaternion.

##### Share on other sites
Er, yeah, quaternions rock. but you may not need their power and complexity. (If you have lots of problems like this then you really want quats tho).

Assuming you're still using Euler (X, Y and Z rotations) and not Quaternions:
But anyway, what are you rotating with? Is this a model using openGL rotations? Is this a pure software rotation?

Assuming you're not using OpenGL (If you are then use oconnelseanm's method)
Are you storing a copy of the original un-rotated object? you probably should be.

Every time you rotate, recalculate the rotated object from the un-rotated object, discard the old rotated co-ordinates. If you don't do this you will never be able to rotate around the original axis, and you will also accumulate rounding errors.

You will have to chose a primary axis to rotate around first. (This is a consequence of using Euler). So it seems you want to rotate around Z first.

##### Share on other sites
Quote:
 Original post by KrylloanEr, yeah, quaternions rock. but you may not need their power and complexity. (If you have lots of problems like this then you really want quats tho).Assuming you're still using Euler (X, Y and Z rotations) and not Quaternions:But anyway, what are you rotating with? Is this a model using openGL rotations? Is this a pure software rotation?Assuming you're not using OpenGL (If you are then use oconnelseanm's method) Are you storing a copy of the original un-rotated object? you probably should be.Every time you rotate, recalculate the rotated object from the un-rotated object, discard the old rotated co-ordinates. If you don't do this you will never be able to rotate around the original axis, and you will also accumulate rounding errors.You will have to chose a primary axis to rotate around first. (This is a consequence of using Euler). So it seems you want to rotate around Z first.

2 l's in oconnell :-)

1. 1
2. 2
3. 3
Rutin
21
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 10
• ### Forum Statistics

• Total Topics
633653
• Total Posts
3013149
×