From 474ee631b25b8a032e59520a4bc3f096c8789b1f Mon Sep 17 00:00:00 2001 From: Noel Berry Date: Sun, 11 Oct 2020 13:50:44 -0700 Subject: [PATCH] angle utility methods --- public/blah/math/calc.cpp | 6 ++++++ public/blah/math/calc.h | 2 ++ public/blah/math/vec2.cpp | 11 +++++++++++ public/blah/math/vec2.h | 2 ++ 4 files changed, 21 insertions(+) diff --git a/public/blah/math/calc.cpp b/public/blah/math/calc.cpp index 1df36af..3bc2f9b 100644 --- a/public/blah/math/calc.cpp +++ b/public/blah/math/calc.cpp @@ -119,6 +119,12 @@ float Calc::angle_diff(float radians_a, float radians_b) return mod((radians_b - radians_a) + PI, TAU) - PI; } +float Calc::angle_lerp(float radians_a, float radians_b, float p) +{ + const auto shortest_angle = mod(mod(radians_b - radians_a, TAU) + (TAU + PI), TAU) - PI; + return radians_a + mod(shortest_angle * p, TAU); +} + float Calc::lerp(float a, float b, float t) { return a + (b - a) * t; diff --git a/public/blah/math/calc.h b/public/blah/math/calc.h index 6a77e35..732ac7e 100644 --- a/public/blah/math/calc.h +++ b/public/blah/math/calc.h @@ -61,6 +61,8 @@ namespace Blah float snap(float val, float interval); float angle_diff(float radians_a, float radians_b); + + float angle_lerp(float radians_a, float radians_b, float p); float lerp(float a, float b, float t); diff --git a/public/blah/math/vec2.cpp b/public/blah/math/vec2.cpp index 14718f0..4518fad 100644 --- a/public/blah/math/vec2.cpp +++ b/public/blah/math/vec2.cpp @@ -35,6 +35,17 @@ Vec2 Vec2::normal() const float length = this->length(); return Vec2(x / length, y / length); } + +Vec2 Vec2::turn_right() const +{ + return Vec2(y, -x); +} + +Vec2 Vec2::turn_left() const +{ + return Vec2(-y, x); +} + float Vec2::length() const { return sqrtf(x * x + y * y); } float Vec2::length_squared() const { return x * x + y * y; } diff --git a/public/blah/math/vec2.h b/public/blah/math/vec2.h index e505bbf..8f4c51a 100644 --- a/public/blah/math/vec2.h +++ b/public/blah/math/vec2.h @@ -35,6 +35,8 @@ namespace Blah bool operator !=(const Vec2& rhs); Vec2 normal() const; + Vec2 turn_right() const; + Vec2 turn_left() const; float length() const; float length_squared() const; Vec2 perpendicular() const;