From 731846a0c654b39e23c26f611470e401df404c9d Mon Sep 17 00:00:00 2001 From: Mel Date: Thu, 6 Oct 2022 01:42:52 +0200 Subject: Relative to camera movement --- src/Camera.cpp | 7 +++++++ src/Camera.hpp | 1 + src/Math/Matrix.hpp | 10 ++++++++++ src/main.cpp | 2 +- 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Camera.cpp b/src/Camera.cpp index 5bb9aa2..59d677c 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -14,6 +14,13 @@ void Camera::move(Vector<3> vector) { m_position = m_position + vector; } +void Camera::move_relative(Vector<3> by) { + auto rotation = Matrix<4, 4>::rotation(m_angles); + + auto result = rotation.transpose() * Vector<4>{by.x(), by.y(), by.z(), 1.0f}; + move(result.elements); +} + Rotation Camera::angles() { return m_angles; } diff --git a/src/Camera.hpp b/src/Camera.hpp index decba87..705f8b9 100644 --- a/src/Camera.hpp +++ b/src/Camera.hpp @@ -12,6 +12,7 @@ public: Vector<3> position(); void set_position(Vector<3> position); void move(Vector<3> by); + void move_relative(Vector<3> by); Rotation angles(); void set_angles(Rotation angles); diff --git a/src/Math/Matrix.hpp b/src/Math/Matrix.hpp index 43f0721..d77760a 100644 --- a/src/Math/Matrix.hpp +++ b/src/Math/Matrix.hpp @@ -78,6 +78,16 @@ public: return result; } + Matrix transpose() { + Matrix result{}; + for (int y = 0; y < R; y++) { + for (int x = 0; x < C; x++) { + result(x, y) = this->operator()(y, x); + } + } + return result; + } + Matrix operator+(Matrix other) { Matrix result{}; for (int i = 0; i < R * C; i++) { diff --git a/src/main.cpp b/src/main.cpp index 32d3b4b..63d371a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -133,7 +133,7 @@ void process_input(MC::Window& window, MC::Camera& camera) { auto speed = 0.05f; - camera.move({x * speed, y * speed, z * speed}); + camera.move_relative({x * speed, y * speed, z * speed}); camera.rotate({rx * speed, ry * speed, 0.0f}); } -- cgit 1.4.1