Mouse Camera Control¶
Overview¶
This document explains the implementation of the mouse-controlled camera system (First Person View) in the engine. The system allows orientation control (Pitch/Yaw) via the mouse and movement via the keyboard.
Features¶
- Capture Mode: The cursor is hidden and locked to the window center.
- Sensitivity: Adjustable sensitivity factor.
- Clamping: Pitch is limited to ± 89 degrees to avoid Gimbal Lock.
- Smoothness: Frame-rate independent movement using
deltaTime.
Architecture¶
Data Flow¶
The input event management is decoupled from the camera update logic.
Implementation Details¶
Rotation Calculation (Euler Angles)¶
The camera's direction vector is calculated from the Yaw and Pitch angles:
// Convert Spherical to Cartesian coordinates
front.x = cos(glm_rad(yaw)) * cos(glm_rad(pitch));
front.y = sin(glm_rad(pitch));
front.z = sin(glm_rad(yaw)) * cos(glm_rad(pitch));
glm_normalize(front);
Input Handling¶
The mouse_callback function handles the raw mouse input:
- Calculate offset:
xoffset = xpos - lastX - Apply sensitivity:
xoffset *= sensitivity - Update angles:
yaw += xoffset - Constrain Pitch:
pitch = clamp(pitch, -89.0f, 89.0f)
[!NOTE] Ensure
firstMousecheck is implemented to prevent a sudden camera jump on the initial frame where the mouse enters the window.
Usage¶
To enable mouse capture:
To release the mouse (e.g., for UI interaction), switch toGLFW_CURSOR_NORMAL.