-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector3.cs
354 lines (299 loc) · 13.7 KB
/
Vector3.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// 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;
[StructLayout(LayoutKind.Sequential)]
public struct Vector3 : IEquatable<Vector3>
{
// *Undocumented*
public const float kEpsilon = 0.00001F;
// *Undocumented*
public const float kEpsilonNormalSqrt = 1e-15F;
// X component of the vector.
public float x;
// Y component of the vector.
public float y;
// Z component of the vector.
public float z;
// Linearly interpolates between two vectors.
public static Vector3 Lerp(Vector3 a, Vector3 b, float t)
{
t = Mathf.Clamp01(t);
return new Vector3(
a.x + (b.x - a.x) * t,
a.y + (b.y - a.y) * t,
a.z + (b.z - a.z) * t
);
}
// Linearly interpolates between two vectors without clamping the interpolant
public static Vector3 LerpUnclamped(Vector3 a, Vector3 b, float t)
{
return new Vector3(
a.x + (b.x - a.x) * t,
a.y + (b.y - a.y) * t,
a.z + (b.z - a.z) * t
);
}
// Moves a point /current/ in a straight line towards a /target/ point.
public static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta)
{
Vector3 toVector = target - current;
float dist = toVector.magnitude;
if (dist <= maxDistanceDelta || dist < float.Epsilon)
return target;
return current + toVector / dist * maxDistanceDelta;
}
public static Vector3 SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, float maxSpeed)
{
float deltaTime = Time.deltaTime;
return SmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, deltaTime);
}
public static Vector3 SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime)
{
float deltaTime = Time.deltaTime;
float maxSpeed = Mathf.Infinity;
return SmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, deltaTime);
}
// Gradually changes a vector towards a desired goal over time.
public static Vector3 SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, float maxSpeed, float deltaTime)
{
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);
Vector3 change = current - target;
Vector3 originalTo = target;
float maxChange = maxSpeed * smoothTime;
change = Vector3.ClampMagnitude(change, maxChange);
target = current - change;
Vector3 temp = (currentVelocity + omega * change) * deltaTime;
currentVelocity = (currentVelocity - omega * temp) * exp;
Vector3 output = target + (change + temp) * exp;
if (Vector3.Dot(originalTo - current, output - originalTo) > 0)
{
output = originalTo;
currentVelocity = (output - originalTo) / deltaTime;
}
return output;
}
// Access the x, y, z components using [0], [1], [2] respectively.
public float this[int index]
{
get
{
switch (index)
{
case 0: return x;
case 1: return y;
case 2: return z;
default:
throw new IndexOutOfRangeException("Invalid Vector3 index!");
}
}
set
{
switch (index)
{
case 0: x = value; break;
case 1: y = value; break;
case 2: z = value; break;
default:
throw new IndexOutOfRangeException("Invalid Vector3 index!");
}
}
}
// Creates a new vector with given x, y, z components.
public Vector3(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
// Creates a new vector with given x, y components and sets /z/ to zero.
public Vector3(float x, float y) { this.x = x; this.y = y; z = 0F; }
// Set x, y and z components of an existing Vector3.
public void Set(float newX, float newY, float newZ) { x = newX; y = newY; z = newZ; }
// Multiplies two vectors component-wise.
public static Vector3 Scale(Vector3 a, Vector3 b) { return new Vector3(a.x * b.x, a.y * b.y, a.z * b.z); }
// Multiplies every component of this vector by the same component of /scale/.
public void Scale(Vector3 scale) { x *= scale.x; y *= scale.y; z *= scale.z; }
// Cross Product of two vectors.
public static Vector3 Cross(Vector3 lhs, Vector3 rhs)
{
return new Vector3(
lhs.y * rhs.z - lhs.z * rhs.y,
lhs.z * rhs.x - lhs.x * rhs.z,
lhs.x * rhs.y - lhs.y * rhs.x);
}
// used to allow Vector3s to be used as keys in hash tables
public override int GetHashCode()
{
return x.GetHashCode() ^ (y.GetHashCode() << 2) ^ (z.GetHashCode() >> 2);
}
// also required for being able to use Vector3s as keys in hash tables
public override bool Equals(object other)
{
if (!(other is Vector3)) return false;
return Equals((Vector3)other);
}
public bool Equals(Vector3 other)
{
return x.Equals(other.x) && y.Equals(other.y) && z.Equals(other.z);
}
// Reflects a vector off the plane defined by a normal.
public static Vector3 Reflect(Vector3 inDirection, Vector3 inNormal)
{
return -2F * Dot(inNormal, inDirection) * inNormal + inDirection;
}
// *undoc* --- we have normalized property now
public static Vector3 Normalize(Vector3 value)
{
float mag = Magnitude(value);
if (mag > kEpsilon)
return value / mag;
else
return zero;
}
// Makes this vector have a ::ref::magnitude of 1.
public void Normalize()
{
float mag = Magnitude(this);
if (mag > kEpsilon)
this = this / mag;
else
this = zero;
}
// Returns this vector with a ::ref::magnitude of 1 (RO).
public Vector3 normalized { get { return Vector3.Normalize(this); } }
// Dot Product of two vectors.
public static float Dot(Vector3 lhs, Vector3 rhs) { return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z; }
// Projects a vector onto another vector.
public static Vector3 Project(Vector3 vector, Vector3 onNormal)
{
float sqrMag = Dot(onNormal, onNormal);
if (sqrMag < Mathf.Epsilon)
return zero;
else
return onNormal * Dot(vector, onNormal) / sqrMag;
}
// Projects a vector onto a plane defined by a normal orthogonal to the plane.
public static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal)
{
return vector - Project(vector, planeNormal);
}
// Returns the angle in degrees between /from/ and /to/. This is always the smallest
public static float Angle(Vector3 from, Vector3 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;
}
// The smaller of the two possible angles between the two vectors is returned, therefore the result will never be greater than 180 degrees or smaller than -180 degrees.
// If you imagine the from and to vectors as lines on a piece of paper, both originating from the same point, then the /axis/ vector would point up out of the paper.
// The measured angle between the two vectors would be positive in a clockwise direction and negative in an anti-clockwise direction.
public static float SignedAngle(Vector3 from, Vector3 to, Vector3 axis)
{
float unsignedAngle = Angle(from, to);
float sign = Mathf.Sign(Dot(axis, Cross(from, to)));
return unsignedAngle * sign;
}
// Returns the distance between /a/ and /b/.
public static float Distance(Vector3 a, Vector3 b)
{
Vector3 vec = new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
return Mathf.Sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z);
}
// Returns a copy of /vector/ with its magnitude clamped to /maxLength/.
public static Vector3 ClampMagnitude(Vector3 vector, float maxLength)
{
if (vector.sqrMagnitude > maxLength * maxLength)
return vector.normalized * maxLength;
return vector;
}
// *undoc* --- there's a property now
public static float Magnitude(Vector3 vector) { return Mathf.Sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z); }
// Returns the length of this vector (RO).
public float magnitude { get { return Mathf.Sqrt(x * x + y * y + z * z); } }
// *undoc* --- there's a property now
public static float SqrMagnitude(Vector3 vector) { return vector.x * vector.x + vector.y * vector.y + vector.z * vector.z; }
// Returns the squared length of this vector (RO).
public float sqrMagnitude { get { return x * x + y * y + z * z; } }
// Returns a vector that is made from the smallest components of two vectors.
public static Vector3 Min(Vector3 lhs, Vector3 rhs)
{
return new Vector3(Mathf.Min(lhs.x, rhs.x), Mathf.Min(lhs.y, rhs.y), Mathf.Min(lhs.z, rhs.z));
}
// Returns a vector that is made from the largest components of two vectors.
public static Vector3 Max(Vector3 lhs, Vector3 rhs)
{
return new Vector3(Mathf.Max(lhs.x, rhs.x), Mathf.Max(lhs.y, rhs.y), Mathf.Max(lhs.z, rhs.z));
}
static readonly Vector3 zeroVector = new Vector3(0F, 0F, 0F);
static readonly Vector3 oneVector = new Vector3(1F, 1F, 1F);
static readonly Vector3 upVector = new Vector3(0F, 1F, 0F);
static readonly Vector3 downVector = new Vector3(0F, -1F, 0F);
static readonly Vector3 leftVector = new Vector3(-1F, 0F, 0F);
static readonly Vector3 rightVector = new Vector3(1F, 0F, 0F);
static readonly Vector3 forwardVector = new Vector3(0F, 0F, 1F);
static readonly Vector3 backVector = new Vector3(0F, 0F, -1F);
static readonly Vector3 positiveInfinityVector = new Vector3(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity);
static readonly Vector3 negativeInfinityVector = new Vector3(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity);
// Shorthand for writing @@Vector3(0, 0, 0)@@
public static Vector3 zero { get { return zeroVector; } }
// Shorthand for writing @@Vector3(1, 1, 1)@@
public static Vector3 one { get { return oneVector; } }
// Shorthand for writing @@Vector3(0, 0, 1)@@
public static Vector3 forward { get { return forwardVector; } }
public static Vector3 back { get { return backVector; } }
// Shorthand for writing @@Vector3(0, 1, 0)@@
public static Vector3 up { get { return upVector; } }
public static Vector3 down { get { return downVector; } }
public static Vector3 left { get { return leftVector; } }
// Shorthand for writing @@Vector3(1, 0, 0)@@
public static Vector3 right { get { return rightVector; } }
// Shorthand for writing @@Vector3(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity)@@
public static Vector3 positiveInfinity { get { return positiveInfinityVector; } }
// Shorthand for writing @@Vector3(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity)@@
public static Vector3 negativeInfinity { get { return negativeInfinityVector; } }
// Adds two vectors.
public static Vector3 operator+(Vector3 a, Vector3 b) { return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z); }
// Subtracts one vector from another.
public static Vector3 operator-(Vector3 a, Vector3 b) { return new Vector3(a.x - b.x, a.y - b.y, a.z - b.z); }
// Negates a vector.
public static Vector3 operator-(Vector3 a) { return new Vector3(-a.x, -a.y, -a.z); }
// Multiplies a vector by a number.
public static Vector3 operator*(Vector3 a, float d) { return new Vector3(a.x * d, a.y * d, a.z * d); }
// Multiplies a vector by a number.
public static Vector3 operator*(float d, Vector3 a) { return new Vector3(a.x * d, a.y * d, a.z * d); }
// Divides a vector by a number.
public static Vector3 operator/(Vector3 a, float d) { return new Vector3(a.x / d, a.y / d, a.z / d); }
// Returns true if the vectors are equal.
public static bool operator==(Vector3 lhs, Vector3 rhs)
{
// Returns false in the presence of NaN values.
return SqrMagnitude(lhs - rhs) < kEpsilon * kEpsilon;
}
// Returns true if vectors are different.
public static bool operator!=(Vector3 lhs, Vector3 rhs)
{
// Returns true in the presence of NaN values.
return !(lhs == rhs);
}
public override string ToString()
{
return string.Format("({0:F1}, {1:F1}, {2:F1})", x, y, z);
}
public string ToString(string format)
{
return string.Format("({0}, {1}, {2})", x.ToString(format), y.ToString(format), z.ToString(format));
}
// public static Vector3 RotateAround(Vector3 npos, Vector3 nrot, float rotation) {
// Matrix4f matrix = new Matrix4f();
// Vector3 pos = new Vector3(npos.x, npos.y, npos.z);
// matrix.m03 = pos.x;
// matrix.m13 = pos.y;
// matrix.m23 = pos.z;
// Vector3 rot = new Vector3(nrot.x, nrot.y, nrot.z);
// Matrix4f.Rotate((float) (rotation / 180.0 * Math.PI), rot, matrix, matrix);
// return new Vector3(matrix.m03, matrix.m13, matrix.m23);
// }
}