diff --git a/include/J3ML/J3ML.hpp b/include/J3ML/J3ML.hpp index 5690aeb..4272d54 100644 --- a/include/J3ML/J3ML.hpp +++ b/include/J3ML/J3ML.hpp @@ -382,10 +382,6 @@ namespace J3ML::Math::Functions { /// 2241 -> 2.2k, 55421 -> 55.4k, 1000000 -> 1.0M std::string Truncate(float input); - - - - float RecipFast(float x); @@ -486,28 +482,5 @@ namespace J3ML::Math::Types { } -namespace J3ML::Math { - struct Rotation { - Rotation(); - Rotation(float value); - Rotation(const Types::Radians& radians); - Rotation(const Types::Degrees& degrees); - - float valueInRadians; - float ValueInRadians() const { return valueInRadians; } - Types::Radians Radians() const { return {valueInRadians}; } - float Degrees() const { return Functions::Degrees(valueInRadians); } - - Rotation operator+(const Rotation& rhs); - }; - - Rotation operator ""_rad(long double rads); - - Rotation operator ""_radians(long double rads); - - Rotation operator ""_deg(long double rads); - - Rotation operator ""_degrees(long double rads); -} diff --git a/include/J3ML/Rotation.hpp b/include/J3ML/Rotation.hpp new file mode 100644 index 0000000..ac31e4c --- /dev/null +++ b/include/J3ML/Rotation.hpp @@ -0,0 +1,56 @@ +#pragma once +#include "J3ML.hpp" +#include "LinearAlgebra/Vector2.hpp" + +namespace J3ML::Math { + + /// Rotation is a class that represents a single axis of rotation. + /// The class is designed to behave very similarly to a float literal, and + /// primarily help organize code involving rotations by handling common boilerplate + /// and providing mathematical expressions. + struct Rotation { + + constexpr Rotation(); + constexpr Rotation(float value); + constexpr explicit Rotation(const Vector2& direction_vector); + + constexpr Rotation FromDegrees(float degrees); + + constexpr Rotation FromRadians(float radians); + + //Rotation(const Types::Radians& radians); + //Rotation(const Types::Degrees& degrees); + + + constexpr float Radians() const { return value;} + //Types::Radians Radians() const { return {value}; } + constexpr float Degrees() const { return Math::Degrees(value); } + + constexpr Rotation operator+(const Rotation& rhs) const; + constexpr Rotation operator-(const Rotation& rhs) const; + constexpr Rotation operator*(float scalar) const; + constexpr Rotation operator/(float scalar) const; + constexpr bool operator==(const Rotation& rhs) const = default; + constexpr Rotation operator-() const; + + /// Rotates a given Vector2 by this Rotation. + Vector2 Rotate(const Vector2& rhs) const; + + float operator()() const { return value; } + Rotation& operator=(const Rotation& rhs) { + this->value = rhs.value; + return *this; + } + + protected: + float value; + }; + + constexpr Rotation operator ""_rad(long double rads); + + constexpr Rotation operator ""_radians(long double rads); + + constexpr Rotation operator ""_deg(long double rads); + + constexpr Rotation operator ""_degrees(long double rads); +} diff --git a/main.cpp b/main.cpp index 6c73b5f..9a44486 100644 --- a/main.cpp +++ b/main.cpp @@ -18,6 +18,8 @@ #include #include +#include "J3ML/Rotation.hpp" + int main(int argc, char** argv) { @@ -90,6 +92,10 @@ int main(int argc, char** argv) v4i ipair4(1,2,3,4); + using namespace J3ML::Math; + + Rotation my_rot = 25_degrees; + return 0; } diff --git a/src/J3ML/J3ML.cpp b/src/J3ML/J3ML.cpp index 7cdc792..65466f9 100644 --- a/src/J3ML/J3ML.cpp +++ b/src/J3ML/J3ML.cpp @@ -136,14 +136,6 @@ namespace J3ML::Math::Functions { } namespace J3ML { - Math::Rotation Math::operator ""_degrees(long double rads) { return {Functions::Radians((float)rads)}; } - - Math::Rotation Math::operator ""_deg(long double rads) { return {Functions::Radians((float)rads)}; } - - Math::Rotation Math::operator ""_radians(long double rads) { return {(float)rads}; } - - Math::Rotation Math::operator ""_rad(long double rads) { return {(float)rads}; } - float Math::Functions::FastRSqrt(float x) { return 1.f / FastSqrt(x); } @@ -305,17 +297,7 @@ namespace J3ML { return 1.f / x; } - Math::Rotation::Rotation() : valueInRadians(0) {} - Math::Rotation::Rotation(float value) : valueInRadians(value) {} - - Math::Rotation::Rotation(const Types::Radians &radians): valueInRadians(radians.value) {} - - Math::Rotation::Rotation(const Types::Degrees °rees): valueInRadians(Functions::Radians(degrees.value)) {} - - Math::Rotation Math::Rotation::operator+(const Math::Rotation &rhs) { - return {valueInRadians + rhs.valueInRadians}; - } // int BitTwiddling::CountBitsSet(u32 value) { } diff --git a/src/J3ML/Rotation.cpp b/src/J3ML/Rotation.cpp new file mode 100644 index 0000000..c2e9a69 --- /dev/null +++ b/src/J3ML/Rotation.cpp @@ -0,0 +1,44 @@ +#include + +namespace J3ML { + Math::Rotation Math::operator ""_degrees(long double rads) { return {Functions::Radians((float)rads)}; } + + Math::Rotation Math::operator ""_deg(long double rads) { return {Functions::Radians((float)rads)}; } + + Math::Rotation Math::operator ""_radians(long double rads) { return {(float)rads}; } + + Vector2 Math::Rotation::Rotate(const Vector2 &rhs) const { + float cos_a = Math::Cos(value); + float sin_a = Math::Sin(value); + + return Vector2( + rhs.x * cos_a - rhs.y * sin_a, + rhs.x * sin_a + rhs.y * cos_a); + } + + Math::Rotation Math::operator ""_rad(long double rads) { return {(float)rads}; } + + Math::Rotation::Rotation() : value(0) {} + + Math::Rotation::Rotation(float value) : value(value) {} + + constexpr Math::Rotation::Rotation(const Vector2 &direction_vector) { + value = Math::Atan2(direction_vector.y, direction_vector.x); + } + + constexpr Math::Rotation Math::Rotation::FromDegrees(float degrees) { + return Rotation(Math::Radians(degrees)); + } + + constexpr Math::Rotation Math::Rotation::FromRadians(float radians) { return Rotation(value);} + + + Math::Rotation Math::Rotation::operator+(const Math::Rotation &rhs) const { + return {value + rhs.value}; + } + + Math::Rotation Math::Rotation::operator-(const Math::Rotation &rhs) const { + return {value - rhs.value}; + + } +}