Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Demystifying Quaternion-to-Matrix conversion.

Started by Khaosifix Jan 21, 2005 at 5:33 AM 0 replies 1.5k views
Original Post
Khaosifix
Khaosifix
Hello, Thanks for the help of several websites I'm near completion of my quaternion class. I have one function in there(GLvoid P3DXQUATERNION::CreateMatrix(P3DXMATRIX* p_mParam); ) that takes a matrix as a function argument and using the Q-to-M formula[as I call it] I convert the quaternion, which contains euler angles, into a 4x4 column major homogeneous matrix. Although I have the function completed I'm still left in the dark trying to figure out the actual process :

void P3DXQUATERNION::ConvertAxisAngle( SCALAR CONST& p_Degree, SCALAR CONST& p_X, SCALAR CONST& p_Y, SCALAR CONST& p_Z )
{

	SCALAR fRadian = DEGTORAD( p_Degree ),
		fSine = sinf( fRadian / 2.0f ) ;

	// w = cos( theta / 2 )
	// x = X * sin( theta / 2 )
	// y = Y * sin( theta / 2 )
	// z = Z * sin( theta / 2 )

	w = cosf( fRadian / 2.0f ) ;
	x = p_X * fSine ;
	y = p_Y * fSine ;
	z = p_Z * fSine ;
}

// ---------------------------------------------------------------------------------------------------

void P3DXQUATERNION::CreateMatrix( P3DXMATRIX* p_mParam )
{

	p_mParam->m11 = 1.0f - 2.0f * ( y*y + z*z )    ;
	p_mParam->m12 = 2.0f * ( x * y + z * w )       ;
	p_mParam->m13 = 2.0f * ( x * z - y * w )       ;
	p_mParam->m14 = 0.0f                           ;

	p_mParam->m21 = 2.0f * ( x * y - z * w )       ;
	p_mParam->m22 = 1.0f - 2.0f * ( x*x + z*z )    ;
	p_mParam->m23 = 2.0f * ( z * y + x * w )       ;
	p_mParam->m24 = 0.0f                           ;

	p_mParam->m31 = 2.0f * ( x * z + y * w )       ;
	p_mParam->m32 = 2.0f * ( y * z - x * w )       ;
	p_mParam->m33 = 1.0f - 2.0f * ( x*x + y*y )    ;
	p_mParam->m34 = 0.0f                           ;

	p_mParam->m41 = 0.0f                           ;
	p_mParam->m42 = 0.0f                           ;
	p_mParam->m43 = 0.0f                           ;
	p_mParam->m44 = 1.0f                           ;
}

// ---------------------------------------------------------------------------------------------------



Know of a website that demystifies this? Also how comes I have to declare three different quaternions just to represent my camera's orientation[along with a translation matrix] ? Before I introduced myself to quaternions I used four different matrices to make a camera. Three rotational matrices where each handles rotation around the x-, y-, and z-axises respectively. Declaring three quaternions, setting the euler angles, and converting each to a matrix is the same as my previous method. How do quaternions simplify creating a camera's orientation? [Edit:] Alright they're not euler angles, they're axis-angle values. But you get the point....I hope. [Edited by - Khaosifix on January 21, 2005 6:08:39 AM]
---http://www.michaelbolton.comI constantly dream about Michael Bolton.
JohnBolton
JohnBolton
Quote:
Original post by Khaosifix
Know of a website that demystifies this?
Just google for "quaternion to rotation matrix conversion" and you will find hundreds...
Quote:
Original post by Khaosifix
... How do quaternions simplify creating a camera's orientation? ...

You only use one quaternion to represent an orientation. One quaternion for each axis is silly. Anyway, a quaternion is one method for representing an orientation. There are advantages and disadvantages to using quaterions, and whether or not it simplifies anything depends on how it is used and how you look at it.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.