我正在尝试转换我在屏幕上单击的点以获取我单击的位置的光线方向。最终,这将用于某种交集。我现在的方向似乎产生了奇怪的结果,我不知道为什么。
projectionMatrix = XMLoadFloat4x4(this->projectionMatrix);
viewMatrix = XMLoadFloat4x4(this->viewMatrix);
pointX = ((2.0f * (float)mouseX) / (float)screenWidth) - 1.0f;
pointY = (((2.0f * (float)mouseY) / (float)screenHeight) - 1.0f) * -1.0f;
pointX = pointX / XMVectorGetX(projectionMatrix.r[0]);
pointY = pointY / XMVectorGetY(projectionMatrix.r[1]);
inverseViewMatrix = XMMatrixInverse(nullptr, viewMatrix);
direction =
XMVectorSet((pointX * XMVectorGetX(inverseViewMatrix.r[0])) + (pointY * XMVectorGetX(inverseViewMatrix.r[1])) + XMVectorGetX(inverseViewMatrix.r[2]),
(pointX * XMVectorGetY(inverseViewMatrix.r[0])) + (pointY * XMVectorGetY(inverseViewMatrix.r[1])) + XMVectorGetY(inverseViewMatrix.r[2]),
(pointX * XMVectorGetZ(inverseViewMatrix.r[0])) + (pointY * XMVectorGetZ(inverseViewMatrix.r[1])) + XMVectorGetZ(inverseViewMatrix.r[2]), 0.0f);
这在某个角度似乎可以正常工作,但是一旦达到某个点,它就会停止旋转并产生“意外”的结果。也许问题在于 Z 值,因为这是我无法面对的方向?
在视频示例中,我让相机查看方向(仅围绕世界 Y 轴旋转),并且方向基于鼠标移动。如您所见,它似乎在一个区域上运行良好,但一旦达到某一点,它将开始向后移动并停止跟随鼠标。
https://i.gyazo.com/c0e1cd6bc5c36a7a488ba81928061104.mp4
有谁知道是什么原因造成的?
编辑:已解决,问题是我使用 acos 旋转相机,它给了我 0-180 度的自由度,而不是 atan 0-360