Sign in to follow this  

Gimbal Lock with Quaternions?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I'm currently trying to write some code that executes a rotation around an arbitrary point in 3d space around two angles (alpha & delta), where alpha is the rotation around the global y axis and delta is the rotation around the global x axis. At first I used this piece of code (point is the point to be rotated, origin the point to rotate about): D3DXMatrixTranslation(&moveToOrigin, -origin.x, -origin.y, -origin.z); D3DXMatrixTranslation(&moveBack, origin.x, origin.y, origin.z); D3DXMatrixRotationY(&yMat, alpha_rad);D3DXMatrixRotationX(&xMat, delta_rad); totalMat = moveToOrigin * xMat * yMat * moveBack; D3DXVec3TransformCoord(&result, &point, &totalMat); The problem was that I got gimbal lock after e.g. rotating 90 degrees around y and then rotating around x. It's obvious that this causes a problem, since when I'm rotating around global x after having rotated 90 degrees around y won't give any useful results. Then I tried: D3DXQUATERNION qX, qY; D3DXQuaternionRotationAxis(&qX, &D3DXVECTOR3(1.0f,0,0), delta_rad); D3DXQuaternionRotationAxis(&qY, &D3DXVECTOR3(0,1.0f,0), alpha_rad); D3DXMatrixAffineTransformation(&totalMat, 1.0f, &origin, &(qX*qY), NULL); D3DXVec3TransformCoord(&result, &point, &totalMat); since you can read everywhere sth. like "using quaternions avoids gimbal lock", but obviously in this example DX handles the rotations the same as in the first one, since my rotation problems remain exactly the same with this piece of code. So how can this problem be solved? Is there an alternative to using global rotation axes? Or how can I get code that properly rotates a point around another given point in 3d space around alpha, delta without any gimbal lock problems? Thanks & greetings in advance. Nico.

Share this post


Link to post
Share on other sites
First off, I am not a DirectX coder, but I have written Quaternion classes.

I wonder why you have two quaternions? Only one should be needed (unless DX is very strange), try something like this:

D3DXQUATERNION q;
D3DXQuaternionRotationAxis(&q, &D3DXVECTOR3(1.0f,0,0), delta_rad);
D3DXQuaternionRotationAxis(&q, &D3DXVECTOR3(0,1.0f,0), alpha_rad);
D3DXMatrixAffineTransformation(&totalMat, 1.0f, &origin, &q, NULL);
D3DXVec3TransformCoord(&result, &point, &totalMat);



But I think there might be more problems in your code snippet, is it really safe to take the address of the temporary (or MACRO expansion) from D3DXVECTOR3?

Hope I have helped,

SwiftCoder

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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