Strange behaviour of setRotation around Z axis

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Pouf
Posts: 6
Joined: Fri Feb 27, 2015 11:37 pm

Strange behaviour of setRotation around Z axis

Post by Pouf »

Hi,

I'm a beginner in 3D programming, and I currently am trying to understand Irrlicht by programming a little controllable space ship. The purpose is to be able to throttle in any 6 directions, and rotate independently as well. So far, I've been able to deal with the moving. But the rotating part is giving me headaches, due to one particular problem, which is that the setRotation method seems to behave differently for Z axis than for X and Y axis. Here's what's happening :

I have a mesh that is centered on the origin of the world. To rotate it with keyboard, I detect when a key is pressed for the three axes, and store each in the same vector, that I add to the rotation of my mesh :

Code: Select all

 
    vector3df vRotation(0, 0, 0);  // Rotation to apply
 
    EKEY_CODE sKeysPositive[3] = {KEY_KEY_I, KEY_KEY_L, KEY_KEY_U};  // Keys to rotate counterclockwise
    EKEY_CODE sKeysNegative[3] = {KEY_KEY_K, KEY_KEY_J, KEY_KEY_O};  // Keys to rotate clockwise
    f32 * sRotationAxe[3] = {&vRotation.X, &vRotation.Y, &vRotation.Z};  // Axes
 
        // For the three axes
    for(u32 i = 0; i < 3; ++i)
    {
        if(bIsKeyDown(sKeysPositive[i]))   // If the key to rotate counterclockwise is pressed
            *sRotationAxe[i] = ROTATION_SPEED;    // Add ROTATION_SPEED to the corresponding component of the rotation vector
        else if(bIsKeyDown(sKeysNegative[i]))  // If the key to rotate counterclockwise is pressed
            *sRotationAxe[i] = -ROTATION_SPEED;   // Add -ROTATION_SPEED to the corresponding component of the rotation vector
 
                // Stay between 0 and 360°
        if(*sRotationAxe[i] > 360)
            *sRotationAxe[i] -= 360;
        else if(*sRotationAxe[i] < -360)
            *sRotationAxe[i] += 360;
    }
 
        // If there is a rotation to apply
    if(vRotation.X != 0
    || vRotation.Y != 0
    || vRotation.Z != 0)
        mpoShip->setRotation(mpoShip->getRotation() + vRotation * fElapsedSecs);  // Add the rotation to the existent one
 
Logically, the mesh should rotate around HIS X, Y and Z axis. A rotation around X tilts the 3 axis of the mesh, and therefore, a later rotation around Y will not rotate the mesh around a vertical axis relative to the world, but a vertical axis relative to the mesh :

Image

Well, it's not. It's not, and I'm willing to accept that, except, inexplicably, it's not, but for the X axis. If I rotate my mesh around Y, then around X, the second rotation will be around the mesh axis, i.e. like in the top-half of the pic. If I rotate around the X axis, then around the Y axis, the second rotation will be around the world axis, i.e. like in the bottom-half of the pic. The Z axis is behaving the same as the Y, i.e. not as I'm expecting it.

What the heck ? What am I missing ? I mean, I would be perfectly fine with the rotation logic to be world-axis based, but 2 axis over 3 world-based, the third mesh-based ? Wut ?

Please clarify this to me. I'm starting to loose it...

Thank you.
Pouf
Posts: 6
Joined: Fri Feb 27, 2015 11:37 pm

Re: Strange behaviour of setRotation around Z axis

Post by Pouf »

Thank you KH.

I've done my homework, I did read this post. Maybe I haven't stressed it enough, but I'm rather looking for an explanation than a solution. This post has some good working code, but no one seems to understand why a simple setRotation can't do the trick. Why it behave like it does. There's only this post : http://irrlicht.sourceforge.net/forum/v ... 52#p285752 which vaguely suggests that the setRotation methods is bugged in Irrlicht. Is that it ?

I like to understand problems rather than getting pre-made solutions :)
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Strange behaviour of setRotation around Z axis

Post by mongoose7 »

When you apply the rotation (X, Y, Z), it is actually applied as Z, X, Y. This conforms with flight, as you "pull up" relative to your current attitude. But the method of using three angles like this is basically non-intuitive. In fact, for true Euler angles, you only use two axes, for example, Y1, X, Y2. But the flight model is useful and it works rather well.
Pouf
Posts: 6
Joined: Fri Feb 27, 2015 11:37 pm

Re: Strange behaviour of setRotation around Z axis

Post by Pouf »

Ok, I think I got it. The mesh is always rotating around the world axis, and in a particular order. I didn't get what you mean by Y1 and Y2, though, but that's not really relevant, I think...

Thank you.
Post Reply