A game about forced loneliness, made by TACStudios
at master 94 lines 4.0 kB view raw
1using UnityEngine; 2 3namespace Unity.Burst.Editor 4{ 5 internal static class BurstMath 6 { 7 private const float HitBoxAdjust = 2f; 8 9 /// <summary> 10 /// Rotates <see cref="point"/> around a pivot point according to angle given in degrees. 11 /// </summary> 12 /// <param name="angle">Angle in degrees.</param> 13 /// <param name="point">Point to rotate.</param> 14 /// <param name="pivotPoint">Pivot point to rotate around.</param> 15 /// <returns>The rotated point.</returns> 16 internal static Vector2 AnglePoint(float angle, Vector2 point, Vector2 pivotPoint) 17 { 18 // https://matthew-brett.github.io/teaching/rotation_2d.html 19 // Problem Angle is calculates as angle clockwise, and here we use it as it was counterclockwise! 20 var s = Mathf.Sin(angle); 21 var c = Mathf.Cos(angle); 22 point -= pivotPoint; 23 24 return new Vector2(c * point.x - s * point.y, s * point.x + c * point.y) + pivotPoint; 25 } 26 27 /// <summary> 28 /// Calculated angle in degrees between two points. 29 /// </summary> 30 /// <param name="start">Starting point.</param> 31 /// <param name="end">End point.</param> 32 /// <returns>Angle in degrees.</returns> 33 internal static float CalculateAngle(Vector2 start, Vector2 end) 34 { 35 var distance = end - start; 36 var angle = Mathf.Rad2Deg * Mathf.Atan(distance.y / distance.x); 37 if (distance.x < 0) 38 { 39 angle += 180; 40 } 41 42 return angle; 43 } 44 45 /// <summary> 46 /// Checks if <see cref="point"/> is within <see cref="rect"/> enlarged by <see cref="HitBoxAdjust"/>. 47 /// </summary> 48 /// <param name="rect">Give rect to enlarge and check with.</param> 49 /// <param name="point">Given point to match in enlarged <see cref="rect"/>.</param> 50 /// <returns>Whether <see cref="point"/> is within enlarged <see cref="rect"/>.</returns> 51 internal static bool AdjustedContains(Rect rect, Vector2 point) 52 { 53 return rect.yMax + HitBoxAdjust >= point.y && rect.yMin - HitBoxAdjust <= point.y 54 && rect.xMax + HitBoxAdjust >= point.x && rect.xMin - HitBoxAdjust <= point.x; 55 } 56 57 /// <summary> 58 /// Checks if <see cref="num"/> is within the closed interval defined by endPoint1 and endPoint2. 59 /// </summary> 60 /// <param name="endPoint1">One side of range.</param> 61 /// <param name="endPoint2">Other side of range.</param> 62 /// <param name="num">Number to check if it is in between <see cref="endPoint1"/> and <see cref="endPoint2"/>.</param> 63 /// <returns>Whether <see cref="num"/> is within given range.</returns> 64 internal static bool WithinRange(float endPoint1, float endPoint2, float num) 65 { 66 float start, end; 67 if (endPoint1 < endPoint2) 68 { 69 start = endPoint1; 70 end = endPoint2; 71 } 72 else 73 { 74 start = endPoint2; 75 end = endPoint1; 76 } 77 return start <= num && num <= end; 78 } 79 80 /// <summary> 81 /// Rounds down to nearest amount specified by <see cref="to"/>. 82 /// </summary> 83 /// <param name="number">Number to round down.</param> 84 /// <param name="to">Specifies what amount to round down to.</param> 85 /// <returns><see cref="number"/> rounded down to amount <see cref="to"/>.</returns> 86 internal static float RoundDownToNearest(float number, float to) 87 { 88 //https://www.programmingnotes.org/7601/cs-how-to-round-a-number-to-the-nearest-x-using-cs/ 89 float inverse = 1 / to; 90 float dividend = Mathf.Floor(number * inverse); 91 return dividend / inverse; 92 } 93 } 94}