Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
This is a great question. – The Communist Duck Jan 30 '11 at 12:58
@The Communist Duck: thanks. ;) – BlackBear Jan 30 '11 at 17:59
up vote 37 down vote accepted

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.

share|improve this answer
Thank you very much. this is exactly what I was looking for! ;) – BlackBear Jan 28 '11 at 17:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.