I am using OpenGL and I try to trap my cursor inside my game window (using both SetCursorPos and ClipCursor)

But, these conflict with my camera rotation code as my camera is rotated with my mouse. If there is a way to do it, please let me know. If possible, I would be willing to make it so that when the cursor reaches an edge of the screen, it jumps to the opposite edge (though I fear that would also conflict with my camera code).

EDIT: My camera code:

float heading;              // Also Y rotation, hard to explain
GLfloat yrot;               // Y Rotation
GLfloat lookupdown = 0.0f;
float mouseX, mouseY, oldMouseX, oldMouseY;

    case WM_MOUSEMOVE:
        // save old mouse coordinates
        oldMouseX = mouseX;
        oldMouseY = mouseY;

        // get mouse coordinates from Windows
        mouseX = LOWORD(lParam);
        mouseY = HIWORD(lParam);

        // these lines limit the camera's range
        if (lookupdown < -90)
            lookupdown = -90;
        if (lookupdown > 90)
            lookupdown = 90;

        if ((mouseX - oldMouseX) > 0) {     // mouse moved to the right
            heading -= 3.0f;
            yrot = heading;
        else if ((mouseX - oldMouseX) < 0) {     // mouse moved to the left
            heading += 3.0f;
            yrot = heading;

        if ((mouseY - oldMouseY) > 0) {     // mouse moved up
            lookupdown += 3.0f;
        else if ((mouseY - oldMouseY) < 0) {     // mouse moved down
            lookupdown -= 3.0f;
  • 2
    \$\begingroup\$ Set it back to the center of the window every frame. \$\endgroup\$ – Ben Aug 23 '14 at 5:33
  • \$\begingroup\$ What Ben said. And use the delta of the movement, not the absolute position. \$\endgroup\$ – rootlocus Aug 23 '14 at 10:24
  • 1
    \$\begingroup\$ Neither C++ nor OpenGL provide facilities to control the cursor. You are presumably using some other library to do so, and what library that is will matter to your question. (From the two functions you mention, I assume that it's the Windows API?) \$\endgroup\$ – Trevor Powell Aug 23 '14 at 11:40
  • \$\begingroup\$ @Ben, if I set it back to the center of the window every frame (which I tried already), it prevents my camera from rotating. I don't know why, it just does. I can update the question with my camera code if needed. \$\endgroup\$ – ezio160324 Aug 23 '14 at 12:04
  • \$\begingroup\$ @TrevorPowell I am just using the Windows API to reset the mouse position (as seen in the question when I said I tried SetCursorPos and ClipCursor). \$\endgroup\$ – ezio160324 Aug 23 '14 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.