-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector2.cs
303 lines (258 loc) · 11.2 KB
/
Vector2.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Runtime.InteropServices;
// Representation of 2D vectors and points.
[StructLayout(LayoutKind.Sequential)]
public struct Vector2 : IEquatable<Vector2>
{
// X component of the vector.
public float x;
// Y component of the vector.
public float y;
// Access the /x/ or /y/ component using [0] or [1] respectively.
public float this[int index]
{
get
{
switch (index)
{
case 0: return x;
case 1: return y;
default:
throw new IndexOutOfRangeException("Invalid Vector2 index!");
}
}
set
{
switch (index)
{
case 0: x = value; break;
case 1: y = value; break;
default:
throw new IndexOutOfRangeException("Invalid Vector2 index!");
}
}
}
// Constructs a new vector with given x, y components.
public Vector2(float x, float y) { this.x = x; this.y = y; }
// Set x and y components of an existing Vector2.
public void Set(float newX, float newY) { x = newX; y = newY; }
// Linearly interpolates between two vectors.
public static Vector2 Lerp(Vector2 a, Vector2 b, float t)
{
t = Mathf.Clamp01(t);
return new Vector2(
a.x + (b.x - a.x) * t,
a.y + (b.y - a.y) * t
);
}
// Linearly interpolates between two vectors without clamping the interpolant
public static Vector2 LerpUnclamped(Vector2 a, Vector2 b, float t)
{
return new Vector2(
a.x + (b.x - a.x) * t,
a.y + (b.y - a.y) * t
);
}
// Moves a point /current/ towards /target/.
public static Vector2 MoveTowards(Vector2 current, Vector2 target, float maxDistanceDelta)
{
Vector2 toVector = target - current;
float dist = toVector.magnitude;
if (dist <= maxDistanceDelta || dist == 0)
return target;
return current + toVector / dist * maxDistanceDelta;
}
// Multiplies two vectors component-wise.
public static Vector2 Scale(Vector2 a, Vector2 b) { return new Vector2(a.x * b.x, a.y * b.y); }
// Multiplies every component of this vector by the same component of /scale/.
public void Scale(Vector2 scale) { x *= scale.x; y *= scale.y; }
// Makes this vector have a ::ref::magnitude of 1.
public void Normalize()
{
float mag = magnitude;
if (mag > kEpsilon)
this = this / mag;
else
this = zero;
}
// Returns this vector with a ::ref::magnitude of 1 (RO).
public Vector2 normalized
{
get
{
Vector2 v = new Vector2(x, y);
v.Normalize();
return v;
}
}
/// *listonly*
public override string ToString() { return string.Format("({0:F1}, {1:F1})", x, y); }
// Returns a nicely formatted string for this vector.
public string ToString(string format)
{
return string.Format("({0}, {1})", x.ToString(format), y.ToString(format));
}
// used to allow Vector2s to be used as keys in hash tables
public override int GetHashCode()
{
return x.GetHashCode() ^ (y.GetHashCode() << 2);
}
// also required for being able to use Vector2s as keys in hash tables
public override bool Equals(object other)
{
if (!(other is Vector2)) return false;
return Equals((Vector2)other);
}
public bool Equals(Vector2 other)
{
return x.Equals(other.x) && y.Equals(other.y);
}
public static Vector2 Reflect(Vector2 inDirection, Vector2 inNormal)
{
return -2F * Dot(inNormal, inDirection) * inNormal + inDirection;
}
public static Vector2 Perpendicular(Vector2 inDirection)
{
return new Vector2(-inDirection.y, inDirection.x);
}
// Dot Product of two vectors.
public static float Dot(Vector2 lhs, Vector2 rhs) { return lhs.x * rhs.x + lhs.y * rhs.y; }
// Returns the length of this vector (RO).
public float magnitude { get { return Mathf.Sqrt(x * x + y * y); } }
// Returns the squared length of this vector (RO).
public float sqrMagnitude { get { return x * x + y * y; } }
// Returns the angle in degrees between /from/ and /to/.
public static float Angle(Vector2 from, Vector2 to)
{
// sqrt(a) * sqrt(b) = sqrt(a * b) -- valid for real numbers
float denominator = Mathf.Sqrt(from.sqrMagnitude * to.sqrMagnitude);
if (denominator < kEpsilonNormalSqrt)
return 0F;
float dot = Mathf.Clamp(Dot(from, to) / denominator, -1F, 1F);
return Mathf.Acos(dot) * Mathf.Rad2Deg;
}
// Returns the signed angle in degrees between /from/ and /to/. Always returns the smallest possible angle
public static float SignedAngle(Vector2 from, Vector2 to)
{
float unsigned_angle = Angle(from, to);
float sign = Mathf.Sign(from.x * to.y - from.y * to.x);
return unsigned_angle * sign;
}
// Returns the distance between /a/ and /b/.
public static float Distance(Vector2 a, Vector2 b) { return (a - b).magnitude; }
// Returns a copy of /vector/ with its magnitude clamped to /maxLength/.
public static Vector2 ClampMagnitude(Vector2 vector, float maxLength)
{
if (vector.sqrMagnitude > maxLength * maxLength)
return vector.normalized * maxLength;
return vector;
}
// Returns a vector that is made from the smallest components of two vectors.
public static Vector2 Min(Vector2 lhs, Vector2 rhs) { return new Vector2(Mathf.Min(lhs.x, rhs.x), Mathf.Min(lhs.y, rhs.y)); }
// Returns a vector that is made from the largest components of two vectors.
public static Vector2 Max(Vector2 lhs, Vector2 rhs) { return new Vector2(Mathf.Max(lhs.x, rhs.x), Mathf.Max(lhs.y, rhs.y)); }
public static Vector2 SmoothDamp(Vector2 current, Vector2 target, ref Vector2 currentVelocity, float smoothTime, float maxSpeed)
{
float deltaTime = Time.deltaTime;
return SmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, deltaTime);
}
public static Vector2 SmoothDamp(Vector2 current, Vector2 target, ref Vector2 currentVelocity, float smoothTime)
{
float deltaTime = Time.deltaTime;
float maxSpeed = Mathf.Infinity;
return SmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, deltaTime);
}
public static Vector2 SmoothDamp(Vector2 current, Vector2 target, ref Vector2 currentVelocity, float smoothTime, float maxSpeed, float deltaTime)
{
// Based on Game Programming Gems 4 Chapter 1.10
smoothTime = Mathf.Max(0.0001F, smoothTime);
float omega = 2F / smoothTime;
float x = omega * deltaTime;
float exp = 1F / (1F + x + 0.48F * x * x + 0.235F * x * x * x);
Vector2 change = current - target;
Vector2 originalTo = target;
// Clamp maximum speed
float maxChange = maxSpeed * smoothTime;
change = ClampMagnitude(change, maxChange);
target = current - change;
Vector2 temp = (currentVelocity + omega * change) * deltaTime;
currentVelocity = (currentVelocity - omega * temp) * exp;
Vector2 output = target + (change + temp) * exp;
// Prevent overshooting
if (Dot(originalTo - current, output - originalTo) > 0)
{
output = originalTo;
currentVelocity = (output - originalTo) / deltaTime;
}
return output;
}
// Adds two vectors.
public static Vector2 operator+(Vector2 a, Vector2 b) { return new Vector2(a.x + b.x, a.y + b.y); }
// Subtracts one vector from another.
public static Vector2 operator-(Vector2 a, Vector2 b) { return new Vector2(a.x - b.x, a.y - b.y); }
// Multiplies one vector by another.
public static Vector2 operator*(Vector2 a, Vector2 b) { return new Vector2(a.x * b.x, a.y * b.y); }
// Divides one vector over another.
public static Vector2 operator/(Vector2 a, Vector2 b) { return new Vector2(a.x / b.x, a.y / b.y); }
// Negates a vector.
public static Vector2 operator-(Vector2 a) { return new Vector2(-a.x, -a.y); }
// Multiplies a vector by a number.
public static Vector2 operator*(Vector2 a, float d) { return new Vector2(a.x * d, a.y * d); }
// Multiplies a vector by a number.
public static Vector2 operator*(float d, Vector2 a) { return new Vector2(a.x * d, a.y * d); }
// Divides a vector by a number.
public static Vector2 operator/(Vector2 a, float d) { return new Vector2(a.x / d, a.y / d); }
// Returns true if the vectors are equal.
public static bool operator==(Vector2 lhs, Vector2 rhs)
{
// Returns false in the presence of NaN values.
return (lhs - rhs).sqrMagnitude < kEpsilon * kEpsilon;
}
// Returns true if vectors are different.
public static bool operator!=(Vector2 lhs, Vector2 rhs)
{
// Returns true in the presence of NaN values.
return !(lhs == rhs);
}
// Converts a [[Vector3]] to a Vector2.
public static implicit operator Vector2(Vector3 v)
{
return new Vector2(v.x, v.y);
}
// Converts a Vector2 to a [[Vector3]].
public static implicit operator Vector3(Vector2 v)
{
return new Vector3(v.x, v.y, 0);
}
static readonly Vector2 zeroVector = new Vector2(0F, 0F);
static readonly Vector2 oneVector = new Vector2(1F, 1F);
static readonly Vector2 upVector = new Vector2(0F, 1F);
static readonly Vector2 downVector = new Vector2(0F, -1F);
static readonly Vector2 leftVector = new Vector2(-1F, 0F);
static readonly Vector2 rightVector = new Vector2(1F, 0F);
static readonly Vector2 positiveInfinityVector = new Vector2(float.PositiveInfinity, float.PositiveInfinity);
static readonly Vector2 negativeInfinityVector = new Vector2(float.NegativeInfinity, float.NegativeInfinity);
// Shorthand for writing @@Vector2(0, 0)@@
public static Vector2 zero { get { return zeroVector; } }
// Shorthand for writing @@Vector2(1, 1)@@
public static Vector2 one { get { return oneVector; } }
// Shorthand for writing @@Vector2(0, 1)@@
public static Vector2 up { get { return upVector; } }
// Shorthand for writing @@Vector2(0, -1)@@
public static Vector2 down { get { return downVector; } }
// Shorthand for writing @@Vector2(-1, 0)@@
public static Vector2 left { get { return leftVector; } }
// Shorthand for writing @@Vector2(1, 0)@@
public static Vector2 right { get { return rightVector; } }
// Shorthand for writing @@Vector2(float.PositiveInfinity, float.PositiveInfinity)@@
public static Vector2 positiveInfinity { get { return positiveInfinityVector; } }
// Shorthand for writing @@Vector2(float.NegativeInfinity, float.NegativeInfinity)@@
public static Vector2 negativeInfinity { get { return negativeInfinityVector; } }
// *Undocumented*
public const float kEpsilon = 0.00001F;
// *Undocumented*
public const float kEpsilonNormalSqrt = 1e-15f;
}