Mathf.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System;
  2. namespace Godot
  3. {
  4. public static class Mathf
  5. {
  6. public const float PI = 3.14159274f;
  7. public const float Epsilon = 1e-06f;
  8. private const float Deg2RadConst = 0.0174532924f;
  9. private const float Rad2DegConst = 57.29578f;
  10. public static float abs(float s)
  11. {
  12. return Math.Abs(s);
  13. }
  14. public static float acos(float s)
  15. {
  16. return (float)Math.Acos(s);
  17. }
  18. public static float asin(float s)
  19. {
  20. return (float)Math.Asin(s);
  21. }
  22. public static float atan(float s)
  23. {
  24. return (float)Math.Atan(s);
  25. }
  26. public static float atan2(float x, float y)
  27. {
  28. return (float)Math.Atan2(x, y);
  29. }
  30. public static Vector2 cartesian2polar(float x, float y)
  31. {
  32. return new Vector2(sqrt(x * x + y * y), atan2(y, x));
  33. }
  34. public static float ceil(float s)
  35. {
  36. return (float)Math.Ceiling(s);
  37. }
  38. public static float clamp(float val, float min, float max)
  39. {
  40. if (val < min)
  41. {
  42. return min;
  43. }
  44. else if (val > max)
  45. {
  46. return max;
  47. }
  48. return val;
  49. }
  50. public static float cos(float s)
  51. {
  52. return (float)Math.Cos(s);
  53. }
  54. public static float cosh(float s)
  55. {
  56. return (float)Math.Cosh(s);
  57. }
  58. public static int decimals(float step)
  59. {
  60. return decimals(step);
  61. }
  62. public static int decimals(decimal step)
  63. {
  64. return BitConverter.GetBytes(decimal.GetBits(step)[3])[2];
  65. }
  66. public static float deg2rad(float deg)
  67. {
  68. return deg * Deg2RadConst;
  69. }
  70. public static float ease(float s, float curve)
  71. {
  72. if (s < 0f)
  73. {
  74. s = 0f;
  75. }
  76. else if (s > 1.0f)
  77. {
  78. s = 1.0f;
  79. }
  80. if (curve > 0f)
  81. {
  82. if (curve < 1.0f)
  83. {
  84. return 1.0f - pow(1.0f - s, 1.0f / curve);
  85. }
  86. return pow(s, curve);
  87. }
  88. else if (curve < 0f)
  89. {
  90. if (s < 0.5f)
  91. {
  92. return pow(s * 2.0f, -curve) * 0.5f;
  93. }
  94. return (1.0f - pow(1.0f - (s - 0.5f) * 2.0f, -curve)) * 0.5f + 0.5f;
  95. }
  96. return 0f;
  97. }
  98. public static float exp(float s)
  99. {
  100. return (float)Math.Exp(s);
  101. }
  102. public static float floor(float s)
  103. {
  104. return (float)Math.Floor(s);
  105. }
  106. public static float fposmod(float x, float y)
  107. {
  108. if (x >= 0f)
  109. {
  110. return x % y;
  111. }
  112. else
  113. {
  114. return y - (-x % y);
  115. }
  116. }
  117. public static float lerp(float from, float to, float weight)
  118. {
  119. return from + (to - from) * clamp(weight, 0f, 1f);
  120. }
  121. public static float log(float s)
  122. {
  123. return (float)Math.Log(s);
  124. }
  125. public static int max(int a, int b)
  126. {
  127. return (a > b) ? a : b;
  128. }
  129. public static float max(float a, float b)
  130. {
  131. return (a > b) ? a : b;
  132. }
  133. public static int min(int a, int b)
  134. {
  135. return (a < b) ? a : b;
  136. }
  137. public static float min(float a, float b)
  138. {
  139. return (a < b) ? a : b;
  140. }
  141. public static int nearest_po2(int val)
  142. {
  143. val--;
  144. val |= val >> 1;
  145. val |= val >> 2;
  146. val |= val >> 4;
  147. val |= val >> 8;
  148. val |= val >> 16;
  149. val++;
  150. return val;
  151. }
  152. public static Vector2 polar2cartesian(float r, float th)
  153. {
  154. return new Vector2(r * cos(th), r * sin(th));
  155. }
  156. public static float pow(float x, float y)
  157. {
  158. return (float)Math.Pow(x, y);
  159. }
  160. public static float rad2deg(float rad)
  161. {
  162. return rad * Rad2DegConst;
  163. }
  164. public static float round(float s)
  165. {
  166. return (float)Math.Round(s);
  167. }
  168. public static float sign(float s)
  169. {
  170. return (s < 0f) ? -1f : 1f;
  171. }
  172. public static float sin(float s)
  173. {
  174. return (float)Math.Sin(s);
  175. }
  176. public static float sinh(float s)
  177. {
  178. return (float)Math.Sinh(s);
  179. }
  180. public static float sqrt(float s)
  181. {
  182. return (float)Math.Sqrt(s);
  183. }
  184. public static float stepify(float s, float step)
  185. {
  186. if (step != 0f)
  187. {
  188. s = floor(s / step + 0.5f) * step;
  189. }
  190. return s;
  191. }
  192. public static float tan(float s)
  193. {
  194. return (float)Math.Tan(s);
  195. }
  196. public static float tanh(float s)
  197. {
  198. return (float)Math.Tanh(s);
  199. }
  200. }
  201. }