I've been trying to figure this out all day and I know that there quite some ressources on that rotation/quaternion stuff out there but I still couldn't figure out the problem. I'm creating an editor in DirectX 11 and are currently doing a camera that can be rotated and moved around using the mouse. Moving the camera works fine. For the rotation I want the user to be able to hold down the right mouse button and perform a free look around. In the code, I chose to use quaternions as I read that they would have quite some advantages compared to other rotation techniques. My code almost works as it should but, although the expression "input.m_rotation.z * ROTATION_SPEED.z" in the code below currently always evaluates to 0, there is still some roll occurring when I rotate the camera. I can't figure out where the problem lies, so I hope you have an idea and can show me what I'm missing or doing wrong. I don't really have experience in using quaternions, so if you can share some knowledge on how to improve the code below in general or on alternate implementations, that would be great, too.
(the "input" variable in the code contains the delta movement/rotation to be applied for the current frame based on user input)
XMVECTOR pitchQuaternion = XMQuaternionRotationAxis(XMLoadFloat3( &m_right ), XMConvertToRadians( input.m_rotation.x * ROTATION_SPEED.x)); XMVECTOR yawQuaternion = XMQuaternionRotationAxis(XMLoadFloat3( &m_up ), XMConvertToRadians( input.m_rotation.y * ROTATION_SPEED.y)); XMVECTOR rollQuaternion = XMQuaternionRotationAxis(XMLoadFloat3( &m_lookAt ), XMConvertToRadians( input.m_rotation.z * ROTATION_SPEED.z)); XMVECTOR rotationQuaternion = XMQuaternionMultiply(yawQuaternion, pitchQuaternion); rotationQuaternion = XMQuaternionMultiply(rotationQuaternion, rollQuaternion); XMMATRIX matTranslation = XMMatrixTranslationFromVector(-XMLoadFloat3(&input.m_movement) * XMLoadFloat3(&MOVEMENT_SPEED)); XMMATRIX matRotation = XMMatrixRotationQuaternion(XMQuaternionConjugate(rotationQuaternion)); XMMATRIX matTransform = XMMatrixMultiply(matTranslation, matRotation); XMStoreFloat4x4(&m_viewMatrix, XMMatrixMultiply(XMLoadFloat4x4(&m_viewMatrix), matTransform));
If you need more information, please ask. Thank you very much in advance for your effort!