summary refs log tree commit diff
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-10-06 01:42:52 +0200
committerMel <einebeere@gmail.com>2022-10-06 01:42:52 +0200
commit731846a0c654b39e23c26f611470e401df404c9d (patch)
treed48a0f4160adaabaf2511808ba0519dc005fa44c
parent1474eb20033d809f85621b8b1060df362a239557 (diff)
downloadmeowcraft-731846a0c654b39e23c26f611470e401df404c9d.tar.zst
meowcraft-731846a0c654b39e23c26f611470e401df404c9d.zip
Relative to camera movement
-rw-r--r--src/Camera.cpp7
-rw-r--r--src/Camera.hpp1
-rw-r--r--src/Math/Matrix.hpp10
-rw-r--r--src/main.cpp2
4 files changed, 19 insertions, 1 deletions
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<R, C, T> transpose() {
+        Matrix<R, C, T> 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<R, C, T> operator+(Matrix<R, C, T> other) {
         Matrix<R, C, T> 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});
 }