How can I change the position of the mouse cursor in OpenGL/Glut?

KingNestor picture KingNestor · Apr 24, 2009 · Viewed 11.2k times · Source

I'm writing a simple game and I'm going to have the mouse control the camera (using GlutPassiveMotionFunc).

I'm going to pitch and yaw based off the mouse difference between callbacks, however I think it would be a good idea to "force" the mouse back to the center of the screen every time they tried to move it. This way their cursor won't be at the edge of the screen and they can't move any further in that direction.

What Glut / OpenGL command can I use to force the position of the mouse to change?

Answer

hbw picture hbw · Apr 24, 2009

Use glutWarpPointer(x, y), where x and y (both ints) are in pixels (relative to the window's origin). For example:

glutWarpPointer(windowWidth / 2, windowHeight / 2);