Falco Engine 3.9.0.1 (beta)
Vector2.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7
8namespace FalcoEngine
9{
10 public struct Vector2
11 {
15 public float x;
16
20 public float y;
21
22 private static readonly Vector2 zeroVector = new Vector2(0f, 0f);
23
24 private static readonly Vector2 oneVector = new Vector2(1f, 1f);
25
26 private static readonly Vector2 upVector = new Vector2(0f, 1f);
27
28 private static readonly Vector2 downVector = new Vector2(0f, -1f);
29
30 private static readonly Vector2 leftVector = new Vector2(-1f, 0f);
31
32 private static readonly Vector2 rightVector = new Vector2(1f, 0f);
33
34 private static readonly Vector2 positiveInfinityVector = new Vector2(float.PositiveInfinity, float.PositiveInfinity);
35
36 private static readonly Vector2 negativeInfinityVector = new Vector2(float.NegativeInfinity, float.NegativeInfinity);
37
38 public const float kEpsilon = 1E-05f;
39
40 public const float kEpsilonNormalSqrt = 1E-15f;
41
42 public float this[int index]
43 {
44 get
45 {
46 switch (index)
47 {
48 case 0:
49 return x;
50 case 1:
51 return y;
52 default:
53 throw new IndexOutOfRangeException("Invalid Vector2 index!");
54 }
55 }
56 set
57 {
58 switch (index)
59 {
60 case 0:
61 x = value;
62 break;
63 case 1:
64 y = value;
65 break;
66 default:
67 throw new IndexOutOfRangeException("Invalid Vector2 index!");
68 }
69 }
70 }
71
76 {
77 get
78 {
79 Vector2 result = new Vector2(x, y);
80 result.Normalize();
81 return result;
82 }
83 }
84
88 public float magnitude => (float)Math.Sqrt((double)(x * x + y * y));
89
93 public float sqrMagnitude => x * x + y * y;
94
98 public static Vector2 zero => zeroVector;
99
103 public static Vector2 one => oneVector;
104
108 public static Vector2 up => upVector;
109
113 public static Vector2 down => downVector;
114
118 public static Vector2 left => leftVector;
119
123 public static Vector2 right => rightVector;
124
128 public static Vector2 positiveInfinity => positiveInfinityVector;
129
133 public static Vector2 negativeInfinity => negativeInfinityVector;
134
140 public Vector2(float x, float y)
141 {
142 this.x = x;
143 this.y = y;
144 }
145
151 public void Set(float newX, float newY)
152 {
153 x = newX;
154 y = newY;
155 }
156
163 public static Vector2 Lerp(Vector2 a, Vector2 b, float t)
164 {
165 t = Mathf.Clamp01(t);
166 return new Vector2(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t);
167 }
168
175 public static Vector2 LerpUnclamped(Vector2 a, Vector2 b, float t)
176 {
177 return new Vector2(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t);
178 }
179
186 public static Vector2 MoveTowards(Vector2 current, Vector2 target, float maxDistanceDelta)
187 {
188 float num = target.x - current.x;
189 float num2 = target.y - current.y;
190 float num3 = num * num + num2 * num2;
191 if (num3 == 0f || (maxDistanceDelta >= 0f && num3 <= maxDistanceDelta * maxDistanceDelta))
192 {
193 return target;
194 }
195 float num4 = (float)Math.Sqrt((double)num3);
196 return new Vector2(current.x + num / num4 * maxDistanceDelta, current.y + num2 / num4 * maxDistanceDelta);
197 }
198
204 public static Vector2 Scale(Vector2 a, Vector2 b)
205 {
206 return new Vector2(a.x * b.x, a.y * b.y);
207 }
208
213 public void Scale(Vector2 scale)
214 {
215 x *= scale.x;
216 y *= scale.y;
217 }
218
222 public void Normalize()
223 {
224 float magnitude = this.magnitude;
225 if (magnitude > 1E-05f)
226 {
227 this /= magnitude;
228 }
229 else
230 {
231 this = zero;
232 }
233 }
234
239 public override string ToString()
240 {
241 return String.Format("({0:F4}, {1:F4})", x, y);
242 }
243
248 public string ToString(string format)
249 {
250 return String.Format("({0}, {1})", x.ToString(format, CultureInfo.InvariantCulture.NumberFormat), y.ToString(format, CultureInfo.InvariantCulture.NumberFormat));
251 }
252
253 public override int GetHashCode()
254 {
255 return x.GetHashCode() ^ (y.GetHashCode() << 2);
256 }
257
262 public override bool Equals(object other)
263 {
264 if (!(other is Vector2))
265 {
266 return false;
267 }
268 return Equals((Vector2)other);
269 }
270
271 public bool Equals(Vector2 other)
272 {
273 return x == other.x && y == other.y;
274 }
275
281 public static Vector2 Reflect(Vector2 inDirection, Vector2 inNormal)
282 {
283 float num = -2f * Dot(inNormal, inDirection);
284 return new Vector2(num * inNormal.x + inDirection.x, num * inNormal.y + inDirection.y);
285 }
286
294 public static Vector2 Perpendicular(Vector2 inDirection)
295 {
296 return new Vector2(0f - inDirection.y, inDirection.x);
297 }
298
304 public static float Dot(Vector2 lhs, Vector2 rhs)
305 {
306 return lhs.x * rhs.x + lhs.y * rhs.y;
307 }
308
314 public static float Angle(Vector2 from, Vector2 to)
315 {
316 float num = (float)Math.Sqrt((double)(from.sqrMagnitude * to.sqrMagnitude));
317 if (num < 1E-15f)
318 {
319 return 0f;
320 }
321 float num2 = Mathf.Clamp(Dot(from, to) / num, -1f, 1f);
322 return (float)Math.Acos((double)num2) * 57.29578f;
323 }
324
330 public static float SignedAngle(Vector2 from, Vector2 to)
331 {
332 float num = Angle(from, to);
333 float num2 = Mathf.Sign(from.x * to.y - from.y * to.x);
334 return num * num2;
335 }
336
342 public static float Distance(Vector2 a, Vector2 b)
343 {
344 float num = a.x - b.x;
345 float num2 = a.y - b.y;
346 return (float)Math.Sqrt((double)(num * num + num2 * num2));
347 }
348
354 public static Vector2 ClampMagnitude(Vector2 vector, float maxLength)
355 {
356 float sqrMagnitude = vector.sqrMagnitude;
357 if (sqrMagnitude > maxLength * maxLength)
358 {
359 float num = (float)Math.Sqrt((double)sqrMagnitude);
360 float num2 = vector.x / num;
361 float num3 = vector.y / num;
362 return new Vector2(num2 * maxLength, num3 * maxLength);
363 }
364 return vector;
365 }
366
367 public static float SqrMagnitude(Vector2 a)
368 {
369 return a.x * a.x + a.y * a.y;
370 }
371
372 public float SqrMagnitude()
373 {
374 return x * x + y * y;
375 }
376
382 public static Vector2 Min(Vector2 lhs, Vector2 rhs)
383 {
384 return new Vector2(Mathf.Min(lhs.x, rhs.x), Mathf.Min(lhs.y, rhs.y));
385 }
386
392 public static Vector2 Max(Vector2 lhs, Vector2 rhs)
393 {
394 return new Vector2(Mathf.Max(lhs.x, rhs.x), Mathf.Max(lhs.y, rhs.y));
395 }
396
397 public static Vector2 SmoothDamp(Vector2 current, Vector2 target, ref Vector2 currentVelocity, float smoothTime, float maxSpeed)
398 {
399 float deltaTime = Time.deltaTime;
400 return SmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, deltaTime);
401 }
402
403 public static Vector2 SmoothDamp(Vector2 current, Vector2 target, ref Vector2 currentVelocity, float smoothTime)
404 {
405 float deltaTime = Time.deltaTime;
406 float maxSpeed = float.PositiveInfinity;
407 return SmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, deltaTime);
408 }
409
410 public static Vector2 SmoothDamp(Vector2 current, Vector2 target, ref Vector2 currentVelocity, float smoothTime, float maxSpeed, float deltaTime)
411 {
412 smoothTime = Mathf.Max(0.0001f, smoothTime);
413 float num = 2f / smoothTime;
414 float num2 = num * deltaTime;
415 float num3 = 1f / (1f + num2 + 0.48f * num2 * num2 + 0.235f * num2 * num2 * num2);
416 float num4 = current.x - target.x;
417 float num5 = current.y - target.y;
418 Vector2 vector = target;
419 float num6 = maxSpeed * smoothTime;
420 float num7 = num6 * num6;
421 float num8 = num4 * num4 + num5 * num5;
422 if (num8 > num7)
423 {
424 float num9 = (float)Math.Sqrt((double)num8);
425 num4 = num4 / num9 * num6;
426 num5 = num5 / num9 * num6;
427 }
428 target.x = current.x - num4;
429 target.y = current.y - num5;
430 float num10 = (currentVelocity.x + num * num4) * deltaTime;
431 float num11 = (currentVelocity.y + num * num5) * deltaTime;
432 currentVelocity.x = (currentVelocity.x - num * num10) * num3;
433 currentVelocity.y = (currentVelocity.y - num * num11) * num3;
434 float num12 = target.x + (num4 + num10) * num3;
435 float num13 = target.y + (num5 + num11) * num3;
436 float num14 = vector.x - current.x;
437 float num15 = vector.y - current.y;
438 float num16 = num12 - vector.x;
439 float num17 = num13 - vector.y;
440 if (num14 * num16 + num15 * num17 > 0f)
441 {
442 num12 = vector.x;
443 num13 = vector.y;
444 currentVelocity.x = (num12 - vector.x) / deltaTime;
445 currentVelocity.y = (num13 - vector.y) / deltaTime;
446 }
447 return new Vector2(num12, num13);
448 }
449
450 public static Vector2 operator +(Vector2 a, Vector2 b)
451 {
452 return new Vector2(a.x + b.x, a.y + b.y);
453 }
454
455 public static Vector2 operator -(Vector2 a, Vector2 b)
456 {
457 return new Vector2(a.x - b.x, a.y - b.y);
458 }
459
460 public static Vector2 operator *(Vector2 a, Vector2 b)
461 {
462 return new Vector2(a.x * b.x, a.y * b.y);
463 }
464
465 public static Vector2 operator /(Vector2 a, Vector2 b)
466 {
467 return new Vector2(a.x / b.x, a.y / b.y);
468 }
469
470 public static Vector2 operator -(Vector2 a)
471 {
472 return new Vector2(0f - a.x, 0f - a.y);
473 }
474
475 public static Vector2 operator *(Vector2 a, float d)
476 {
477 return new Vector2(a.x * d, a.y * d);
478 }
479
480 public static Vector2 operator *(float d, Vector2 a)
481 {
482 return new Vector2(a.x * d, a.y * d);
483 }
484
485 public static Vector2 operator /(Vector2 a, float d)
486 {
487 return new Vector2(a.x / d, a.y / d);
488 }
489
490 public static bool operator ==(Vector2 lhs, Vector2 rhs)
491 {
492 float num = lhs.x - rhs.x;
493 float num2 = lhs.y - rhs.y;
494 return num * num + num2 * num2 < 9.99999944E-11f;
495 }
496
497 public static bool operator !=(Vector2 lhs, Vector2 rhs)
498 {
499 return !(lhs == rhs);
500 }
501
502 public static implicit operator Vector2(Vector3 v)
503 {
504 return new Vector2(v.x, v.y);
505 }
506
507 public static implicit operator Vector3(Vector2 v)
508 {
509 return new Vector3(v.x, v.y, 0f);
510 }
511 }
512}
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 Clamp01(float value)
Definition: Mathf.cs:436
static float Sign(float f)
Definition: Mathf.cs:389
static float deltaTime
The interval in seconds from the last frame to the current one
Definition: Time.cs:17
const float kEpsilon
Definition: Vector2.cs:38
const float kEpsilonNormalSqrt
Definition: Vector2.cs:40
static Vector2 SmoothDamp(Vector2 current, Vector2 target, ref Vector2 currentVelocity, float smoothTime, float maxSpeed)
Definition: Vector2.cs:397
float SqrMagnitude()
Definition: Vector2.cs:372
void Scale(Vector2 scale)
Definition: Vector2.cs:213
static Vector2 Scale(Vector2 a, Vector2 b)
Definition: Vector2.cs:204
static Vector2 Lerp(Vector2 a, Vector2 b, float t)
Definition: Vector2.cs:163
bool Equals(Vector2 other)
Definition: Vector2.cs:271
static Vector2 positiveInfinity
Definition: Vector2.cs:128
static Vector2 Perpendicular(Vector2 inDirection)
Definition: Vector2.cs:294
static Vector2 ClampMagnitude(Vector2 vector, float maxLength)
Definition: Vector2.cs:354
string ToString(string format)
Definition: Vector2.cs:248
static Vector2 SmoothDamp(Vector2 current, Vector2 target, ref Vector2 currentVelocity, float smoothTime)
Definition: Vector2.cs:403
static float Distance(Vector2 a, Vector2 b)
Definition: Vector2.cs:342
override bool Equals(object other)
Definition: Vector2.cs:262
static bool operator==(Vector2 lhs, Vector2 rhs)
Definition: Vector2.cs:490
static Vector2 left
Definition: Vector2.cs:118
override string ToString()
Definition: Vector2.cs:239
static float SqrMagnitude(Vector2 a)
Definition: Vector2.cs:367
static float Angle(Vector2 from, Vector2 to)
Definition: Vector2.cs:314
static Vector2 operator*(Vector2 a, Vector2 b)
Definition: Vector2.cs:460
static Vector2 SmoothDamp(Vector2 current, Vector2 target, ref Vector2 currentVelocity, float smoothTime, float maxSpeed, float deltaTime)
Definition: Vector2.cs:410
override int GetHashCode()
Definition: Vector2.cs:253
static Vector2 MoveTowards(Vector2 current, Vector2 target, float maxDistanceDelta)
Definition: Vector2.cs:186
static Vector2 down
Definition: Vector2.cs:113
static Vector2 negativeInfinity
Definition: Vector2.cs:133
static Vector2 zero
Definition: Vector2.cs:98
static Vector2 operator-(Vector2 a, Vector2 b)
Definition: Vector2.cs:455
static float Dot(Vector2 lhs, Vector2 rhs)
Definition: Vector2.cs:304
static Vector2 Max(Vector2 lhs, Vector2 rhs)
Definition: Vector2.cs:392
static Vector2 operator+(Vector2 a, Vector2 b)
Definition: Vector2.cs:450
static Vector2 up
Definition: Vector2.cs:108
static Vector2 right
Definition: Vector2.cs:123
static Vector2 LerpUnclamped(Vector2 a, Vector2 b, float t)
Definition: Vector2.cs:175
Vector2(float x, float y)
Definition: Vector2.cs:140
static bool operator!=(Vector2 lhs, Vector2 rhs)
Definition: Vector2.cs:497
static float SignedAngle(Vector2 from, Vector2 to)
Definition: Vector2.cs:330
static Vector2 Reflect(Vector2 inDirection, Vector2 inNormal)
Definition: Vector2.cs:281
void Set(float newX, float newY)
Definition: Vector2.cs:151
Vector2 normalized
Definition: Vector2.cs:76
static Vector2 one
Definition: Vector2.cs:103
float sqrMagnitude
Definition: Vector2.cs:93
static Vector2 Min(Vector2 lhs, Vector2 rhs)
Definition: Vector2.cs:382
static Vector2 operator/(Vector2 a, Vector2 b)
Definition: Vector2.cs:465