From 006cfa2893d4e153ae89331013b4cfd569bf45e0 Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Sun, 6 Oct 2024 19:32:59 +0200 Subject: [PATCH] camera accelerating like in unity and faster/slower with modifier keys --- include/camera.hpp | 2 ++ src/camera.cpp | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/include/camera.hpp b/include/camera.hpp index f6c1269..c96c4f7 100644 --- a/include/camera.hpp +++ b/include/camera.hpp @@ -9,6 +9,8 @@ class Camera : private MouseListener { private: glm::vec3 position {0, 0, 5}; + float velocityMultiplier = 0; + float phi = 0; float theta = 0; diff --git a/src/camera.cpp b/src/camera.cpp index a8d8588..0cf0d6c 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -14,13 +14,26 @@ const std::map moveDirections { }; void Camera::update(float dt) { - glm::vec3 add {}; + glm::vec3 velocity {}; for (const auto &[key, dir] : moveDirections) if (Input::KeyIsDown[key]) - add += dir; + velocity += dir; - position += localToWorld(add) * dt; + if (velocity == glm::vec3(0)) + velocityMultiplier = 0; + + const float baseModifier = 3; + float modifier = 1; + if (Input::KeyIsDown[GLFW_KEY_LEFT_SHIFT]) + modifier *= baseModifier; + if (Input::KeyIsDown[GLFW_KEY_LEFT_CONTROL]) + modifier /= baseModifier; + + velocityMultiplier += modifier * dt; + velocity *= (1 + velocityMultiplier) * modifier; + + position += localToWorld(velocity) * dt; } glm::mat4 Camera::view() const {