Proper object rotation in all three dimensions

Recommended Posts

Hey,

I want to rotate my object in the world in X, Y and Z axis. I have vector with 3 rotation values, each for one dimension.

But If I want to rotate it in more than one dimension, my object is not rotating properly. For example after 90 degrees X rotation if I want to rotate in my object in Y axis it is rotating like in his Z axis.

I want to calculate my rotation values to make the object rotation proper but really don't want to waste for this much time. Is there any simple solution or maybe example code for this?

Share this post


Link to post
Share on other sites

One way to think of it is that your object vertices start in model space, so the first matrix you apply is always in model space. For instance I have an airplane AI in my RTS game. Every frame I will decide if the plane needs to adjust roll/pitch/yaw etc, based on his current heading. I simply take the current matrix and do a model space transform before, to get the final matrix.

(Curr plane matrix world )*(some Yaw matrix model space) = new current matrix world

(new current matrix world)*(some Roll matrix model space) = new current matrix world.

So if you understand that you can always pre-apply a model space matrix, that is, you are multiplaying a local space matrix first, and then applying the world space matrix, you can always do anything relative to your model, such as continually apply new yaw before your current matrix, and make the model spin on its own yaw axis.
 

Share this post


Link to post
Share on other sites
Posted (edited)

What you should be doing is to keep rotated axes as well as the rotated object, so you can rotate the object about its local axes. What your currently doing is to rotate about world axis like the link below:
https://en.wikipedia.org/wiki/Rotation_matrix#General_rotations

You can also store the rotation angles of each axis and apply them in order to model and the axes of next rotations.

Edited by IYP

Share this post


Link to post
Share on other sites
Posted (edited)

3D rotations are much more complicated than 2D rotations and take some time to learn.  Here are some tips and pointers:

 

But If I want to rotate it in more than one dimension, my object is not rotating properly.

The rotations you using are called "Euler Angles".  With Euler angles, order matters.  45°x, 45°y, 45°z does not yield the same results as 45°z, 45°y, 45°x.

 

For example after 90 degrees X rotation if I want to rotate in my object in Y axis it is rotating like in his Z axis.

 

What you are seeing is called "Gimbal Lock."  When two axes overlap (ie, after a 90° rotation), you lose a degree of freedom and strange things happen.

 

It sounds like you are performing rotations in worldspace.  As IYP noted, you should probably be performing most of your rotations in local space (I don't know what math library you're using, but it should have a local rotation function).  Order still matters, so be consistent about that order and conscientious if you need to change the order.  You might also find yourself needing to mix and match local and world rotations.  Here's a (poor) example - imagine a car driving around a bend.  You would make the car steer with a worldspace y-rotation, but you would simulate body roll with a localspace z-rotation.

My years as a tech artist have also taught me that you should attempt to isolate axial rotations.  What I mean is, try to only perform rotations on a single axis at a time if possible.  Think of a gun turret - the body of the turret only rotates left-and-right on the y-axis, while the gun barrel only rotates up-and-down on the x-axis.

Edited by missionctrl

Share this post


Link to post
Share on other sites
To add what missionctrl said, here's a video that explains gimbal lock (at 1:00, the video shows the gimbal lock):
https://youtu.be/zc8b2Jo7mno?t=52s


If you are using a custom game engine, you need to implement quaternions (of finding a library that does that job for you), and you can get your matrix rotation for proper rotation of the object.

Share this post


Link to post
Share on other sites

But If I want to rotate it in more than one dimension, my object is not rotating properly. For example after 90 degrees X rotation if I want to rotate in my object in Y axis it is rotating like in his Z axis.

 

this is correct behavior for global rotations around x, then Z.    as mentioned, gimibal lock is an issue in this particular case.

be aware that:

1. rotations are not commutative - ie they are order dependent.

2. euler angles can result in gimbal lock.

3. global and local axis rotations are different things.

i personally suspect its possible to express any orientation in eulers without gimbal lock.

Share this post


Link to post
Share on other sites

Good explanation about rotation matrices, and its construction.

https://www.fastgraph.com/makegames/3drotation/

https://www.youtube.com/watch?v=RqZH-7hlI48

If you are using DirectX, you could use DirectXMath library that has several methods to apply operations on matrices.

https://msdn.microsoft.com/en-us/library/windows/desktop/ee415594(v=vs.85).aspx

If you want to read a book, I recommend 3D Math Primer 2nd Edition. It has geometric and mathematic explanation about many topics, in particular, rotation matrices.

https://www.amazon.com/Math-Primer-Graphics-Game-Development/dp/1568817231/ref=sr_1_1?ie=UTF8&qid=1498480799&sr=8-1&keywords=3d+math+primer

Share this post


Link to post
Share on other sites

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