# Gimbal Lock with Quaternions?

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

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627666
• Total Posts
2978532

• 10
• 10
• 12
• 22
• 13