如何将光标锁定到游戏窗口?
我正在尝试为 FPS 游戏实现摄像机移动。我想我已经差不多明白了,但还有一些问题需要解决。我的鼠标移动设置如下:
protected override void OnLoad(EventArgs e)
{
Mouse.Move += OnMouseMove;
}
void OnMouseMove(object sender, MouseMoveEventArgs e)
{
_lookDir.X += e.XDelta * _mouseSensitivity;
_lookDir.Y -= e.YDelta * _mouseSensitivity;
}
当鼠标实际上位于窗口内时,这似乎工作得很好,但一旦我离开窗口,它就不起作用了。我认为我必须做的是以某种方式将鼠标限制在窗口内,因为即使当我的鼠标在窗口外时它触发了鼠标移动,并且仍然遇到相同的问题,就在我桌面的边界处坐标代替。
那么...我该怎么做——将鼠标锁定在窗口内?我基本上只是将鼠标位置设置为中心吗?如果是这样...我如何设置鼠标位置?我使用的是 Windows,但如果 OpenTK 提供的话,我更喜欢非本机解决方案。
I'm trying to implement camera movement for an FPS game. I think I've almost got it, but there's just a few more kinks to work out. I've got my mouse movement set up like this:
protected override void OnLoad(EventArgs e)
{
Mouse.Move += OnMouseMove;
}
void OnMouseMove(object sender, MouseMoveEventArgs e)
{
_lookDir.X += e.XDelta * _mouseSensitivity;
_lookDir.Y -= e.YDelta * _mouseSensitivity;
}
Which seems to work pretty well when the mouse is actually inside the window, but as soon as I leave the window it doesn't work. I think what I have to do is somehow constrain the mouse to be inside the window, because even if it triggered a mouse move when my mouse was outside of the window, and still run into the same issue, just at the bounds on my desktop coords instead.
So...how do I do that -- lock my mouse to inside the window? Do I essentially just keep setting the mouse position to the center? If so...how do I set the mouse position? I'm using Windows, but I'd prefer a non-native solution if OpenTK provides it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 OpenTK 3.2 中非常简单
您还可以使用此代码片段来隐藏光标。只需根据需要实施即可。
Quite simple in OpenTK 3.2
You can additionally use this snippet to hide the cursor. Just implement it as needed.
我做过的一个做法,以及旧的《雷神之锤》游戏(以及《半条命 1》)所使用的做法,是在每一帧中将光标位置重置为屏幕中间,然后从屏幕中间计算增量。
编辑:要移动光标,请查看 光标.位置
One practice that I have done, and that the old Quake games used (and half-life 1), is every frame you reset the cursor position to be the middle of the screen, then you calculate the delta from the middle of the screen.
EDIT: And to move the cursor, check out Cursor.Position
这是我现在的解决方案...但它特定于 Windows。
This is my solution right now...it's specific to windows though.