ramad77 101 Posted June 8, 2017 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? 0 Share this post Link to post Share on other sites

dpadam450 2357 Posted June 8, 2017 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. 0 Share this post Link to post Share on other sites

KKTHXBYE 1454 Posted June 8, 2017 Create rotation matrix apply that transformation as world matrix 0 Share this post Link to post Share on other sites

IYP 1545 Posted June 9, 2017 (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 June 9, 2017 by IYP 1 Share this post Link to post Share on other sites

missionctrl 206 Posted June 9, 2017 (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 June 9, 2017 by missionctrl 1 Share this post Link to post Share on other sites

ferreiradaselva 379 Posted June 9, 2017 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. 0 Share this post Link to post Share on other sites

Norman Barrows 7181 Posted June 9, 2017 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. 1 Share this post Link to post Share on other sites

Zototh 1 Posted June 26, 2017 (edited) 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 Edited June 26, 2017 by Zototh gave link instead of video 1 Share this post Link to post Share on other sites

nbertoa 1022 Posted June 26, 2017 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 0 Share this post Link to post Share on other sites

Good explanation about rotation matrices, and its construction.

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

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