From 328a85feb1df03f6f5cb45e811a62ce9c672975a Mon Sep 17 00:00:00 2001 From: Quinta <0pietroquintavalle0@gmail.com> Date: Fri, 12 Jul 2024 16:58:21 +0200 Subject: [PATCH] Fixed wonky WASD movement --- Renderer.cpp | 66 ++++++++++++++++++++++++++++++++-------------------- Renderer.h | 12 ++++++---- 2 files changed, 48 insertions(+), 30 deletions(-) diff --git a/Renderer.cpp b/Renderer.cpp index bd7cbf9..a9a6538 100644 --- a/Renderer.cpp +++ b/Renderer.cpp @@ -17,9 +17,9 @@ Renderer::Renderer(int width, int height) mouseSensitivity(0.05f), yaw(-45.0f), pitch(-30.0f), - firstMouse(true), - lastX(width / 2.0f), - lastY(height / 2.0f) + mousePressed(false), + lastMouseX(width / 2.0), + lastMouseY(height / 2.0) { if (!glfwInit()) { throw std::runtime_error("Failed to initialize GLFW"); @@ -45,11 +45,10 @@ Renderer::Renderer(int width, int height) createSphereMesh(1.0f, 20, 20); // Set up camera - glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); glfwSetWindowUserPointer(window, this); - glfwSetCursorPosCallback(window, [](GLFWwindow* window, double xpos, double ypos) { - static_cast(glfwGetWindowUserPointer(window))->cursorPosCallback(xpos, ypos); - }); + glfwSetCursorPosCallback(window, cursorPosCallback); + glfwSetMouseButtonCallback(window, mouseButtonCallback); } Renderer::~Renderer() { @@ -312,27 +311,46 @@ void Renderer::drawTrajectories(const std::vector& bodies) { } void Renderer::processInput() { + float cameraSpeed = this->cameraSpeed; + + glm::vec3 front(cameraFront.x, 0, cameraFront.z); + front = glm::normalize(front); + glm::vec3 right = glm::normalize(glm::cross(front, cameraUp)); + if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) - cameraPos += cameraSpeed * cameraFront; + cameraPos += front * cameraSpeed; if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) - cameraPos -= cameraSpeed * cameraFront; + cameraPos -= front * cameraSpeed; if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) - cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed; + cameraPos -= right * cameraSpeed; if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) - cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed; + cameraPos += right * cameraSpeed; } -void Renderer::cursorPosCallback(double xpos, double ypos) { - if (firstMouse) { - lastX = xpos; - lastY = ypos; - firstMouse = false; - } +void Renderer::cursorPosCallback(GLFWwindow* window, double xpos, double ypos) { + Renderer* renderer = static_cast(glfwGetWindowUserPointer(window)); + renderer->handleMouseMove(xpos, ypos); +} - float xoffset = xpos - lastX; - float yoffset = lastY - ypos; - lastX = xpos; - lastY = ypos; +void Renderer::mouseButtonCallback(GLFWwindow* window, int button, int action, int mods) { + Renderer* renderer = static_cast(glfwGetWindowUserPointer(window)); + if (button == GLFW_MOUSE_BUTTON_LEFT) { + if (action == GLFW_PRESS) { + renderer->mousePressed = true; + glfwGetCursorPos(window, &renderer->lastMouseX, &renderer->lastMouseY); + } else if (action == GLFW_RELEASE) { + renderer->mousePressed = false; + } + } +} + +void Renderer::handleMouseMove(double xpos, double ypos) { + if (!mousePressed) return; + + float xoffset = xpos - lastMouseX; + float yoffset = lastMouseY - ypos; + lastMouseX = xpos; + lastMouseY = ypos; xoffset *= mouseSensitivity; yoffset *= mouseSensitivity; @@ -340,10 +358,8 @@ void Renderer::cursorPosCallback(double xpos, double ypos) { yaw += xoffset; pitch += yoffset; - if (pitch > 89.0f) - pitch = 89.0f; - if (pitch < -89.0f) - pitch = -89.0f; + if (pitch > 89.0f) pitch = 89.0f; + if (pitch < -89.0f) pitch = -89.0f; updateCameraVectors(); } diff --git a/Renderer.h b/Renderer.h index 16acdb6..8e2f5f6 100644 --- a/Renderer.h +++ b/Renderer.h @@ -19,20 +19,21 @@ public: bool shouldClose(); void swapBuffers(); void processInput(); - void cursorPosCallback(double xpos, double ypos); + + static void cursorPosCallback(GLFWwindow* window, double xpos, double ypos); + static void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods); private: GLFWwindow* window; void drawSphere(const glm::vec3& position, float radius); void createSphereMesh(float radius, int sectors, int stacks); - void drawDebugTriangle(); // Debug function to draw a triangle + void drawDebugTriangle(); GLuint sphereVAO, sphereVBO, sphereEBO; int sphereVertexCount, sphereIndexCount; void drawGrid(const Simulator& simulator); float calculateGravityFieldStrength(const glm::vec3& point, const std::vector& bodies); - void drawGravityField(const Simulator& simulator); void drawTrajectories(const std::vector& bodies); glm::vec3 cameraPos; @@ -42,9 +43,10 @@ private: float mouseSensitivity; float yaw; float pitch; - bool firstMouse; - double lastX, lastY; + bool mousePressed; + double lastMouseX, lastMouseY; void updateCameraVectors(); + void handleMouseMove(double xpos, double ypos); }; #endif //GRAVITY_RENDERER_H