diff options
| author | Anata <[email protected]> | 2020-05-01 17:50:41 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-05-01 17:50:41 +0200 |
| commit | 4583987fb937bd3f1ff1409d10e015fa460a37d9 (patch) | |
| tree | 98522f6e129ba468e5b8e4888182a0087d1c0c8d /src/raymath.h | |
| parent | c83477ffcad3f2ff457e74a4cfee9cb6e606d4a1 (diff) | |
| download | raylib-4583987fb937bd3f1ff1409d10e015fa460a37d9.tar.gz raylib-4583987fb937bd3f1ff1409d10e015fa460a37d9.zip | |
Add Vector2MoveTowards function (#1233)
Diffstat (limited to 'src/raymath.h')
| -rw-r--r-- | src/raymath.h | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/src/raymath.h b/src/raymath.h index 398aef4a..acf97fb9 100644 --- a/src/raymath.h +++ b/src/raymath.h @@ -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 //---------------------------------------------------------------------------------- |
