Add some more missing functions to C++ Math
This commit is contained in:
@@ -393,7 +393,7 @@ namespace Math
|
||||
/// <param name="target">The value to move towards.</param>
|
||||
/// <param name="maxDelta">The maximum change that should be applied to the value.</param>
|
||||
template<class T>
|
||||
float MoveTowards(const T current, const T target, const T maxDelta)
|
||||
static T MoveTowards(const T current, const T target, const T maxDelta)
|
||||
{
|
||||
if (Abs(target - current) <= maxDelta)
|
||||
return target;
|
||||
@@ -407,13 +407,38 @@ namespace Math
|
||||
/// <param name="target"></param>
|
||||
/// <param name="maxDelta"></param>
|
||||
template<class T>
|
||||
float MoveTowardsAngle(const T current, const T target, const T maxDelta)
|
||||
static T MoveTowardsAngle(const T current, const T target, const T maxDelta)
|
||||
{
|
||||
float delta = DeltaAngle(current, target);
|
||||
T delta = DeltaAngle(current, target);
|
||||
if ((-maxDelta < delta) && (delta < maxDelta))
|
||||
return target;
|
||||
target = current + delta;
|
||||
return MoveTowards(current, target, maxDelta);
|
||||
T deltaTarget = current + delta;
|
||||
return MoveTowards(current, deltaTarget, maxDelta);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the shortest difference between two given angles given in degrees.
|
||||
/// </summary>
|
||||
/// <param name="current"></param>
|
||||
/// <param name="target"></param>
|
||||
template<class T>
|
||||
static T DeltaAngle(const T current, const T target)
|
||||
{
|
||||
T t = Repeat(target - current, (T)360);
|
||||
if (t > (T)180)
|
||||
t -= (T)360;
|
||||
return t;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loops the value t, so that it is never larger than length and never smaller than 0.
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <param name="length"></param>
|
||||
template<class T>
|
||||
static T Repeat(const T t, const T length)
|
||||
{
|
||||
return t - Floor(t / length) * length;
|
||||
}
|
||||
|
||||
// Multiply value by itself
|
||||
|
||||
Reference in New Issue
Block a user