I would like to make a First Person Shooter and move the camera with the mouse. The problem is that when the cursor reaches the limits of the screen, the camera won't turn anymore. How can I keep the mouse centered and detect its movements anyway?


1 Answer 1


The general approach to handling this in classic Win32 programming is to capture the mouse delta each frame, and then reset the mouse position to the center of the screen. You also want to make the mouse cursor invisible, obviously, as otherwise things look ugly.

You can do the same thing in XNA, you just work with a slightly different interface. So you might do something like:

  1. At startup, set the mouse's position to the center of your screen with Mouse.SetPosition.
  2. Also at startup, make the mouse invisible.
  3. Every input frame, grab the current mouse state.
  4. Since you know the mouse was at the center, you can use the current X and Y values from the MouseState object you recovered in step #3 to compute the delta movement in both dimensions. You can feed these deltas to the rest of your game logic, such as (in your case) the camera code.
  5. Reset the mouse to the center of the screen before moving on to the next frame.

EDIT: Also, here is a tutorial I dug up via a Google search for "XNA relative mouse input" (which is what you're asking about). It's a bit old, I think, so some of the methods may have had their names changed, but the concepts should still apply.

EDIT 2: Meant to say "handling this in Win32" and not "handling this is Win32," updated accordingly.

  • \$\begingroup\$ Thank you very much. this is exactly what I was looking for! ;) \$\endgroup\$
    – BlackBear
    Jan 28, 2011 at 17:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .