Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MoveTowards functions to C++ Math #1005

Merged
merged 1 commit into from Apr 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions Source/Engine/Core/Math/Math.h
Expand Up @@ -386,6 +386,36 @@ namespace Math
return Min(Min(Min(a, b), c), d);
}

/// <summary>
/// Moves a value current towards target.
/// </summary>
/// <param name="current">The current value.</param>
/// <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)
{
if (Abs(target - current) <= maxDelta)
return target;
return current + Sign(target - current) * maxDelta;
}

/// <summary>
/// Same as MoveTowards but makes sure the values interpolate correctly when they wrap around 360 degrees.
/// </summary>
/// <param name="current"></param>
/// <param name="target"></param>
/// <param name="maxDelta"></param>
template<class T>
float MoveTowardsAngle(const T current, const T target, const T maxDelta)
{
float delta = DeltaAngle(current, target);
if ((-maxDelta < delta) && (delta < maxDelta))
return target;
target = current + delta;
return MoveTowards(current, target, maxDelta);
}

// Multiply value by itself
template<class T>
static T Square(const T a)
Expand Down