-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMathUtilities.cs
52 lines (45 loc) · 1.46 KB
/
MathUtilities.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using Microsoft.Xna.Framework;
namespace Desktoptale
{
public class MathUtilities
{
public static float Min(float a, float b)
{
return a < b ? a : b;
}
public static float Clamp(float value, float min, float max)
{
if (value < min)
value = min;
else if (value > max)
value = max;
return value;
}
public static float InterpolateQuadraticGetSlowerTowardsEnd(float a, float b, float t)
{
float oneMinusT = 1 - t;
float progress = 1 - oneMinusT * oneMinusT * oneMinusT * oneMinusT;
return MathHelper.Lerp(a, b, progress);
}
public static float SignedAngleBetween(Vector2 v, Vector2 w)
{
return (float)Math.Atan2((w.Y * v.X) - (w.X * v.Y), (w.X * v.X) + (w.Y * v.Y));
}
public static Orientation GetClosestOrientation(Vector2 direction)
{
float xDot = Vector2.Dot(direction, Vector2.UnitX);
float yDot = Vector2.Dot(direction, Vector2.UnitY);
if (Math.Abs(xDot) > Math.Abs(yDot))
{
if (direction.X < 0) return Orientation.Left;
return Orientation.Right;
}
else
{
if (direction.Y < 0) return Orientation.Up;
return Orientation.Down;
}
}
}
}