Add Vector2MoveTowards function (#1233)
This commit is contained in:
parent
c83477ffca
commit
4583987fb9
1 changed files with 11 additions and 0 deletions
|
@ -283,6 +283,17 @@ RMDEF Vector2 Vector2Rotate(Vector2 v, float degs)
|
|||
return result;
|
||||
}
|
||||
|
||||
// Move towards Target.
|
||||
RMDEF Vector2 Vector2MoveTowards( Vector2 v, Vector2 target, float maxDistance)
|
||||
{
|
||||
float dx = target.x - v.x;
|
||||
float dy = target.y - v.y;
|
||||
float value = ( dx * dx ) + ( dy * dy );
|
||||
if ( value == 0 || ( maxDistance >= 0 && value <= maxDistance * maxDistance )) return target;
|
||||
float result = sqrtf( value );
|
||||
return (Vector2){ v.x + dx / result * maxDistance, v.y + dy / result * maxDistance };
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module Functions Definition - Vector3 math
|
||||
//----------------------------------------------------------------------------------
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue