我似乎无法让相机根据其自身的本地轴旋转。它只会绕X和Y旋转绕原点旋转。
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;
起初我以为我是以错误的顺序乘以它们,但是当我反转它们时,我[[still绕原点旋转。我无法找出我在做什么错。
XMMatrixRotationX
,您可以在其中插入那些局部轴:XMMatrixRotationAxis
或XMMatrixRotationNormal
进行旋转。(来源:sharetechnote.com)
例如,要使相机绕y轴旋转,则应将x = 0&z = 0设置为零。然后应用旋转矩阵(现在它在寻找其他位置),然后将相机移回其原始位置。另一种选择是创建一个视图矩阵来为您模拟整个相机,并在每帧左右更改它。