matrixToQuaternion

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
wing64
Competition winner
Posts: 242
Joined: Wed Jul 23, 2008 2:35 am
Location: Thailand
Contact:

matrixToQuaternion

Post by wing64 »

Code: Select all

template <typename T> int sign(T val) {
    return (T(0) < val) - (val < T(0));
}
 
irr::core::quaternion m2q( const irr::core::matrix4& m ) 
{
    // Adapted from: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
    irr::core::quaternion q;
    q.W = sqrtf( irr::core::max_( 0.0f, 1.0f + m[0] + m[5] + m[10] ) ) / 2.0f; 
    q.X = sqrtf( irr::core::max_( 0.0f, 1.0f + m[0] - m[5] - m[10] ) ) / 2.0f; 
    q.Y = sqrtf( irr::core::max_( 0.0f, 1.0f - m[0] + m[5] - m[10] ) ) / 2.0f; 
    q.Z = sqrtf( irr::core::max_( 0.0f, 1.0f - m[0] - m[5] + m[10] ) ) / 2.0f; 
    q.X *= sign( q.X * ( m[6] - m[9] ) );
    q.Y *= sign( q.Y * ( m[8] - m[2] ) );
    q.Z *= sign( q.Z * ( m[1] - m[4] ) );
    return q;
}
copy code from http://answers.unity3d.com/questions/11 ... ctor3.html
chronologicaldot
Competition winner
Posts: 685
Joined: Mon Sep 10, 2012 8:51 am

Re: matrixToQuaternion

Post by chronologicaldot »

That might come in handy. Thanks for posting!
CuteAlien
Admin
Posts: 9647
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: matrixToQuaternion

Post by CuteAlien »

Our irr::core::quaternion constructor does take a matrix (note that it got fixed in 1.8, so if you use an older Irrlicht version it didn't work well).
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
wing64
Competition winner
Posts: 242
Joined: Wed Jul 23, 2008 2:35 am
Location: Thailand
Contact:

Re: matrixToQuaternion

Post by wing64 »

irr::core::quaternion constructor (1.8 trunk svn.4438) not work for my unreal psk/psa loader (no luck) :-(
CuteAlien
Admin
Posts: 9647
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: matrixToQuaternion

Post by CuteAlien »

Depending on how the matrix are described in psk/psa you might have to transpose them first before passing them to the quaternion.
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
fmx

Re: matrixToQuaternion

Post by fmx »

I did tests on this shortly after 1.8 came out and I can confirm there is nothing wrong with irrlicht's Quaternion and Matrix calculations, so as CuteAlien said the problem is likely to be elsewhere in your pipeline
Post Reply