Barotrauma Client Doc
VectorExtensions.cs
1 using System;
2 using Microsoft.Xna.Framework;
3 
4 namespace Barotrauma.Extensions
5 {
6  public static class VectorExtensions
7  {
13  public static float Angle(this Vector2 from, Vector2 to)
14  {
15  return (float)Math.Acos(MathHelper.Clamp(Vector2.Dot(Vector2.Normalize(from), Vector2.Normalize(to)), -1f, 1f));
16  }
17 
21  public static Vector2 Forward(float radians, float length = 1)
22  {
23  return new Vector2((float)Math.Cos(radians), (float)Math.Sin(radians)) * length;
24  }
25 
29  public static Vector2 Backward(float radians, float length = 1)
30  {
31  return -Forward(radians, length);
32  }
33 
37  public static Vector2 ForwardFlipped(float radians, float length = 1)
38  {
39  return new Vector2((float)Math.Sin(radians), (float)Math.Cos(radians)) * length;
40  }
41 
45  public static Vector2 BackwardFlipped(float radians, float length = 1)
46  {
47  return -ForwardFlipped(radians, length);
48  }
49 
53  public static Vector2 Right(this Vector2 forward)
54  {
55  var normV = Vector2.Normalize(forward);
56  return new Vector2(normV.Y, -normV.X);
57  }
58 
62  public static Vector2 Left(this Vector2 forward)
63  {
64  return -forward.Right();
65  }
66 
70  public static Vector2 TransformVector(this Vector2 v, Vector2 up)
71  {
72  up = Vector2.Normalize(up);
73  return (up * v.Y) + (up.Right() * v.X);
74  }
75 
79  public static Vector2 Flip(this Vector2 v) => new Vector2(v.Y, v.X);
80 
84  public static float Combine(this Vector2 v) => v.X + v.Y;
85 
86  public static Vector2 Clamp(this Vector2 v, Vector2 min, Vector2 max)
87  {
88  return Vector2.Clamp(v, min, max);
89  }
90 
91  public static bool NearlyEquals(this Vector2 v, Vector2 other)
92  {
93  return MathUtils.NearlyEqual(v.X, other.X) && MathUtils.NearlyEqual(v.Y, other.Y);
94  }
95 
96  public static Vector2 Pad(this Vector2 v, Vector4 padding)
97  {
98  v.X += padding.X + padding.Z;
99  v.Y += padding.Y + padding.W;
100  return v;
101  }
102  }
103 }