-
Notifications
You must be signed in to change notification settings - Fork 19
/
Extensions.cs
30 lines (23 loc) · 892 Bytes
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
namespace LDtkMonogameExample;
using System;
using Microsoft.Xna.Framework;
static class Extensions
{
/// <summary>
/// Magic lerp that doesnt use start position but instead uses current position and <paramref name="maxDistanceDelta"/>.
/// </summary>
public static Vector2 MoveTowards(this Vector2 current, Vector2 end, float maxDistanceDelta, out bool done)
{
float diffX = end.X - current.X;
float diffY = end.Y - current.Y;
float sqDist = (diffX * diffX) + (diffY * diffY);
if (sqDist == 0 || (maxDistanceDelta >= 0 && sqDist <= maxDistanceDelta * maxDistanceDelta))
{
done = true;
return end;
}
float dist = MathF.Sqrt(sqDist);
done = false;
return new Vector2(current.X + (diffX / dist * maxDistanceDelta), current.Y + (diffY / dist * maxDistanceDelta));
}
}