|
1 | | -namespace IPT.Common.API |
| 1 | +using Rage; |
| 2 | + |
| 3 | +namespace IPT.Common.API |
2 | 4 | { |
3 | 5 | /// <summary> |
4 | 6 | /// Additional math-related functions. |
5 | 7 | /// </summary> |
6 | 8 | public static class Math |
7 | 9 | { |
| 10 | + /// <summary> |
| 11 | + /// A value used for converting radians to degrees. |
| 12 | + /// </summary> |
| 13 | + public static readonly double Rad2Deg = 180.0 / System.Math.PI; |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// A value used for converting degrees to radians. |
| 17 | + /// </summary> |
| 18 | + public static readonly double Deg2Rad = System.Math.PI / 180.0; |
| 19 | + |
8 | 20 | /// <summary> |
9 | 21 | /// Clamps a float between two (inclusive) values. |
10 | 22 | /// </summary> |
@@ -81,5 +93,38 @@ public static int Snap(int value, int min, int max, int increment) |
81 | 93 |
|
82 | 94 | return cValue; |
83 | 95 | } |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Gets the angle (0-180 degrees) between the source and target entities. |
| 99 | + /// </summary> |
| 100 | + /// <param name="source">The source entity.</param> |
| 101 | + /// <param name="target">The target entity.</param> |
| 102 | + /// <returns>A double indicating the angle in degrees.</returns> |
| 103 | + public static double GetVectorAngle(Entity source, Entity target) |
| 104 | + { |
| 105 | + var targetVector = target.Position - source.Position; |
| 106 | + var dotProduct = Vector3.Dot(source.ForwardVector.ToNormalized(), targetVector.ToNormalized()); |
| 107 | + return ConvertRadiansToDegrees(System.Math.Acos(dotProduct)); |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Converts radians into degrees. |
| 112 | + /// </summary> |
| 113 | + /// <param name="radians">The initial value in radians.</param> |
| 114 | + /// <returns>A double value representing the radians in degrees.</returns> |
| 115 | + public static double ConvertRadiansToDegrees(double radians) |
| 116 | + { |
| 117 | + return radians * Rad2Deg; |
| 118 | + } |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Converts degrees into radians. |
| 122 | + /// </summary> |
| 123 | + /// <param name="degrees">The initial value in degrees.</param> |
| 124 | + /// <returns>A double value representing the degrees in radians.</returns> |
| 125 | + public static double ConvertDegreesToRadians(double degrees) |
| 126 | + { |
| 127 | + return degrees * Deg2Rad; |
| 128 | + } |
84 | 129 | } |
85 | 130 | } |
0 commit comments