# Proper object rotation in all three dimensions

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

## 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 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 on other sites
Create rotation matrix apply that transformation as world matrix

##### Share on other sites

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 on other sites

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 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):

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 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 on other sites

Good explanation about rotation matrices, and its construction.

" rel="external nofollow">

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

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.

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
A4L
13
5. 5

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633767
• Total Posts
3013737
×