optimizing my quaternion code

Started by
0 comments, last by confuted 20 years, 8 months ago
note: this was originally posted on cprogramming.com at this URL: http://cboard.cprogramming.com/showthread.php?s=&postid=303395 However, it seems that nobody on that board is able to answer the question. I got quaternions working correctly now, but the code seems awfully inefficient, even after I cleaned it up quite a bit. What it does: It accepts an amount to change a rotation on the x,y, or z axis. It then generates a temporary quaternion which represents just the new rotation. The temp quaternion is multiplied by the quaternion representing the cumulative effect of all the rotations on every axis. Then a rotation matrix is built from the temp quaternion to rotate the axes, and the axes are rotated with this matrix. (This keeps all the axes in object space) Anyway, it just seems like a lot of computation for each and every rotation. Can some people have a look at my transformation class and see if you can think of a better way to do that?

//ctransformation.h

#ifndef ctransformation_h
#define ctransformation_h

#include "main.h"

/* The CTransformation class is for the switch between
object space and world space.  The X,Y,and Z rotations
that are applied will orient the object in the correct
direction for world space, and the translation will
bring it into world space
*/

class CTransformation
{
	public:
		CTransformation(void);

		void ChangeXRotation(float fDeltaX);
		void ChangeYRotation(float fDeltaY);
		void ChangeZRotation(float fDeltaZ);

		void SetXTranslation(float fNewX); 
		void SetYTranslation(float fNewY); 
		void SetZTranslation(float fNewZ); 

		float ChangeXTranslation(float fDeltaX);
		float ChangeYTranslation(float fDeltaY);
		float ChangeZTranslation(float fDeltaZ);

		D3DXMATRIX GetWorldTransform(void);

	private:
		float fTranslationX,fTranslationY,fTranslationZ;

		D3DXMATRIX matTranslation, matRotation;
		D3DXQUATERNION Quaternion,temp;
		D3DXVECTOR3 xaxis,yaxis,zaxis;
};

#endif

//ctransformation.cpp

#include "main.h"

CTransformation::CTransformation(void)
{
		fTranslationX=0.0f;
		fTranslationY=0.0f;
		fTranslationZ=0.0f;

		D3DXMatrixIdentity( &matTranslation );

		Quaternion.w=1;
		Quaternion.x=0;
		Quaternion.y=0;
		Quaternion.z=0;

		xaxis.x=1;
		xaxis.y=0;
		xaxis.z=0;

		yaxis.x=0;
		yaxis.y=1;
		yaxis.z=0;

		zaxis.x=0;
		zaxis.y=0;
		zaxis.z=1;
}

void CTransformation::ChangeXRotation(float fDeltaX)
{
	Quaternion*=*D3DXQuaternionRotationAxis(&temp,&xaxis,fDeltaX);
	D3DXMatrixRotationQuaternion(&matRotation,&temp);
	D3DXVec3TransformCoord(&yaxis,&yaxis,&matRotation);
	D3DXVec3TransformCoord(&zaxis,&zaxis,&matRotation);
}

void CTransformation::ChangeYRotation(float fDeltaY)
{
	Quaternion*=*D3DXQuaternionRotationAxis(&temp,&yaxis,fDeltaY);
	D3DXMatrixRotationQuaternion(&matRotation,&temp);
	D3DXVec3TransformCoord(&xaxis,&xaxis,&matRotation);
	D3DXVec3TransformCoord(&zaxis,&zaxis,&matRotation);
}

void CTransformation::ChangeZRotation(float fDeltaZ)
{
	Quaternion*=*D3DXQuaternionRotationAxis(&temp,&zaxis,fDeltaZ);
	D3DXMatrixRotationQuaternion(&matRotation,&temp);
	D3DXVec3TransformCoord(&xaxis,&xaxis,&matRotation);
	D3DXVec3TransformCoord(&yaxis,&yaxis,&matRotation);
}

float CTransformation::ChangeXTranslation(float fDeltaX)
{
	fTranslationX+=fDeltaX;
	return fTranslationX;
}

float CTransformation::ChangeYTranslation(float fDeltaY)
{
	fTranslationY+=fDeltaY;
	return fTranslationY;
}

float CTransformation::ChangeZTranslation(float fDeltaZ)
{
	fTranslationZ+=fDeltaZ;
	return fTranslationZ;
}

void CTransformation::SetXTranslation(float fNewX) 
{
	fTranslationX=fNewX; 
}

void CTransformation::SetYTranslation(float fNewY) 
{
	fTranslationY=fNewY; 
}

void CTransformation::SetZTranslation(float fNewZ) 
{
	fTranslationZ=fNewZ; 
}

D3DXMATRIX CTransformation::GetWorldTransform(void)
{
	D3DXMatrixRotationQuaternion(&matRotation,&Quaternion);
	D3DXMatrixTranslation(&matTranslation,fTranslationX,fTranslationY,fTranslationZ);
	return (matRotation*matTranslation);
}
Sorry about the long post, but there doesn''t appear to be a way to attach files here. A compiled version of my project is available here: http://confuted.webhop.org/engine.html
Credendo vives.
Advertisement
Yea, two things.

1. If you change the rotation code to something like this you can get rid of xaxis, yaxis, and zaxis and all the computations associated with them.
void CTransformation:: ChangeXRotation( float fDeltaX ){    D3DQUATERNION local_rotation( sinf( fDeltaX/2 ), 0, 0, cosf( fDeltaX/2 ) );    Quaternion = local_rotation * Quaternion; // Notice that the order was changed here.}  


2. temp, matRotation, matTranslation are temps and should not be member variables -- make them local to the functions that use them.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement