-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2Extensions.cs
More file actions
89 lines (78 loc) · 3.32 KB
/
Copy pathVector2Extensions.cs
File metadata and controls
89 lines (78 loc) · 3.32 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using UnityEngine;
namespace asim.unity.extensions
{
public static class Vector2Extensions
{
public static Vector2 Normalize(Vector2 v)
{
double dlength = System.Math.Sqrt(v.x * v.x + v.y * v.y);
if (dlength > double.Epsilon)
{
float flength = (float)dlength;
v.x /= flength;
v.y /= flength;
return v;
}
else return Vector2.zero;
}
/// <summary>
/// Set the Length(Magnitude) of the Vector
/// </summary>
public static Vector2 SetMagnitude(this Vector2 v, float value)
{
return Normalize(v) * value;
}
/// <summary>
/// Restrict the length(magitude) of a vector
/// </summary>
public static Vector2 ClampMagitude(this Vector2 v,float min,float max)
{
float length = v.magnitude;
if(length < min)
{
return Normalize(v) * min;
}
else if (v.magnitude > max)
{
return Normalize(v) * max;
}
return v;
}
/// <summary>
/// Create Directional Vector From Angle couter-clockwise starting using Vector2(1,0)
/// </summary>
public static Vector2 fromAngle(float angleRad)
{
return new Vector2(Mathf.Cos(angleRad), Mathf.Sin(angleRad));
}
/// <summary>
/// Returns a vector rotated in radians couter-clockwise
/// </summary>
public static Vector2 Rotate(this Vector2 v, float radians)
{
return new Vector2(Mathf.Cos(radians) * v.x - Mathf.Sin(radians) * v.y, Mathf.Sin(radians) * v.x + Mathf.Cos(radians) * v.y);
}
//====================================================================================================
//====================================================================================================
/// <summary>
/// http://answers.unity3d.com/questions/131624/vector3-comparison.html#answer-131672
/// Returns true if vector are equal with a angle error margin
/// </summary>
public static bool ApproxEqual(this Vector2 v1, Vector2 v2, float angleError = 0.001f)
{
//if they aren't the same length, don't bother checking the rest.
if (!Mathf.Approximately(v1.magnitude, v2.magnitude))
return false;
var cosAngleError = Mathf.Cos(angleError * Mathf.Deg2Rad);
//A value between -1 and 1 corresponding to the angle.
//The dot product of normalized Vectors is equal to the cosine of the angle between them.
//So the closer they are, the closer the value will be to 1. Opposite Vectors will be -1
//and orthogonal Vectors will be 0.
var cosAngle = Vector2.Dot(v1.normalized, v2.normalized);
//If angle is greater, that means that the angle between the two vectors is less than the error allowed.
return cosAngle >= cosAngleError;
}
//====================================================================================================
//====================================================================================================
}
}