Advertisement

First Person Camera SDL2 problem SDL_WarpMouseInWindow

Started by February 27, 2018 03:14 AM
1 comment, last by swiftcoder 6 years, 9 months ago

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

i have also tried relative mouse movement,  and .xrel and .yrel to avail. polling mouse state with sdl_event. I do also know that SDL_WarpMouseInWindow makes change to event and have tried also ignore and reenabling to no avail

SDL already has this functionality built-in. You are meant to call SDL_SetRelativeMouseMode(true) to turn it on.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement