|
| 1 | +using Rage; |
| 2 | + |
| 3 | +namespace IPT.Common.API |
| 4 | +{ |
| 5 | + /// <summary> |
| 6 | + /// Math helper. |
| 7 | + /// </summary> |
| 8 | + public static class MathHelperExtensions |
| 9 | + { |
| 10 | + /// <summary> |
| 11 | + /// Calculates a forward vector. |
| 12 | + /// </summary> |
| 13 | + /// <param name="heading">The heading for the vector.</param> |
| 14 | + /// <returns>A forward vector.</returns> |
| 15 | + public static Vector3 GetForwardVector(float heading) |
| 16 | + { |
| 17 | + var headingRads = MathHelper.ConvertDegreesToRadians(90f - heading); |
| 18 | + float x = System.Convert.ToSingle(-System.Math.Cos(headingRads)); |
| 19 | + float y = System.Convert.ToSingle(System.Math.Sin(headingRads)); |
| 20 | + return new Vector3(x, y, 0f); |
| 21 | + } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Gets the angle (0-180 degrees) between the source and target entities. |
| 25 | + /// </summary> |
| 26 | + /// <param name="source">The source entity.</param> |
| 27 | + /// <param name="target">The target entity.</param> |
| 28 | + /// <returns>A double indicating the angle in degrees.</returns> |
| 29 | + public static double GetVectorAngle(Entity source, Entity target) |
| 30 | + { |
| 31 | + var targetVector = target.Position - source.Position; |
| 32 | + var dotProduct = Vector3.Dot(source.ForwardVector.ToNormalized(), targetVector.ToNormalized()); |
| 33 | + return MathHelper.ConvertRadiansToDegrees(System.Math.Acos(dotProduct)); |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Gets the angle (0-180 degrees) between the source and target positions given the heading. |
| 38 | + /// </summary> |
| 39 | + /// <param name="sourcePosition">The source position.</param> |
| 40 | + /// <param name="targetPosition">The target position.</param> |
| 41 | + /// <param name="heading">The in-game heading (where 0 degrees is due north).</param> |
| 42 | + /// <returns>The offset from 0 degrees (straight towards) between the source and target along a heading.</returns> |
| 43 | + public static double GetVectorAngle(Vector3 sourcePosition, Vector3 targetPosition, float heading) |
| 44 | + { |
| 45 | + var targetVector = targetPosition - sourcePosition; |
| 46 | + var forwardVector = GetForwardVector(heading); |
| 47 | + var dotProduct = Vector3.Dot(forwardVector.ToNormalized(), targetVector.ToNormalized()); |
| 48 | + return MathHelper.ConvertRadiansToDegrees(System.Math.Acos(dotProduct)); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Wraps an angle. |
| 53 | + /// </summary> |
| 54 | + /// <param name="angle">The angle.</param> |
| 55 | + /// <returns>The wrapped angle.</returns> |
| 56 | + public static float WrapAngle(float angle) |
| 57 | + { |
| 58 | + angle %= 360f; |
| 59 | + if (angle > 180f) |
| 60 | + { |
| 61 | + angle -= 360f; |
| 62 | + } |
| 63 | + else if (angle < -180f) |
| 64 | + { |
| 65 | + angle += 360f; |
| 66 | + } |
| 67 | + |
| 68 | + return angle; |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments