rotation around local 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
Vel0city
Posts: 4
Joined: Sun Sep 25, 2016 2:22 pm

rotation around local axis

Post by Vel0city »

Hi,

I am trying to rotate a scene node around "the 3d model's local axis" by keystroke. So obviously that has to be converted to the Euler rotation somehow.
I searched this forum and already found something with matrices but didn't understand anything with my rather basic math skills.

So how do I convert local rotation to Euler rotation?
CuteAlien
Admin
Posts: 9634
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: rotation around local axis

Post by CuteAlien »

Probably there is some better way where I don't need X matrices, but following might work for what you want:

Code: Select all

 
// oldRotation: the stuff you get from node->getRotation()
// rotationAngles - in degrees
// useLocalAxes - when true rotate object around it's own axes, otherwise rotated around global axes
// return euler angles to use in node->setRotation
irr::core::vector3df rotateAxesXYZToEuler(const irr::core::vector3df& oldRotation, const irr::core::vector3df& rotationAngles, bool useLocalAxes)
{
    irr::core::matrix4 transformation;
    transformation.setRotationDegrees(oldRotation);
    irr::core::vector3df axisX(1,0,0), axisY(0,1,0), axisZ(0,0,1);
    irr::core::matrix4 matRotX, matRotY, matRotZ;
 
    if ( useLocalAxes )
    {
        transformation.rotateVect(axisX);
        transformation.rotateVect(axisY);
        transformation.rotateVect(axisZ);
    }
 
    matRotX.setRotationAxisRadians(rotationAngles.X*irr::core::DEGTORAD, axisX);
    matRotY.setRotationAxisRadians(rotationAngles.Y*irr::core::DEGTORAD, axisY);
    matRotZ.setRotationAxisRadians(rotationAngles.Z*irr::core::DEGTORAD, axisZ);
 
    irr::core::matrix4 newTransform = matRotX * matRotY * matRotZ * transformation;
    return newTransform.getRotationDegrees();
}
 
(for explanation: I rotate around each of the 3 axes on it's own and create a matrix describing each of those 3 rotations. When local is wanted I transform the 3 axes into the local coordinate system first. Then I merge the matrices again and multiply them by the original transformation which the node had).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Vel0city
Posts: 4
Joined: Sun Sep 25, 2016 2:22 pm

Re: rotation around local axis

Post by Vel0city »

Thanks for the quick reply and the solution. It works perfectly
Post Reply