First Person Camera, mouse movement SDL2/OpenGL

I am somewhat new to game development and trying to create a basic 3d engine. I have managed to set up a first person camera and it seems to be working fine for the most part. While I am able to look up, down, left and right just fine the camera is constrained to the mouse movement in the window (i.e when the mouse reaches edges of the window it discontinues camera rotation and mouse is out of window bounds. I tried to use SDL_WarpMouseInWindow(window, center.x,center.y) but when I do this then it messes up the camera and the camera is stuck, even though there is some slight movement of the camera, it keeps going back to the center.

void Camera::UpdateViewByMouse(SDL_Window &window, glm::vec2 mousePosition)
{
float xDistanceFromWindowCenter = mousePosition.x - ((float)1024 / 2) ;
float yDistanceFromWindowCenter = ((float)720 / 2) - mousePosition.y;
yaw = xDistanceFromWindowCenter * cameraRotationSpeed;
pitch = yDistanceFromWindowCenter * cameraRotationSpeed;

SDL_WarpMouseInWindow(&window, 1024 / 2, 768 / 2); 

}

i’ve been stuck on this for far too long. any help would be much appreciated

You want relative mouse movement, see https://wiki.libsdl.org/SDL_SetRelativeMouseMode and .xrel and .yrel in https://wiki.libsdl.org/SDL_MouseMotionEvent

1 Like

thanks daniel, I got it working using setrelativemousemode and .xrel and .yrel

1 Like

Is this the function you’re after:

https://wiki.libsdl.org/SDL_CaptureMouse