# FPS-like mouse-look camera WinAPI problem

I have a problem with implementing mouse-look camera movement, like in FPS games. For me common solution is:

1. Process WM_MOUSEMOVE event in WndProc
2. Calculate delta movement from the window's center using event's lParam
3. Rotate camera
4. Return cursor back to window's center using SetCursorPos

The problem is when SetCursorPos is called, another WM_MOUSEMOVE event is being fired. So camera rotates back.

What is the common way to create such type of camera on Windows platform (using WinAPI)?

I know that in WM_MOSEMOVE I can check is mouse.x == windowCenter.x and if it is - do nothing, but it's a hack from my point of view. Is there any "non-hacky" way to achieve the goal?

## 2 Answers

You can use GetCursorPos, and use the result from that to calculate how far the mouse has moved from the center, then SetCursorPos to put it back. With this scheme you don't even need to handle WM_MOUSEMOVE messages; just call GetCursorPos each frame.

This, IIRC, is the approach used by Quake and derivatives.

• In that case I need addition code to manage focus window state, too? – Bohdan Bessonov Oct 22 '19 at 22:27
• @BohdanBessonov - correct, but you're probably going to need similar for showing/hiding the cursor as well, so it's not really that big a deal. The other alternative is to use DirectInput - despite it being deprecated - which will handle all of this automatically for you. – Maximus Minimus Oct 23 '19 at 9:13
• Probably, there is also RawInput and maybe XINPUT, will take a look – Bohdan Bessonov Oct 23 '19 at 10:07

You can use some sort of flag to skip WM_MOUSEMOVE handling whenever you are adjusting cursor position. That may seem a bit hacky too, but that's how usually this gets done.

• It does not work since there can be some real mouse events in the queue before the SetCursosPos's event is processed, in that case wrong event will be discarded by a flag – Bohdan Bessonov Oct 23 '19 at 10:06
• True, although, highly unlikely. – badunius Oct 23 '19 at 12:26