Falco Engine 3.9.0.1 (beta)
Vector3.cs
Go to the documentation of this file.
1
2using System;
3using System.Globalization;
4
5namespace FalcoEngine
6{
7 //[StructLayout(LayoutKind.Sequential)]
8 public struct Vector3
9 {
10 public float x;
11 public float y;
12 public float z;
13
14 public const float kEpsilon = 1E-05f;
15 public const float kEpsilonNormalSqrt = 1E-15f;
16
17 public Vector3(float x, float y, float z)
18 {
19 this.x = x;
20 this.y = y;
21 this.z = z;
22 }
23
24 public static Vector3 operator + (Vector3 a, Vector3 b)
25 {
26 return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
27 }
28
29 public static Vector3 operator -(Vector3 a, Vector3 b)
30 {
31 return new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
32 }
33
34 public static Vector3 operator -(Vector3 a)
35 {
36 return new Vector3(0f - a.x, 0f - a.y, 0f - a.z);
37 }
38
39 public static Vector3 operator *(Vector3 a, float b)
40 {
41 return new Vector3(a.x * b, a.y * b, a.z * b);
42 }
43
44 public static Vector3 operator *(float d, Vector3 a)
45 {
46 return new Vector3(a.x * d, a.y * d, a.z * d);
47 }
48
49 public static Vector3 operator /(Vector3 a, float d)
50 {
51 return new Vector3(a.x / d, a.y / d, a.z / d);
52 }
53
54 public static bool operator ==(Vector3 lhs, Vector3 rhs)
55 {
56 return SqrMagnitude(lhs - rhs) < 9.99999944E-11f;
57 }
58
59 public static bool operator !=(Vector3 lhs, Vector3 rhs)
60 {
61 return !(lhs == rhs);
62 }
63
67 public static Vector3 zero { get; } = new Vector3(0f, 0f, 0f);
68
72 public static Vector3 one { get; } = new Vector3(1f, 1f, 1f);
73
77 public static Vector3 forward { get; } = new Vector3(0f, 0f, 1f);
78
82 public static Vector3 back { get; } = new Vector3(0f, 0f, -1f);
83
87 public static Vector3 up { get; } = new Vector3(0f, 1f, 0f);
88
92 public static Vector3 down { get; } = new Vector3(0f, -1f, 0f);
93
97 public static Vector3 left { get; } = new Vector3(-1f, 0f, 0f);
98
102 public static Vector3 right { get; } = new Vector3(1f, 0f, 0f);
103
107 public static Vector3 positiveInfinity { get; } = new Vector3(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity);
108
112 public static Vector3 negativeInfinity { get; } = new Vector3(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity);
113
114 public float magnitude => Mathf.Sqrt(x * x + y * y + z * z);
115
119 public float sqrMagnitude => x * x + y * y + z * z;
120
124 public Vector3 normalized => Normalize(this);
125
132 public static Vector3 Lerp(Vector3 a, Vector3 b, float t)
133 {
134 t = Mathf.Clamp01(t);
135 return new Vector3(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t);
136 }
137
144 public static Vector3 LerpUnclamped(Vector3 a, Vector3 b, float t)
145 {
146 return new Vector3(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t);
147 }
148
153 public void Scale(Vector3 scale)
154 {
155 x *= scale.x;
156 y *= scale.y;
157 z *= scale.z;
158 }
159
160 public override int GetHashCode()
161 {
162 return x.GetHashCode() ^ (y.GetHashCode() << 2) ^ (z.GetHashCode() >> 2);
163 }
164
169 public override bool Equals(object other)
170 {
171 if (!(other is Vector3))
172 {
173 return false;
174 }
175 return Equals((Vector3)other);
176 }
177
178 public bool Equals(Vector3 other)
179 {
180 return x.Equals(other.x) && y.Equals(other.y) && z.Equals(other.z);
181 }
182
188 public static Vector3 Cross(Vector3 lhs, Vector3 rhs)
189 {
190 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);
191 }
192
198 public static Vector3 Reflect(Vector3 inDirection, Vector3 inNormal)
199 {
200 return -2f * Dot(inNormal, inDirection) * inNormal + inDirection;
201 }
202
207 public static Vector3 Normalize(Vector3 value)
208 {
209 float num = Magnitude(value);
210 if (num > 1E-05f)
211 {
212 return value / num;
213 }
214 return zero;
215 }
216
217 public void Normalize()
218 {
219 float num = Magnitude(this);
220 if (num > 1E-05f)
221 {
222 this /= num;
223 }
224 else
225 {
226 this = zero;
227 }
228 }
229
235 public static float Dot(Vector3 lhs, Vector3 rhs)
236 {
237 return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
238 }
239
245 public static Vector3 Project(Vector3 vector, Vector3 onNormal)
246 {
247 float num = Dot(onNormal, onNormal);
248 if (num < kEpsilon)
249 {
250 return zero;
251 }
252 return onNormal * Dot(vector, onNormal) / num;
253 }
254
260 public static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal)
261 {
262 return vector - Project(vector, planeNormal);
263 }
264
273 public static float Angle(Vector3 from, Vector3 to)
274 {
275 float num = Mathf.Sqrt(from.sqrMagnitude * to.sqrMagnitude);
276 if (num < 1E-15f)
277 {
278 return 0f;
279 }
280 float f = Mathf.Clamp(Dot(from, to) / num, -1f, 1f);
281 return Mathf.Acos(f) * 57.29578f;
282 }
283
290 public static float SignedAngle(Vector3 from, Vector3 to, Vector3 axis)
291 {
292 float num = Angle(from, to);
293 float num2 = Mathf.Sign(Dot(axis, Cross(from, to)));
294 return num * num2;
295 }
296
302 public static float Distance(Vector3 a, Vector3 b)
303 {
304 Vector3 vector = new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
305 return Mathf.Sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z);
306 }
307
313 public static Vector3 ClampMagnitude(Vector3 vector, float maxLength)
314 {
315 if (vector.sqrMagnitude > maxLength * maxLength)
316 {
317 return vector.normalized * maxLength;
318 }
319 return vector;
320 }
321
322 public static float Magnitude(Vector3 vector)
323 {
324 return Mathf.Sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z);
325 }
326
327 public static float SqrMagnitude(Vector3 vector)
328 {
329 return vector.x * vector.x + vector.y * vector.y + vector.z * vector.z;
330 }
331
337 public static Vector3 Min(Vector3 lhs, Vector3 rhs)
338 {
339 return new Vector3(Mathf.Min(lhs.x, rhs.x), Mathf.Min(lhs.y, rhs.y), Mathf.Min(lhs.z, rhs.z));
340 }
341
347 public static Vector3 Max(Vector3 lhs, Vector3 rhs)
348 {
349 return new Vector3(Mathf.Max(lhs.x, rhs.x), Mathf.Max(lhs.y, rhs.y), Mathf.Max(lhs.z, rhs.z));
350 }
351
356 public override string ToString()
357 {
358 return String.Format("({0:F4}, {1:F4}, {2:F4})", x, y, z);
359 }
360
365 public string ToString(string format)
366 {
367 return String.Format("({0}, {1}, {2})", x.ToString(format, CultureInfo.InvariantCulture.NumberFormat), y.ToString(format, CultureInfo.InvariantCulture.NumberFormat), z.ToString(format, CultureInfo.InvariantCulture.NumberFormat));
368 }
369 }
370}
static float Acos(float f)
Definition: Mathf.cs:93
static float Min(float a, float b)
Definition: Mathf.cs:150
static float Clamp(float value, float min, float max)
Definition: Mathf.cs:400
static float Max(float a, float b)
Definition: Mathf.cs:220
static float Sqrt(float f)
Definition: Mathf.cs:121
static float Clamp01(float value)
Definition: Mathf.cs:436
static float Sign(float f)
Definition: Mathf.cs:389
static Vector3 Normalize(Vector3 value)
Definition: Vector3.cs:207
override bool Equals(object other)
Definition: Vector3.cs:169
static float Magnitude(Vector3 vector)
Definition: Vector3.cs:322
static float SignedAngle(Vector3 from, Vector3 to, Vector3 axis)
Definition: Vector3.cs:290
static Vector3 one
Definition: Vector3.cs:72
static Vector3 LerpUnclamped(Vector3 a, Vector3 b, float t)
Definition: Vector3.cs:144
bool Equals(Vector3 other)
Definition: Vector3.cs:178
static Vector3 Lerp(Vector3 a, Vector3 b, float t)
Definition: Vector3.cs:132
const float kEpsilonNormalSqrt
Definition: Vector3.cs:15
static float SqrMagnitude(Vector3 vector)
Definition: Vector3.cs:327
Vector3 normalized
Definition: Vector3.cs:124
static Vector3 right
Definition: Vector3.cs:102
static Vector3 ClampMagnitude(Vector3 vector, float maxLength)
Definition: Vector3.cs:313
void Scale(Vector3 scale)
Definition: Vector3.cs:153
static Vector3 Min(Vector3 lhs, Vector3 rhs)
Definition: Vector3.cs:337
static Vector3 back
Definition: Vector3.cs:82
static float Distance(Vector3 a, Vector3 b)
Definition: Vector3.cs:302
static Vector3 Max(Vector3 lhs, Vector3 rhs)
Definition: Vector3.cs:347
static bool operator!=(Vector3 lhs, Vector3 rhs)
Definition: Vector3.cs:59
static Vector3 operator+(Vector3 a, Vector3 b)
Definition: Vector3.cs:24
static float Dot(Vector3 lhs, Vector3 rhs)
Definition: Vector3.cs:235
static float Angle(Vector3 from, Vector3 to)
Definition: Vector3.cs:273
static Vector3 operator*(Vector3 a, float b)
Definition: Vector3.cs:39
static Vector3 operator-(Vector3 a, Vector3 b)
Definition: Vector3.cs:29
override int GetHashCode()
Definition: Vector3.cs:160
override string ToString()
Definition: Vector3.cs:356
static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal)
Definition: Vector3.cs:260
static Vector3 positiveInfinity
Definition: Vector3.cs:107
static Vector3 Reflect(Vector3 inDirection, Vector3 inNormal)
Definition: Vector3.cs:198
Vector3(float x, float y, float z)
Definition: Vector3.cs:17
static Vector3 forward
Definition: Vector3.cs:77
static Vector3 zero
Definition: Vector3.cs:67
static bool operator==(Vector3 lhs, Vector3 rhs)
Definition: Vector3.cs:54
string ToString(string format)
Definition: Vector3.cs:365
const float kEpsilon
Definition: Vector3.cs:14
static Vector3 down
Definition: Vector3.cs:92
static Vector3 negativeInfinity
Definition: Vector3.cs:112
static Vector3 Project(Vector3 vector, Vector3 onNormal)
Definition: Vector3.cs:245
static Vector3 up
Definition: Vector3.cs:87
static Vector3 Cross(Vector3 lhs, Vector3 rhs)
Definition: Vector3.cs:188
static Vector3 operator/(Vector3 a, float d)
Definition: Vector3.cs:49
static Vector3 left
Definition: Vector3.cs:97