diff options
| author | Mel <einebeere@gmail.com> | 2022-10-06 01:42:52 +0200 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2022-10-06 01:42:52 +0200 |
| commit | 731846a0c654b39e23c26f611470e401df404c9d (patch) | |
| tree | d48a0f4160adaabaf2511808ba0519dc005fa44c /src | |
| parent | 1474eb20033d809f85621b8b1060df362a239557 (diff) | |
| download | meowcraft-731846a0c654b39e23c26f611470e401df404c9d.tar.zst meowcraft-731846a0c654b39e23c26f611470e401df404c9d.zip | |
Relative to camera movement
Diffstat (limited to 'src')
| -rw-r--r-- | src/Camera.cpp | 7 | ||||
| -rw-r--r-- | src/Camera.hpp | 1 | ||||
| -rw-r--r-- | src/Math/Matrix.hpp | 10 | ||||
| -rw-r--r-- | src/main.cpp | 2 |
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}); } |
