我如何获得在世界坐标(DX11,C++)中点击屏幕的光线方向
问题描述:
我试图转换点击屏幕上的点以获得光线方向,点击。最终这将用于某种交叉点。我现在的方向似乎产生了奇怪的结果,我不知道为什么。我如何获得在世界坐标(DX11,C++)中点击屏幕的光线方向
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
有谁知道是什么原因造成的?
编辑:解决了,问题是用这给了我0-180自由度,而不是反正切ACOS我的相机旋转0-360
答
的问题是使用这给了我0-180自由度ACOS我的相机旋转而不是使用atan