From 69cca0d308216861d678d30ae9a3c51ab14aca6a Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Tue, 24 Sep 2024 00:40:34 +0100 Subject: [PATCH] Attempt to fix first person camera implementation --- src/main.cc | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main.cc b/src/main.cc index 484ab16..dca429b 100644 --- a/src/main.cc +++ b/src/main.cc @@ -25,8 +25,10 @@ #include #include -#define WINDOW_WIDTH 1280 -#define WINDOW_HEIGHT 720 +#define WINDOW_WIDTH 1280 +#define WINDOW_HEIGHT 720 +#define WINDOW_HALF_WIDTH 640 +#define WINDOW_HALF_HEIGHT 360 enum exit_codes : int { EXIT_CODE_SUCCESS, @@ -84,6 +86,9 @@ struct App { glm::mat4 projection_mat; GLint u_projection_idx; + glm::vec2 current_mouse; + glm::vec2 prev_mouse; + bool running; }; @@ -132,6 +137,7 @@ int init(App &app) { return EXIT_CODE_WINDOW_CREATION_FAILED; } + SDL_WarpMouseInWindow(app.window, WINDOW_HALF_WIDTH, WINDOW_HALF_HEIGHT); SDL_SetRelativeMouseMode(SDL_TRUE); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); @@ -173,6 +179,8 @@ int init(App &app) { 0.1f, 20.0f ); + app.current_mouse = glm::vec2(WINDOW_HALF_WIDTH, WINDOW_HALF_HEIGHT); + app.prev_mouse = glm::vec2(WINDOW_HALF_WIDTH, WINDOW_HALF_HEIGHT); return EXIT_CODE_SUCCESS; } @@ -431,9 +439,12 @@ void handle_object_movement(App &app) { void handle_camera_movement(App &app) { if (app.event.type == SDL_MOUSEMOTION) { + app.current_mouse += glm::vec2(app.event.motion.xrel, app.event.motion.yrel); + glm::vec2 mouse_delta = app.prev_mouse - app.current_mouse; app.camera.view_direction = glm::rotate(app.camera.view_direction, - glm::radians((float)app.event.motion.xrel * -0.5f), + glm::radians(mouse_delta.x), app.camera.up_vector); + app.prev_mouse = app.current_mouse; } else { switch (app.event.key.keysym.sym) { case SDLK_w: