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
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.