# Quaternion Rotation

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

## 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 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 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.

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 14
• 9
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
632915
• Total Posts
3009197
• ### Who's Online (See full list)

There are no registered users currently online

×