Skip to content

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.

Graphviz Diagram

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:

  1. Calculate offset: xoffset = xpos - lastX
  2. Apply sensitivity: xoffset *= sensitivity
  3. Update angles: yaw += xoffset
  4. Constrain Pitch: pitch = clamp(pitch, -89.0f, 89.0f)

[!NOTE] Ensure firstMouse check is implemented to prevent a sudden camera jump on the initial frame where the mouse enters the window.

Usage

To enable mouse capture:

// Enable mouse capture in GLFW
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
To release the mouse (e.g., for UI interaction), switch to GLFW_CURSOR_NORMAL.