使用供过于求来防止鼠标离开窗口
Using glut to prevent the mouse from leaving the window
我现在正在使用glut进行游戏,我正试图将鼠标放在窗口内。这不是第一人称射击,所以把它锁在中间是不好的。我已经知道glutWarpPointer(int, int);
了,我已经尝试了一些有效的方法。
我试过让鼠标在离开时弯曲回窗口的最近边缘,这很有效,但在那一瞬间,你会看到鼠标在窗口外,然后传送回窗口。我不希望这样,我希望看起来鼠标只是碰到了窗口的边缘,不再朝那个方向移动,同时保持向任何其他可用方向移动。就像你所期望的那样。
这不是你问题的答案,但它是你问题的回答!
几乎每个游戏都有自己的光标。他们会隐藏鼠标,并手动将光标绘制在鼠标应该定位的位置。
如果你得到自己的光标图像,并按照我说的做,你可以简单地在屏幕边缘绘制光标,即使鼠标位置超出了边界。然后你可以将鼠标向后弯曲。
试图搜索并找出答案,但找不到答案,所以我自己实现了它。以下是我的第一人称相机外壳的工作原理:
来自glutPassiveMotion
的回调
代码样本
void Game::passiveMouseMotion(int x, int y)
{
//of my code for doing the cam, yours is may be different, this is based on the example from https://learnopengl.com/Getting-started/Camera
if (firstMouse) {
lastX = x;
lastY = y;
firstMouse = false;
}
float xoffset = x - lastX;
float yoffset = lastY - y; // reversed since y-coordinates go from bottom to top
lastX = x;
lastY = y;
camera->ProcessMouseMovement(xoffset, yoffset);
glutPostRedisplay();
//this is the main thing that keeps it from leaving the screen
if ( x < 100 || x > win_w - 100 ) { //you can use values other than 100 for the screen edges if you like, kind of seems to depend on your mouse sensitivity for what ends up working best
lastX = win_w/2; //centers the last known position, this way there isn't an odd jump with your cam as it resets
lastY = win_h/2;
glutWarpPointer(win_w/2, win_h/2); //centers the cursor
} else if (y < 100 || y > win_h - 100) {
lastX = win_w/2;
lastY = win_h/2;
glutWarpPointer(win_w/2, win_h/2);
}
}
希望这能有所帮助!
相关文章:
- WinAPI挂钩鼠标
- 将鼠标悬停在问题上时与预期">"相关的代码错误
- 跟踪滚动条上的鼠标事件
- SendInput()鼠标移动计算
- 为什么我的点没有在 OpenGL 中绘制鼠标所在的位置?
- 如何在鼠标挂钩过程中检测拖动
- 在函数范围内在堆栈上分配的数组在离开函数时是否总是被释放?
- Win32 发送输入鼠标移动滞后并冻结
- 将鼠标和键盘输入发送到 unity3d 游戏 (Rust)
- 有没有办法区分Qt小部件是通过鼠标单击还是通过按表键获得焦点?
- 在 macOS 中使用 C/C++ 设置鼠标光标图像
- 如何通过拖动鼠标QT检查按钮?鼠标移动事件
- SDL2 调整窗口大小后如何缩放鼠标坐标?
- 鼠标离开 GLFW窗口后触发 GLFW 鼠标回调?
- ncurses检测鼠标何时离开窗口
- MFC鼠标移动/离开,悬停在按钮上
- 使用供过于求来防止鼠标离开窗口
- Qt QItemDelegate提交数据并在鼠标上关闭编辑器,离开视图小部件(listView)
- Qt鼠标离开事件,而按下按钮
- 鼠标悬停时改变图片框,鼠标离开时重置