Proper object rotation in all three dimensions

Started by
8 comments, last by nbertoa 6 years, 10 months ago

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?

Advertisement

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.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Create rotation matrix apply that transformation as world matrix

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.

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.

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.

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.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

You don't need quaternions you just need to do correctly with a matrix.

watch this guy's video

" rel="external nofollow">Gimbal Lock and Matrices

Good explanation about rotation matrices, and its construction.

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

" rel="external nofollow">

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

This topic is closed to new replies.

Advertisement