angle utility methods

This commit is contained in:
Noel Berry 2020-10-11 13:50:44 -07:00
parent 1af8669441
commit 474ee631b2
4 changed files with 21 additions and 0 deletions

View File

@ -119,6 +119,12 @@ float Calc::angle_diff(float radians_a, float radians_b)
return mod((radians_b - radians_a) + PI, TAU) - PI; 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) float Calc::lerp(float a, float b, float t)
{ {
return a + (b - a) * t; return a + (b - a) * t;

View File

@ -61,6 +61,8 @@ namespace Blah
float snap(float val, float interval); float snap(float val, float interval);
float angle_diff(float radians_a, float radians_b); 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); float lerp(float a, float b, float t);

View File

@ -35,6 +35,17 @@ Vec2 Vec2::normal() const
float length = this->length(); float length = this->length();
return Vec2(x / length, y / 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() const { return sqrtf(x * x + y * y); }
float Vec2::length_squared() const { return x * x + y * y; } float Vec2::length_squared() const { return x * x + y * y; }

View File

@ -35,6 +35,8 @@ namespace Blah
bool operator !=(const Vec2& rhs); bool operator !=(const Vec2& rhs);
Vec2 normal() const; Vec2 normal() const;
Vec2 turn_right() const;
Vec2 turn_left() const;
float length() const; float length() const;
float length_squared() const; float length_squared() const;
Vec2 perpendicular() const; Vec2 perpendicular() const;