From 32361fe5fb0a43f8f471a0da7e81d85a750c95cf Mon Sep 17 00:00:00 2001 From: Shehryar Date: Mon, 17 May 2021 14:37:15 +0400 Subject: [PATCH] Fix FPS camera movement bug --- .../7.5.camera_exercise1/camera_exercise1.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/1.getting_started/7.5.camera_exercise1/camera_exercise1.cpp b/src/1.getting_started/7.5.camera_exercise1/camera_exercise1.cpp index c1f203a72..dfc2622de 100644 --- a/src/1.getting_started/7.5.camera_exercise1/camera_exercise1.cpp +++ b/src/1.getting_started/7.5.camera_exercise1/camera_exercise1.cpp @@ -5,16 +5,18 @@ // processes input received from any keyboard-like input system. Accepts input parameter in the form of camera defined ENUM (to abstract it from windowing systems) void ProcessKeyboard(Camera_Movement direction, float deltaTime) { + // Make sure the user stays at the ground level by setting the y axis to 0 + // The x and z axis should not include pitch as you will only move horizontally + glm::vec3 forward = glm::vec3(cos(glm::radians(Yaw)), 0.0f, sin(glm::radians(Yaw))); + float velocity = MovementSpeed * deltaTime; if (direction == FORWARD) - Position += Front * velocity; + Position += forward * velocity; // Add the forward vector to position if (direction == BACKWARD) - Position -= Front * velocity; + Position -= forward * velocity; // Subtract the forward vector from position if (direction == LEFT) Position -= Right * velocity; if (direction == RIGHT) Position += Right * velocity; - // make sure the user stays at the ground level - Position.y = 0.0f; // <-- this one-liner keeps the user at the ground level (xz plane) } [...] \ No newline at end of file