WM_MOUSEMOVE 不适用于 Direct3D 中的 FPS 相机实现

发布于 2024-10-21 01:50:59 字数 533 浏览 6 评论 0原文

嘿伙计们, 我正在尝试实现 FPS=Style 相机。鼠标移动正常,但甚至没有触摸鼠标。相机在各个角度运转,我什至没有碰鼠标。基本上,在鼠标本身不移动的情况下,偏航和俯仰从鼠标获取错误的值。 这是 win32 循环的代码

    case WM_MOUSEMOVE:
        gCamera->Yaw() = (float)LOWORD(lparam);
        gCamera->Pitch() = (float)HIWORD(lparam);
        break;

Yaw 和 Pitch 方法基本上返回对数据成员 mPitch 和 mYaw 的引用,通过它们,我对基本向量(右、上和看向量)进行旋转

只是为了澄清,我WM_MOUSEMOVE 正在获取输入(我通过调试进行了检查),但它的值非常高且非常错误,因为我什至没有移动鼠标,而且因为相机在各个方向旋转,就像它刚刚吃了一些火箭燃料一样。

PS:我必须对这些值进行类型转换,因为我使用偏航和俯仰来创建矩阵,我必须使用浮点数。

感谢大家的帮助

hey guys,
i am trying to implement an FPS=Style camera. The mouse movement is working but without even touching the mouse. The camera is going on all degrees without me even touching the mouse. Basically, the yaw and the pitch are getting wrong values from the mouse without the movement of the mouse itself.
here is the code for the win32 loop

    case WM_MOUSEMOVE:
        gCamera->Yaw() = (float)LOWORD(lparam);
        gCamera->Pitch() = (float)HIWORD(lparam);
        break;

the Yaw and Pitch methods basically return a reference to the data members mPitch and mYaw, and through them, i do the rotations for the basis vectors(right, up and look vectors)

Just to clarify, i WM_MOUSEMOVE is getting input(i checked through debugging), but it is getting very high and very wrong values because i am not even moving the mouse and because the camera is rotating in every direction like it just ate some rocket fuel.

P.S: i had to typecast the values because i am using the Yaw and the Pitch to create matrices, i have to use floats.

Appreciate the help, guys

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻的心爱 2024-10-28 01:50:59

请记住您的单位。 WM_MOUSEMOVE lparam x & y 值采用逻辑(屏幕像素)坐标,但游戏中的大多数旋转值均以度数或弧度表示。例如,如果鼠标位于<400, 300>,则鼠标位于<400, 300>处。但是您的相机类需要弧度,那么您将在变换数学中将大量旋转与其他一些(可能变化的)数字相乘,即使您没有移动鼠标,也可能导致疯狂的移动。这种情况下的解决方案是使用您选择的比例因子将逻辑单位转换为弧度或度数。

回应进一步的评论
思考这个问题的一种方法是问自己一个问题:您想要多少个逻辑移动单位(通过鼠标)对应于 360 度旋转?

例如,如果您决定希望鼠标在窗口整个宽度上的移动对应于 360 度,则数学关系为

screenW * scaleFactor = 2 * PI

求解scaleFactor,然后将其应用于未来鼠标值使用:

mouseX * scaleFactor =orientationInRadians

请记住,这种方法会将绝对鼠标位置链接到绝对相机方向(至少一个自由度),因此您可能希望跟踪鼠标位置,而不是绝对鼠标位置;然后计算方向(弧度)的变化以应用于现有方向。可以使用相同的公式将 delta(变化量)从逻辑坐标转换为弧度。

Keep in mind your units. The WM_MOUSEMOVE lparam x & y values are in logical (screen pixel) coordinates, but most rotation values in games are expected in terms of degrees or radians. For instance, if the mouse is at say <400, 300> but your camera class expects radians, then you're multiplying a large number of rotations against some other (potentially varying) numbers in your transform math, potentially leading to crazy movement even though you're not moving the mouse. The solution in such a case is to convert your logical units into radians or degrees, using a scale factor of your choosing.

In response to further comments:
One way to think about it is to ask yourself the question: how many logical units of movement (by the mouse) do you want to correspond to 360 degrees of rotation?

For instance, if you decided you wanted mouse movement across the full width of the window to correspond to 360 degrees, then then mathematical relationship is

screenW * scaleFactor = 2 * PI

Solve for scaleFactor then apply it to future mouse values using:

mouseX * scaleFactor = orientationInRadians

Keep in mind, this approach would link an absolute mouse location to an absolute camera orientation (for at least one DOF), so you may instead want to track changes in mouse position, rather than absolute mouse position; and then calculate changes in orientation (radians) to apply to existing orientation. The same formula can be used to convert the delta (change amount) from logical coords to radians.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文