Jump to content
  • Advertisement
Sign in to follow this  
ProjectinMatrix

Quaternion to Matrix

This topic is 2606 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello again everyone. I need help once again. This time i'm trying to convert a Quaternion to a Matrix that i can use in OpenGL ES functions.

I have a quaternion and a matrix class, and use a static function to try and convert the quaternion to a matrix


Matrix Quaternion::ToMatrix(Quaternion& q) {
float sqw = q.w*q.w;
float sqx = q.x*q.x;
float sqy = q.y*q.y;
float sqz = q.z*q.z;
float invs = 1.0f / (sqx + sqy + sqz + sqw);

Matrix matrix = Matrix::identity;

matrix.mat[0] = ( sqx - sqy - sqz + sqw) * invs;
matrix.mat[5] = (-sqx + sqy - sqz + sqw) * invs;
matrix.mat[10] = (-sqx - sqy + sqz + sqw) * invs;

float tmp1 = q.x*q.y;
float tmp2 = q.z*q.w;
matrix.mat[4] = 2.0 * (tmp1 + tmp2) * invs;
matrix.mat[1] = 2.0 * (tmp1 - tmp2) * invs;

tmp1 = q.x*q.z;
tmp2 = q.y*q.w;
matrix.mat[8] = 2.0 * (tmp1 - tmp2) * invs;
matrix.mat[2] = 2.0 * (tmp1 + tmp2) * invs;

tmp1 = q.y*q.z;
tmp2 = q.x*q.w;
matrix.mat[9] = 2.0 * (tmp1 + tmp2) * invs;
matrix.mat[6] = 2.0 * (tmp1 - tmp2) * invs;

return matrix;
}



To test if this works i first create a quaternion using an angle and an axis
Quaternion q = Quaternion::AngleAxis(50.0f, Vector(10.0f, 60.0f, -80.0f));
Then i convert that to a matrix
Matrix toMat = Quaternion::ToMatrix(q);
Next i create a matrix using the same agle axis
Matrix newMat = Matrix::AxisAngleRotation(Vector(10.0f, 60.0f, -80.0f), 50.0f);

Then i loop trough both matrices to print them out, the output of the matrix created from the quaternion is:
0.64 | 0.63 | 0.42 | 0.00
-0.58 | 0.77 | -0.24 | 0.00
-0.48 | -0.09 | 0.86 | 0.00
0.00 | 0.00 | 0.00 | 1.00

Now the output from the matrix created with an angle axis rotation is
36.36 | 275.61 | -239.8 | 0.00
153.04 | 128.61 | -1722.2 | 0.00
-331.73 | -1706.9 | 2286.80 | 0.00
0.00 | 0.00| 0.00 | 1.00

So am i doing something wrong? Or is this just a scaling issue?

Share this post


Link to post
Share on other sites
Advertisement


Now the output from the matrix created with an angle axis rotation is
36.36 | 275.61 | -239.8 | 0.00
153.04 | 128.61 | -1722.2 | 0.00
-331.73 | -1706.9 | 2286.80 | 0.00
0.00 | 0.00| 0.00 | 1.00

So am i doing something wrong? Or is this just a scaling issue?


This is very wrong. Creating a rotation matrix from axis-angle rotations should produce an orthonormal matrix, which is a matrix which has the length of all its column (and row) vectors one, (additionally the column and row vectors are orthogonal). The magnitudes of the columns or rows of this matrix are much greater than one.


Perhaps you passed in an unnormalized direction vector to the code that creates a rotation matrix based off an angle and a axis direction? For reference, here is the code from my library (though currently not used, atm I route axis-angles to quaternions to matrices).

Share this post


Link to post
Share on other sites
wikipedia quaterions




btw it works:




void __fastcall tquaterion::makematrix()
{
if (requiresNormalization() == true) Normalize();
matrix.pointer[0] = w*w+x*x-y*y-z*z;
matrix.pointer[1] = 2.0*x*y-2.0*w*z;
matrix.pointer[2] = 2.0*x*z+2.0*w*y;
matrix.pointer[3] = 0.0;

matrix.pointer[4] = 2.0*x*y+2.0*w*z;
matrix.pointer[5] = w*w-x*x+y*y-z*z;
matrix.pointer[6] = 2.0*y*z+2.0*w*x;
matrix.pointer[7] = 0.0;

matrix.pointer[8] = 2.0*x*z-2.0*w*y;
matrix.pointer[9] = 2.0*y*z-2.0*w*x;
matrix.pointer[10] = w*w-x*x-y*y+z*z;
matrix.pointer[11] = 0.0;

matrix.pointer[12] = 0.0;
matrix.pointer[13] = 0.0;
matrix.pointer[14] = 0.0;
matrix.pointer[15] = 1.0;
}



[/quote]

Share this post


Link to post
Share on other sites
fuuuuu.....
Yeah, i didn't normalize the rotate vector. Added this to the function


Vector nr = rotate.Normalized();
#define x nr.x
#define y nr.y
#define z nr.z


Now it works perfectly. Thanks for pointing it out, i feel beyond dumb.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!