0

I understand that both Euler and Quaternion rotation types have their own distinctive quirks, however the problem that I'm having is that (for example) when performing the following rotations to an object:

  • rotateX = 90.0
  • rotateY = 90.0
  • ... Oh, hang on a minute... now the X and Z axis are basically the same!

See, what I want is to rotate a cube say 90 degrees X, 90 degrees Y and still have all axis points back in their original position as opposed of rotating locally.

Any code examples would be ideal - Here is the code I'm currently using:

_model =    scale(_scale) *
                    translate(_position) *
                    (   rotate(_rotation.data[0], 1.0f, 0.0f, 0.0f) * 
                        rotate(_rotation.data[1], 0.0f, 1.0f, 0.0f) * 
                        rotate(_rotation.data[2], 0.0f, 0.0f, 1.0f) );

I have a Math.h that calculates the rotations like so:

    template <typename T>
    static inline Tmat4<T> rotate(T angle, T x, T y, T z)
    {
        Tmat4<T> result;

        const T x2 = x * x;
        const T y2 = y * y;
        const T z2 = z * z;
        float rads = float(angle) * 0.0174532925f;
        const float c = cosf(rads);
        const float s = sinf(rads);
        const float omc = 1.0f - c;

        result[0] = Tvec4<T>(T(x2 * omc + c),           T(y * x * omc + z * s),     T(x * z * omc - y * s),     T(0));
        result[1] = Tvec4<T>(T(x * y * omc - z * s),    T(y2 * omc + c),            T(y * z * omc + x * s),     T(0));
        result[2] = Tvec4<T>(T(x * z * omc + y * s),    T(y * z * omc - x * s),     T(z2 * omc + c),            T(0));
        result[3] = Tvec4<T>(T(0),                      T(0),                       T(0),                       T(1));

        return result;
    }
DrStrange
  • 94
  • 1
  • 9
  • 1
    What is it about Euler angles that makes everyone seem to want to use them? Why are axial rotations like this the go-to method people have for storing orientations of objects? – Nicol Bolas Nov 06 '16 at 20:35
  • Because it's the mainstream way to go. Most tutorials on OpenGL and GLM seem to only use Euler rotations because it's local and easy to use. – DrStrange Nov 06 '16 at 21:54
  • I'm not 100% sure but I think I might be experiencing gimballock? All axis angles seem to merge into one after performing 2 or 3 rotations. How would I work around this in the simplest way as possible? Thanks. – DrStrange Nov 06 '16 at 22:05
  • @William: The only 'quirk' of quaternions is if you interpolate them. Otherwise they are the best choice for representing orientations in a computer. Also it's unclear what you're asking. What kind of effect you are trying to achieve? The way you formulated the question in title you will always get a gimbal lock no matter what representation you choose. – Yakov Galka Nov 06 '16 at 22:20
  • Sorry for the vague question. Basically I want to be able to rotate on each axis using a model matrix (as shown above). However, instead of having it so each axis rotation will effect another axel (causing gimbal lock), instead I'd like the orientation to reset after a rotation has been performed - almost like the "Reset X-Form" button in 3Ds Max. – DrStrange Nov 06 '16 at 22:39
  • @NicolBolas because matrices are scary for beginners usually for the lack of linear algebra knowledge even if their use is much simpler and better then Euler angles. When the rookies mature to pros most of them stick to Eulers because they either lazy or still did not learn (why should they when the Euler stuff is "working"). Sadly it is true Euler angles are main stream especially for games which makes me cry sometimes when nice piece of code is spoiled by them. – Spektre Nov 08 '16 at 10:16
  • 1
    @William take a look at: [Understanding 4x4 homogenous transform matrices](http://stackoverflow.com/a/28084380/2521214) especially the part where local and global rotations are described and last edit with QA's with examples and demos how to do it in C++ – Spektre Nov 08 '16 at 10:27

0 Answers0