Sign in to follow this  

How to apply two rotations "independently"?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by lanceking
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?


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.

Share this post


Link to post
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);
glLoadMatrixd(local_matrix);
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Krylloan
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.


2 l's in oconnell :-)

Share this post


Link to post
Share on other sites
Quote:
Original post by Krylloan
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.


Doesn't it all come down to implementation? What you're suggesting here is to:

A) Completely rebuild your transform matrix every time you draw the transformed object - then using that transformation:
B) Transform the object from a set of untransformed vertices.

I don't see any problem doing it this way, but I do take some concern over the statement, "If you don't do this you will never be able to rotate around the original axis, and you will also accumulate rounding errors."

The original axis still exists, or else the object wouldn't be in the same shape after the rotation. The key here is getting the 'local space' axis info and applying the rotation across that space. If rotations are applied in this manner, the order of rotation (x first, y first, z first, whatever) does not matter in the slightest.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this