From 67535647999faea49daf43bfeafeb4c4fc4f26ea Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 5 Mar 2021 19:39:40 +0000 Subject: Template rotations and add one for 2D rotation matrix --- lib/maths.cpp | 18 +++++++++++++----- lib/maths.h | 1 + test/test-maths.cpp | 1 + 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/maths.cpp b/lib/maths.cpp index 5800b1b..4ae0693 100644 --- a/lib/maths.cpp +++ b/lib/maths.cpp @@ -24,35 +24,43 @@ operator^(M & m, glm::ivec2 xy) } // Create a matrix for the angle, given the targets into the matrix +template inline auto -rotation(float a, glm::ivec2 c1, glm::ivec2 s1, glm::ivec2 c2, glm::ivec2 ms2) +rotation(typename M::value_type a, glm::ivec2 c1, glm::ivec2 s1, glm::ivec2 c2, glm::ivec2 ms2) { - glm::mat4 m(1); + M m(1); sincosf(a, m ^ s1, m ^ c1); m ^ c2 = m ^ c1; m ^ ms2 = -(m ^ s1); return m; } +// Create a flat (2D) transformation matrix +glm::mat2 +rotate_flat(float a) +{ + return rotation(a, {0, 0}, {0, 1}, {1, 1}, {1, 0}); +} + // Create a roll transformation matrix glm::mat4 rotate_roll(float a) { - return rotation(a, {0, 0}, {0, 1}, {1, 1}, {1, 0}); + return rotation(a, {0, 0}, {0, 1}, {1, 1}, {1, 0}); } // Create a yaw transformation matrix glm::mat4 rotate_yaw(float a) { - return rotation(a, {0, 0}, {2, 0}, {2, 2}, {0, 2}); + return rotation(a, {0, 0}, {2, 0}, {2, 2}, {0, 2}); } // Create a pitch transformation matrix glm::mat4 rotate_pitch(float a) { - return rotation(a, {1, 1}, {1, 2}, {2, 2}, {2, 1}); + return rotation(a, {1, 1}, {1, 2}, {2, 2}, {2, 1}); } // Create a bomcined yaw, pitch, roll transformation matrix diff --git a/lib/maths.h b/lib/maths.h index a3a391d..dc0c617 100644 --- a/lib/maths.h +++ b/lib/maths.h @@ -45,6 +45,7 @@ sincosf(float a) return sc; } +glm::mat2 rotate_flat(float); glm::mat4 rotate_roll(float); glm::mat4 rotate_yaw(float); glm::mat4 rotate_pitch(float); diff --git a/test/test-maths.cpp b/test/test-maths.cpp index ea60f2a..420b69e 100644 --- a/test/test-maths.cpp +++ b/test/test-maths.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include -- cgit v1.2.3