Advertisement

Quaternion to rotation matrix...

Started by April 27, 2003 03:04 AM
7 comments, last by James Trotter 21 years, 9 months ago
I''m trying to convert a quaternion to rotation matrix, but it isn''t working... I got this from the Matrix & Quaternion FAQ... What am I doing wrong?
  
void Matrix::setRotationQuaternion(const Quaternion q) {		// Creates a rotation matrix from a quaternion

	float xx = q.x * q.x;
	float xy = q.x * q.y;
	float xz = q.x * q.z;
	float xw = q.x * q.w;

	float yy = q.y * q.y;
	float yz = q.y * q.z;
	float yw = q.y * q.w;

	float zz = q.z * q.z;
	float zw = q.z * q.w;

	M[0] = 1.0f - 2.0f * (yy + zz);
	M[1] = 2 * (xy - zw);
	M[2] = 2 * (xz + yw);

	M[4] = 2 * (xy + zw);
	M[5] = 1 - 2 * (xx + zz);
	M[6] = 2 * (yz - xw);

	M[8] = 2 * (xz - yw);
	M[9] = 2 * (yz + xw);
	M[10] = 1 - 2 * (xx + yy);

	M[3] = M[7] = M[11] = M[12] = M[13] = M[14] = 0;
	M[15] = 1;
}
  
Thanks!!!!
Maybe your quaternion should be normalized before this
(x2+y2+z2+w2 = 1)
Advertisement
Didn''t work... I welcome any suggestoins at all!
In what way is it ''wrong''?

Did you try transposing the resulting matrix?
Are your quaternion components in the same order theirs are? Are your matrices of the same form(both row major or col major)?
quote:
Original post by Tramboi
Maybe your quaternion should be normalized before this
(x2+y2+z2+w2 = 1)



Normalized means the magnitude is equal to 1, not the sum of the components
Advertisement
The 2 was supposed to be a square
The usual reference:

www.magic-software.com

Look in source code->Documentation for some very helpful PDF files.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
I'm not transposing the matrix, should I? It didn't say anything about that in the FAQ... I'm using the Matrix & Quaternion FAQ at http://www.j3d.org/matrix_faq...

Oh, and it is wrong in the way that the rotation does not appear... I'm trying to animate a 3D model.

Thank you for your help!

[edited by - James Trotter on April 28, 2003 4:40:38 PM]

This topic is closed to new replies.

Advertisement