Skip to main content
1 of 5
user
  • 31
  • 4

How do I change quaternion rotation to use the local camera axis rather than the world axis in DirectX 11?

I'm currently trying to rotate the camera around its local axis based on keyboard/mouse input and the code I currently have uses DirectXMath and works nicely, however it is using the world axis to rotate around rather than the cameras local axis. Because of this, some of the rotations are not as expected and causes issues as the camera rotates. For example, when we tilt our camera, the Y axis will change and we will want to rotate around another axis to get our expected results.

What am I doing wrong in the code or what do I need to change in order to rotate around its local axis?

XMVECTOR viewQuaternion = XMQuaternionIdentity(); viewQuaternion = XMVectorSet(lookat.x, lookat.y, lookat.z, 0.0f); XMVECTOR rotationVector = XMVectorSet(vector.x, vector.y, vector.z, 0.0f); XMVECTOR rotationQuaternion = XMVectorSet( XMVectorGetX(rotationVector) * sin(theta / 2), XMVectorGetY(rotationVector) * sin(theta / 2), XMVectorGetZ(rotationVector) * sin(theta / 2), cos(theta / 2)); XMVECTOR rotationInverse = XMQuaternionInverse(rotationQuaternion); XMVECTOR newViewQuaternion = XMQuaternionMultiply(rotationQuaternion, viewQuaternion); newViewQuaternion = XMQuaternionMultiply(newViewQuaternion, rotationInverse); lookat = XMFLOAT3(XMVectorGetX(newViewQuaternion), XMVectorGetY(newViewQuaternion), XMVectorGetZ(newViewQuaternion)); 

This new lookat value is plugged into the camera before the camera matrix is built

XMStoreFloat4x4(&cameraMatrix, XMMatrixLookAtLH( XMVectorSet(position.x, position.y, position.z, 0.0f), XMVectorSet(lookat.x, lookat.y, lookat.z, 0.0f), XMVectorSet(up.x, up.y, up.z, 0.0f))); 
user
  • 31
  • 4