Skip to content

Commit 14f6ab2

Browse files
committed
Update camera pointing control scheme
Originally I had cursor disabled via GLFW. This worked and was very immersive (almost like an FPS) but now that I have imgui that solution won't work as the cursor obviously doesn't work. I adopted to use the popular scheme of drag clicking with right mouse button to move around where the camera was pointing. I think this will give a better UX for moving around the camera and allowing for quick changes with the imgui menu.
1 parent ca1a503 commit 14f6ab2

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

include/window.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ class Window {
4646
std::chrono::steady_clock::time_point last_time = std::chrono::steady_clock::now();
4747

4848
// Mouse tracking for delta calculation
49-
double last_mouse_x = 400.0;
50-
double last_mouse_y = 300.0;
49+
std::optional<double> last_mouse_x;
50+
std::optional<double> last_mouse_y;
5151
};

src/window.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "window.h"
22

33
#include <chrono>
4-
#include <iostream>
54
#include <stdexcept>
65

76
Window::Window(const WindowConfig& config) {
@@ -22,7 +21,6 @@ Window::Window(const WindowConfig& config) {
2221
if (window_m == nullptr) {
2322
throw std::runtime_error("Failed to create GLFW window");
2423
}
25-
glfwSetInputMode(window_m, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
2624
glfwMakeContextCurrent(window_m);
2725

2826
if (first_window) {
@@ -84,16 +82,27 @@ void Window::ProcessInput(Camera& camera) {
8482
camera.UpdatePosition(Camera::MoveDirection::DOWN, delta_ms);
8583

8684
// Mouse look
87-
double current_x, current_y;
88-
glfwGetCursorPos(window_m, &current_x, &current_y);
89-
90-
double delta_x = current_x - last_mouse_x;
91-
double delta_y = current_y - last_mouse_y;
92-
93-
last_mouse_x = current_x;
94-
last_mouse_y = current_y;
95-
96-
camera.UpdatePointingDirection(delta_x, delta_y);
85+
if (glfwGetMouseButton(window_m, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS) {
86+
double current_x, current_y;
87+
glfwGetCursorPos(window_m, &current_x, &current_y);
88+
if (last_mouse_x && last_mouse_y) {
89+
double delta_x = current_x - *last_mouse_x;
90+
double delta_y = current_y - *last_mouse_y;
91+
92+
last_mouse_x = current_x;
93+
last_mouse_y = current_y;
94+
95+
camera.UpdatePointingDirection(delta_x, delta_y);
96+
} else {
97+
glfwSetInputMode(window_m, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
98+
last_mouse_x = current_x;
99+
last_mouse_y = current_y;
100+
}
101+
} else if (last_mouse_x && last_mouse_y) {
102+
glfwSetInputMode(window_m, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
103+
last_mouse_x = std::nullopt;
104+
last_mouse_y = std::nullopt;
105+
}
97106
}
98107

99108
void Window::FramebufferSizeCallback(GLFWwindow* window, int width,

0 commit comments

Comments
 (0)