# Gimbal Lock with Quaternions?

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

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

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 16
• 18
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633362
• Total Posts
3011526
• ### Who's Online (See full list)

There are no registered users currently online

×