PDA

View Full Version : Matrices, quads



wh1sp3rik
10-30-2009, 11:13 AM
Hello,
I would like to ask, how to calculate quaternion from matrix ? thank you :)

i found this site, but it shows different values then my other app, which is right (i think)

http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm

Ilian Dinev
10-30-2009, 01:30 PM
void Quat::FromMatrix(const Mat3& mat)
{
float trace = mat(0, 0) + mat(1, 1) + mat(2, 2);
if (trace > nv_zero){
float scale = sqrtf(trace + nv_one);
w = nv_zero_5 * scale;
scale = nv_zero_5 / scale;
x = scale * (mat(2, 1) - mat(1, 2));
y = scale * (mat(0, 2) - mat(2, 0));
z = scale * (mat(1, 0) - mat(0, 1));
}
else
{
static int next[] = { 1, 2, 0 };
int i = 0;
if (mat(1, 1) > mat(0, 0))
i = 1;
if (mat(2, 2) > mat(i, i))
i = 2;
int j = next[i];
int k = next[j];
float scale = sqrtf(mat(i, i) - mat(j, j) - mat(k, k) + 1);
float* q[] = { &x, &y, &z };
*q[i] = 0.5f * scale;
scale = 0.5f / scale;
w = scale * (mat(k, j) - mat(j, k));
*q[j] = scale * (mat(j, i) + mat(i, j));
*q[k] = scale * (mat(k, i) + mat(i, k));
}
}

James W. Walker
10-30-2009, 01:59 PM
Be aware that a matrix can include reflections and scaling, while a quaternion can only represent a rotation.

DmitryM
10-30-2009, 02:31 PM
First, why do you guys answer this question?
It's asked in the wrong section and the answer is not difficult to find on the Internet.

To the question itself...
Q and -Q (component-wise) both are equivalents of the orthonormal matrix transformation in a space with a given handness(!).

So, I guess you can find 4 correct algorithms all producing different results (Q from right-handed, Q from left handed, -Q from right, -Q from left).

wh1sp3rik
10-31-2009, 09:16 AM
DmitryM: Because, They are kind ? :)

thank you ;-)