使用供过于求来防止鼠标离开窗口

Using glut to prevent the mouse from leaving the window

本文关键字:鼠标 离开 开窗口 供过于求      更新时间:2023-10-16

我现在正在使用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);
    } 
}

希望这能有所帮助!