I've been trying to build a 3D camera system in SDL2 with OpenGL so that I can use the mouse to look around. With glfw it's possible to trap the mouse in the window, and keep it centered with a library call, so I was looking for something equivalent in SDL2.
In order to process mouse events in the main loop I'm checking for SDL_MOUSEMOTION
.
Searching around the web, I've found that SDL2 allows for mouse trapping with the call to SDL_SetRelativeMouseMode()
. With this call, though, the mouse is trapped and limited to move within the window, and not just at the center. This causes the mouse to be able to move from the top left corner at (0,0)
to the bottom right corner at (WIDTH,HEIGHT)
, and not any further.
I've tried to fix this by repositioning the cursor position with SDL_WarpMouseInWindow()
, but this generates a SDL_MOUSEMOTION
event as well, basically resetting any movement I might've tried to do.
How can I fix this?
SDL_SetRelativeMouseMode
) don't work for OP. \$\endgroup\$ – Tyyppi_77 Apr 4 '17 at 9:08xrel
andyrel
even when the mouse is at the border of the window whenSDL_SetRelativeMouseMode
is set to true. Maybe you're using the mouse positions to calculate velocity instead? So, for me the solutions in the two linked questions work just fine. \$\endgroup\$ – Tyyppi_77 Apr 4 '17 at 9:10SDL_ShowCursor(0); SDL_SetRelativeMouseMode(SDL_TRUE);
Should do the trick. \$\endgroup\$ – rioki Apr 4 '17 at 9:35