0
\$\begingroup\$

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?

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ In that case I need addition code to manage focus window state, too? \$\endgroup\$ Oct 22, 2019 at 22:27
  • \$\begingroup\$ @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. \$\endgroup\$ Oct 23, 2019 at 9:13
  • \$\begingroup\$ Probably, there is also RawInput and maybe XINPUT, will take a look \$\endgroup\$ Oct 23, 2019 at 10:07
0
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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 \$\endgroup\$ Oct 23, 2019 at 10:06
  • \$\begingroup\$ True, although, highly unlikely. \$\endgroup\$
    – badunius
    Oct 23, 2019 at 12:26

You must log in to answer this question.

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