Fixed wonky WASD movement
This commit is contained in:
+40
-24
@@ -17,9 +17,9 @@ Renderer::Renderer(int width, int height)
|
|||||||
mouseSensitivity(0.05f),
|
mouseSensitivity(0.05f),
|
||||||
yaw(-45.0f),
|
yaw(-45.0f),
|
||||||
pitch(-30.0f),
|
pitch(-30.0f),
|
||||||
firstMouse(true),
|
mousePressed(false),
|
||||||
lastX(width / 2.0f),
|
lastMouseX(width / 2.0),
|
||||||
lastY(height / 2.0f)
|
lastMouseY(height / 2.0)
|
||||||
{
|
{
|
||||||
if (!glfwInit()) {
|
if (!glfwInit()) {
|
||||||
throw std::runtime_error("Failed to initialize GLFW");
|
throw std::runtime_error("Failed to initialize GLFW");
|
||||||
@@ -45,11 +45,10 @@ Renderer::Renderer(int width, int height)
|
|||||||
createSphereMesh(1.0f, 20, 20);
|
createSphereMesh(1.0f, 20, 20);
|
||||||
|
|
||||||
// Set up camera
|
// Set up camera
|
||||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
||||||
glfwSetWindowUserPointer(window, this);
|
glfwSetWindowUserPointer(window, this);
|
||||||
glfwSetCursorPosCallback(window, [](GLFWwindow* window, double xpos, double ypos) {
|
glfwSetCursorPosCallback(window, cursorPosCallback);
|
||||||
static_cast<Renderer*>(glfwGetWindowUserPointer(window))->cursorPosCallback(xpos, ypos);
|
glfwSetMouseButtonCallback(window, mouseButtonCallback);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Renderer::~Renderer() {
|
Renderer::~Renderer() {
|
||||||
@@ -312,27 +311,46 @@ void Renderer::drawTrajectories(const std::vector<CelestialBody>& bodies) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::processInput() {
|
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)
|
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||||
cameraPos += cameraSpeed * cameraFront;
|
cameraPos += front * cameraSpeed;
|
||||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||||
cameraPos -= cameraSpeed * cameraFront;
|
cameraPos -= front * cameraSpeed;
|
||||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
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)
|
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) {
|
void Renderer::cursorPosCallback(GLFWwindow* window, double xpos, double ypos) {
|
||||||
if (firstMouse) {
|
Renderer* renderer = static_cast<Renderer*>(glfwGetWindowUserPointer(window));
|
||||||
lastX = xpos;
|
renderer->handleMouseMove(xpos, ypos);
|
||||||
lastY = ypos;
|
|
||||||
firstMouse = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float xoffset = xpos - lastX;
|
void Renderer::mouseButtonCallback(GLFWwindow* window, int button, int action, int mods) {
|
||||||
float yoffset = lastY - ypos;
|
Renderer* renderer = static_cast<Renderer*>(glfwGetWindowUserPointer(window));
|
||||||
lastX = xpos;
|
if (button == GLFW_MOUSE_BUTTON_LEFT) {
|
||||||
lastY = ypos;
|
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;
|
xoffset *= mouseSensitivity;
|
||||||
yoffset *= mouseSensitivity;
|
yoffset *= mouseSensitivity;
|
||||||
@@ -340,10 +358,8 @@ void Renderer::cursorPosCallback(double xpos, double ypos) {
|
|||||||
yaw += xoffset;
|
yaw += xoffset;
|
||||||
pitch += yoffset;
|
pitch += yoffset;
|
||||||
|
|
||||||
if (pitch > 89.0f)
|
if (pitch > 89.0f) pitch = 89.0f;
|
||||||
pitch = 89.0f;
|
if (pitch < -89.0f) pitch = -89.0f;
|
||||||
if (pitch < -89.0f)
|
|
||||||
pitch = -89.0f;
|
|
||||||
|
|
||||||
updateCameraVectors();
|
updateCameraVectors();
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-5
@@ -19,20 +19,21 @@ public:
|
|||||||
bool shouldClose();
|
bool shouldClose();
|
||||||
void swapBuffers();
|
void swapBuffers();
|
||||||
void processInput();
|
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:
|
private:
|
||||||
GLFWwindow* window;
|
GLFWwindow* window;
|
||||||
void drawSphere(const glm::vec3& position, float radius);
|
void drawSphere(const glm::vec3& position, float radius);
|
||||||
void createSphereMesh(float radius, int sectors, int stacks);
|
void createSphereMesh(float radius, int sectors, int stacks);
|
||||||
void drawDebugTriangle(); // Debug function to draw a triangle
|
void drawDebugTriangle();
|
||||||
|
|
||||||
GLuint sphereVAO, sphereVBO, sphereEBO;
|
GLuint sphereVAO, sphereVBO, sphereEBO;
|
||||||
int sphereVertexCount, sphereIndexCount;
|
int sphereVertexCount, sphereIndexCount;
|
||||||
|
|
||||||
void drawGrid(const Simulator& simulator);
|
void drawGrid(const Simulator& simulator);
|
||||||
float calculateGravityFieldStrength(const glm::vec3& point, const std::vector<CelestialBody>& bodies);
|
float calculateGravityFieldStrength(const glm::vec3& point, const std::vector<CelestialBody>& bodies);
|
||||||
void drawGravityField(const Simulator& simulator);
|
|
||||||
void drawTrajectories(const std::vector<CelestialBody>& bodies);
|
void drawTrajectories(const std::vector<CelestialBody>& bodies);
|
||||||
|
|
||||||
glm::vec3 cameraPos;
|
glm::vec3 cameraPos;
|
||||||
@@ -42,9 +43,10 @@ private:
|
|||||||
float mouseSensitivity;
|
float mouseSensitivity;
|
||||||
float yaw;
|
float yaw;
|
||||||
float pitch;
|
float pitch;
|
||||||
bool firstMouse;
|
bool mousePressed;
|
||||||
double lastX, lastY;
|
double lastMouseX, lastMouseY;
|
||||||
|
|
||||||
void updateCameraVectors();
|
void updateCameraVectors();
|
||||||
|
void handleMouseMove(double xpos, double ypos);
|
||||||
};
|
};
|
||||||
#endif //GRAVITY_RENDERER_H
|
#endif //GRAVITY_RENDERER_H
|
||||||
|
|||||||
Reference in New Issue
Block a user