added Calc::approach for Vec2

This commit is contained in:
Noel Berry 2020-12-31 18:29:13 -08:00
parent 30cfbf203e
commit d8930f15ac
2 changed files with 13 additions and 3 deletions

View File

@ -1,5 +1,6 @@
#pragma once
#include <inttypes.h>
#include <blah/math/vec2.h>
namespace Blah
{
@ -22,7 +23,9 @@ namespace Blah
int rand_int();
float approach(float t, float target, float maxDelta);
float approach(float t, float target, float delta);
Vec2 approach(const Vec2& t, const Vec2& target, float delta);
float clamp(float t, float min, float max);

View File

@ -31,9 +31,16 @@ int Calc::rand_int()
return rand();
}
float Calc::approach(float t, float target, float maxDelta)
float Calc::approach(float t, float target, float delta)
{
return t < target ? min(t + maxDelta, target) : max(t - maxDelta, target);
return t < target ? min(t + delta, target) : max(t - delta, target);
}
Vec2 Calc::approach(const Vec2& t, const Vec2& target, float delta)
{
if ((target - t).length() <= delta)
return target;
return t + (target - t).normal() * delta;
}
float Calc::clamp(float t, float min, float max)