Direct X 11中的摄像机旋转

问题描述 投票:0回答:2

我似乎无法让相机根据其自身的本地轴旋转。它只会绕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绕原点旋转。我无法找出我在做什么错。

camera rotation directx-11
2个回答
1
投票
您所需的局部旋转轴已内置在视图矩阵中……。视图矩阵是矩阵的倒置形式,代表相机的世界空间位置和旋转。首先,您必须反转视图矩阵,然后摄像机的本地x轴为第一行,y轴为第二行,z轴为第三行。(或者您可以通过使用适当的列来提取反转矩阵来避免反转过程局部向量)。然后,使用轴/角度版本而不是XMMatrixRotationX,您可以在其中插入那些局部轴:XMMatrixRotationAxisXMMatrixRotationNormal进行旋转。

1
投票
这就是旋转矩阵的工作原理。它们围绕原点旋转空间中的每个点。

RotationMatrix

(来源:sharetechnote.com

例如,要使相机绕y轴旋转,则应将x = 0&z = 0设置为零。然后应用旋转矩阵(现在它在寻找其他位置),然后将相机移回其原始位置。

另一种选择是创建一个视图矩阵来为您模拟整个相机,并在每帧左右更改它。

© www.soinside.com 2019 - 2024. All rights reserved.