diff --git a/include/Spectre/Math/Transform.h b/include/Spectre/Math/Transform.h index 51ddf88..f29fa9c 100644 --- a/include/Spectre/Math/Transform.h +++ b/include/Spectre/Math/Transform.h @@ -53,6 +53,10 @@ public : Transform& scale(Vector2f offset); Transform& scale(float s); + // Inverse + + Transform inverse(); + Transform& combine(const Transform& other); Vector2f transformPoint(float x, float y) const; diff --git a/source/Math/Transform.cpp b/source/Math/Transform.cpp index ab4835b..35a7793 100644 --- a/source/Math/Transform.cpp +++ b/source/Math/Transform.cpp @@ -1,5 +1,6 @@ #include +#include #include namespace sp { @@ -83,6 +84,23 @@ Transform& Transform::scale(float s) return scale(s, s); } +Transform Transform::inverse() +{ + // Because a 2D Transform matrix is really just a 3x3 matrix. + // We can cheat here :) + Matrix3f m(m_matrix.e[0], m_matrix.e[1], m_matrix.e[3], + m_matrix.e[4], m_matrix.e[5], m_matrix.e[7], + m_matrix.e[12], m_matrix.e[13], m_matrix.e[15]); + + m = m.inverse(); + + return Transform( + m.v[0], m.v[3], m.v[6], + m.v[1], m.v[4], m.v[7], + m.v[2], m.v[5], m.v[8] + ); +} + Transform& Transform::combine(const Transform& other) { m_matrix *= other.m_matrix;