我似乎無法讓我的相機根據其自身的本地軸進行旋轉。它只會圍繞X和Y旋轉的原點旋轉。Direct X中的相機旋轉11
POINT cursorPos;
GetCursorPos(&cursorPos);
LONG deltaX = oldCursorPos.x - cursorPos.x;
LONG deltaY = oldCursorPos.y - cursorPos.y;
if (GetAsyncKeyState(VK_RBUTTON))
{
XMMATRIX xRotation = XMMatrixRotationY((-deltaX * (float)timer.Delta()));
XMMATRIX yRotation = XMMatrixRotationX((-deltaY * (float)timer.Delta()));
XMFLOAT4 viewVector = XMFLOAT4(sceneMatrix.viewMatrix.m[3][0], sceneMatrix.viewMatrix.m[3][1], sceneMatrix.viewMatrix.m[3][2], sceneMatrix.viewMatrix.m[3][3]);
XMVECTOR pos = XMLoadFloat4(&viewVector);
for (size_t i = 0; i < 4; i++) { sceneMatrix.viewMatrix.m[3][i] = 0.0f; }
XMMATRIX view = XMLoadFloat4x4(&sceneMatrix.viewMatrix);
view = xRotation * view;
view = view * yRotation;
XMStoreFloat4x4(&sceneMatrix.viewMatrix, view);
sceneMatrix.viewMatrix.m[3][0] = XMVectorGetX(pos);
sceneMatrix.viewMatrix.m[3][1] = XMVectorGetY(pos);
sceneMatrix.viewMatrix.m[3][2] = XMVectorGetZ(pos);
sceneMatrix.viewMatrix.m[3][3] = XMVectorGetW(pos);
}
oldCursorPos = cursorPos;
起初我還以爲我是在錯誤的順序乘以他們,但是當我扭轉他們,我仍然旋轉圍繞原點。我無法挑選出我做錯了什麼。
鑑於標量和矢量操作的所有混合,在這裏最好使用[SimpleMath](https://github.com/Microsoft/DirectXTK/wiki/SimpleMath)DirectXMath包裝器[ DirectX工具包](https://github.com/Microsoft/DirectXTK)。 –