summary refs log tree commit diff
path: root/src/Math
diff options
context:
space:
mode:
Diffstat (limited to 'src/Math')
-rw-r--r--src/Math/MVP.cpp2
-rw-r--r--src/Math/Math.hpp3
-rw-r--r--src/Math/Matrix.hpp7
-rw-r--r--src/Math/Rotation.hpp16
-rw-r--r--src/Math/Trig.hpp17
5 files changed, 39 insertions, 6 deletions
diff --git a/src/Math/MVP.cpp b/src/Math/MVP.cpp
index 146b208..de9f2ee 100644
--- a/src/Math/MVP.cpp
+++ b/src/Math/MVP.cpp
@@ -19,7 +19,7 @@ Matrix<4, 4> view(Vector<3> position, Rotation angles) {
 }
 
 Matrix<4, 4> projection(float aspect, float fov, float near, float far) {
-    auto fov_radians = (fov * M_PI) / 180.0f;
+    auto fov_radians = Math::radians(fov);
 
     float x_scale = 1.0f / (tan(fov_radians / 2.0f) * aspect);
     float y_scale = 1.0f / tan(fov_radians / 2.0f);
diff --git a/src/Math/Math.hpp b/src/Math/Math.hpp
index b8b61a1..94840b1 100644
--- a/src/Math/Math.hpp
+++ b/src/Math/Math.hpp
@@ -2,4 +2,5 @@
 
 #include "Vector.hpp"
 #include "Matrix.hpp"
-#include "Rotation.hpp"
\ No newline at end of file
+#include "Rotation.hpp"
+#include "Trig.hpp"
\ No newline at end of file
diff --git a/src/Math/Matrix.hpp b/src/Math/Matrix.hpp
index d77760a..545b6a5 100644
--- a/src/Math/Matrix.hpp
+++ b/src/Math/Matrix.hpp
@@ -4,6 +4,7 @@
 #include <cstddef>
 #include <iostream>
 #include "Rotation.hpp"
+#include "Trig.hpp"
 
 template <size_t R, size_t C, typename T = float>
 struct Matrix {
@@ -39,8 +40,10 @@ public:
     }
 
     static Matrix<4, 4> rotation(Rotation angles) {
-        auto c = angles.vector.map([](auto a) { return cos(a); });
-        auto s = angles.vector.map([](auto a) { return sin(a); });
+        auto radians = angles.vector.map([](auto a) { return Math::radians(a); });
+
+        auto c = radians.map([](auto a) { return cos(a); });
+        auto s = radians.map([](auto a) { return sin(a); });
 
         Matrix<4, 4> rotation_x{
             1.0f, 0.0f,  0.0f,   0.0f,
diff --git a/src/Math/Rotation.hpp b/src/Math/Rotation.hpp
index 73a8219..0c5c606 100644
--- a/src/Math/Rotation.hpp
+++ b/src/Math/Rotation.hpp
@@ -11,8 +11,8 @@ public:
 
     Rotation(float angles[3]) : Rotation(angles[0], angles[1], angles[2]) {};
 
-    Rotation(float x, float y, float z) {
-        vector = wrap({ x, y, z });
+    Rotation(float pitch, float yaw, float roll) {
+        vector = wrap({pitch, yaw, roll });
     };
 
     Rotation operator+(Rotation other) const {
@@ -27,5 +27,17 @@ public:
         return v.map([](auto a) { return fmod(a, 360.0f); });
     }
 
+    float& pitch() {
+        return vector[0];
+    }
+
+    float& yaw() {
+        return vector[1];
+    }
+
+    float& roll() {
+        return vector[2];
+    }
+
     Vector<3> vector;
 };
\ No newline at end of file
diff --git a/src/Math/Trig.hpp b/src/Math/Trig.hpp
new file mode 100644
index 0000000..c64e2f2
--- /dev/null
+++ b/src/Math/Trig.hpp
@@ -0,0 +1,17 @@
+#pragma once
+
+namespace Math {
+
+constexpr double PI = 3.14159265358979323846;
+
+template<typename T>
+T radians(T degrees) {
+    return (degrees * PI) / 180.0f;
+}
+
+template<typename T>
+T degrees(T radians) {
+    return radians * 180.0f / PI;
+}
+
+}
\ No newline at end of file