Sign in to follow this  
Matt328

Quaternion Rotation

Recommended Posts

I have the following rotate function that will rotate my camera by a given axis, and an angle.
void Camera::Rotate(D3DXVECTOR3 axis, float angle)
{
	D3DXVECTOR4 newAxis = D3DXVECTOR4(axis, 1.0f);
	D3DXMATRIX rotationMatrix;

	D3DXMatrixRotationQuaternion(&rotationMatrix, &this->rotation);

	D3DXVec3Transform(&newAxis, &axis, &rotationMatrix);

	D3DXQUATERNION quaternionFromAxisAngle;
	D3DXQuaternionRotationAxis(&quaternionFromAxisAngle, &D3DXVECTOR3(newAxis.x, newAxis.y, newAxis.z), angle);
	D3DXQUATERNION rotationTimesAxisAngle;

	D3DXQuaternionMultiply(&rotationTimesAxisAngle, &this->rotation, &quaternionFromAxisAngle);

	D3DXQuaternionNormalize(&this->rotation, &rotationTimesAxisAngle);
}
In debug mode it works just fine, but when I compile in the release configuration, I get negative infinity and such in my rotation quaternion. (this->rotation is defined as a D3DXQUATERNION). I understand that in release mode, often times the compiler won't initialize variables as it does in debug mode, but I don't see anything like that going on here. Does anyone with more insight into the D3DX math operations I'm using see anything blatantly wrong here? Also, this code was first prototyped in C#/XNA, so if there are any optimizations or more appropriate DirectX-ey things I should be doing, I'd appreciate the comments.

Share this post


Link to post
Share on other sites
I'm not sure why you're getting such strange results, but you're definitely doing things in a bit of a roundabout way here. If you want to to rotate an existing quaternion about an arbitrary axis, just make a new quaternion or rotation matrix using D3DXQuaternionRotationAxis or D3DXMatrixRotationAxis and then multiply that with the existing rotation quaternion. If you multiply new * old, the rotation will be with respect to the camera's local axes. If you multiply old * new, it will be with respect to the global world axes.


void Camera::Rotate(D3DXVECTOR3 axis, float angle)
{
D3DXQUATERNION newRotation;
D3DXQuaternionRotationAxis(&newRotation, &axis, angle);
D3DXQuaternionMultiply(&this->rotation, &this->rotation, &newRotation);
}


By the way if you want to transform a D3DXVECTOR3 by a transformation matrix, you can use D3DXVec3TransformCoord. This will give you the result as a D3DXVECTOR3, not a D3DXVECTOR4.

Share this post


Link to post
Share on other sites
Thanks for the help with refactoring there, definitely helped simplify things.

That changed a few things, and got me looking, and it turned out the values I was passing in for the angle was garbage that came from an uninitialized structure from when the application first starts up, so I guess the moral of the story here is to always initialize everything.

I do find it odd though that Visual Studio apparently initializes things for you since I could not duplicate the bug when launching from the IDE, only when going out and firing up the executable.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this