Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

BurstMath.cs 3.9KB

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