How to use Quaternions in Irr?

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
Apoptyzm
Posts: 19
Joined: Tue Sep 23, 2008 7:53 pm

How to use Quaternions in Irr?

Post by Apoptyzm »

Hello,
im fighting endlessly with quaternions...

Currently im using quaternions for Slerp interpolation around Y axis.
It works fine(angle from atan2()) except for 180 angles in place of 0 on X and Z.
The angle (0, 90, 0) always looks like (180, 90, 180) after converting to euler.

So i tried to test it out but it gets even worse...

simple example:

Code: Select all

	core::vector3df angles(180.0f*core::DEGTORAD,90.f*core::DEGTORAD,45.f*core::DEGTORAD);
	core::matrix4 m;
        core::vector3df euler ;
	float anglef=0;

	m.setRotationRadians( angles);
	core::quaternion find180(m);

	find180.toEuler( euler );
	find180.toAngleAxis(anglef ,core::vector3df(0,0,1));
	anglef *= core::RADTODEG;	//148.6002   
	euler = euler * core::RADTODEG;//-89,9999, 45.0000, 90.0000
I've tried other ways to initialise quats and i give up... I don't know what im missing here.
xDan
Competition winner
Posts: 673
Joined: Thu Mar 30, 2006 1:23 pm
Location: UK
Contact:

Post by xDan »

(0, 90, 0) always looks like (180, 90, 180)
Perhaps they are exactly the same rotation? Just represented differently. Try applying the rotations to a scene node, for example, and see if they appear the same.

If you specifically want Y rotation as a value from 0-360, you might want this:

Code: Select all

// Extract the Y component from a rotation. (euler,degrees)
// Rotation starting at {0,0,1}
f32 extract_y_rotation(const core::vector3df &rot)
{
    core::vector3df vec(0,0,1);
    core::quaternion quat(rot * core::DEGTORAD);
    vec = quat * vec;
    
    core::vector2df vec2d(vec.X,vec.Z);
    vec2d.normalize();
    return vec2d.getAngle() + 90.0; // Irrlicht has 0 to the right (1,0) rather than up (0,1).
}
Finally, I think there is a bug in toEuler, however I'm not sure that's your problem in this case.
Post Reply