Mathf.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. using System;
  2. #if REAL_T_IS_DOUBLE
  3. using real_t = System.Double;
  4. #else
  5. using real_t = System.Single;
  6. #endif
  7. namespace Godot
  8. {
  9. public static partial class Mathf
  10. {
  11. // Define constants with Decimal precision and cast down to double or float.
  12. public const real_t Tau = (real_t) 6.2831853071795864769252867666M; // 6.2831855f and 6.28318530717959
  13. public const real_t Pi = (real_t) 3.1415926535897932384626433833M; // 3.1415927f and 3.14159265358979
  14. public const real_t Inf = real_t.PositiveInfinity;
  15. public const real_t NaN = real_t.NaN;
  16. private const real_t Deg2RadConst = (real_t) 0.0174532925199432957692369077M; // 0.0174532924f and 0.0174532925199433
  17. private const real_t Rad2DegConst = (real_t) 57.295779513082320876798154814M; // 57.29578f and 57.2957795130823
  18. public static real_t Abs(real_t s)
  19. {
  20. return Math.Abs(s);
  21. }
  22. public static int Abs(int s)
  23. {
  24. return Math.Abs(s);
  25. }
  26. public static real_t Acos(real_t s)
  27. {
  28. return (real_t)Math.Acos(s);
  29. }
  30. public static real_t Asin(real_t s)
  31. {
  32. return (real_t)Math.Asin(s);
  33. }
  34. public static real_t Atan(real_t s)
  35. {
  36. return (real_t)Math.Atan(s);
  37. }
  38. public static real_t Atan2(real_t x, real_t y)
  39. {
  40. return (real_t)Math.Atan2(x, y);
  41. }
  42. public static Vector2 Cartesian2Polar(real_t x, real_t y)
  43. {
  44. return new Vector2(Sqrt(x * x + y * y), Atan2(y, x));
  45. }
  46. public static real_t Ceil(real_t s)
  47. {
  48. return (real_t)Math.Ceiling(s);
  49. }
  50. public static int Clamp(int value, int min, int max)
  51. {
  52. return value < min ? min : value > max ? max : value;
  53. }
  54. public static real_t Clamp(real_t value, real_t min, real_t max)
  55. {
  56. return value < min ? min : value > max ? max : value;
  57. }
  58. public static real_t Cos(real_t s)
  59. {
  60. return (real_t)Math.Cos(s);
  61. }
  62. public static real_t Cosh(real_t s)
  63. {
  64. return (real_t)Math.Cosh(s);
  65. }
  66. public static int Decimals(real_t step)
  67. {
  68. return Decimals((decimal)step);
  69. }
  70. public static int Decimals(decimal step)
  71. {
  72. return BitConverter.GetBytes(decimal.GetBits(step)[3])[2];
  73. }
  74. public static real_t Deg2Rad(real_t deg)
  75. {
  76. return deg * Deg2RadConst;
  77. }
  78. public static real_t Ease(real_t s, real_t curve)
  79. {
  80. if (s < 0f)
  81. {
  82. s = 0f;
  83. }
  84. else if (s > 1.0f)
  85. {
  86. s = 1.0f;
  87. }
  88. if (curve > 0f)
  89. {
  90. if (curve < 1.0f)
  91. {
  92. return 1.0f - Pow(1.0f - s, 1.0f / curve);
  93. }
  94. return Pow(s, curve);
  95. }
  96. if (curve < 0f)
  97. {
  98. if (s < 0.5f)
  99. {
  100. return Pow(s * 2.0f, -curve) * 0.5f;
  101. }
  102. return (1.0f - Pow(1.0f - (s - 0.5f) * 2.0f, -curve)) * 0.5f + 0.5f;
  103. }
  104. return 0f;
  105. }
  106. public static real_t Exp(real_t s)
  107. {
  108. return (real_t)Math.Exp(s);
  109. }
  110. public static real_t Floor(real_t s)
  111. {
  112. return (real_t)Math.Floor(s);
  113. }
  114. public static real_t InverseLerp(real_t from, real_t to, real_t weight)
  115. {
  116. return (weight - from) / (to - from);
  117. }
  118. public static bool IsInf(real_t s)
  119. {
  120. return real_t.IsInfinity(s);
  121. }
  122. public static bool IsNaN(real_t s)
  123. {
  124. return real_t.IsNaN(s);
  125. }
  126. public static real_t Lerp(real_t from, real_t to, real_t weight)
  127. {
  128. return from + (to - from) * weight;
  129. }
  130. public static real_t Log(real_t s)
  131. {
  132. return (real_t)Math.Log(s);
  133. }
  134. public static int Max(int a, int b)
  135. {
  136. return a > b ? a : b;
  137. }
  138. public static real_t Max(real_t a, real_t b)
  139. {
  140. return a > b ? a : b;
  141. }
  142. public static int Min(int a, int b)
  143. {
  144. return a < b ? a : b;
  145. }
  146. public static real_t Min(real_t a, real_t b)
  147. {
  148. return a < b ? a : b;
  149. }
  150. public static int NearestPo2(int value)
  151. {
  152. value--;
  153. value |= value >> 1;
  154. value |= value >> 2;
  155. value |= value >> 4;
  156. value |= value >> 8;
  157. value |= value >> 16;
  158. value++;
  159. return value;
  160. }
  161. public static Vector2 Polar2Cartesian(real_t r, real_t th)
  162. {
  163. return new Vector2(r * Cos(th), r * Sin(th));
  164. }
  165. /// <summary>
  166. /// Performs a canonical Modulus operation, where the output is on the range [0, b).
  167. /// </summary>
  168. public static real_t PosMod(real_t a, real_t b)
  169. {
  170. real_t c = a % b;
  171. if ((c < 0 && b > 0) || (c > 0 && b < 0))
  172. {
  173. c += b;
  174. }
  175. return c;
  176. }
  177. /// <summary>
  178. /// Performs a canonical Modulus operation, where the output is on the range [0, b).
  179. /// </summary>
  180. public static int PosMod(int a, int b)
  181. {
  182. int c = a % b;
  183. if ((c < 0 && b > 0) || (c > 0 && b < 0))
  184. {
  185. c += b;
  186. }
  187. return c;
  188. }
  189. public static real_t Pow(real_t x, real_t y)
  190. {
  191. return (real_t)Math.Pow(x, y);
  192. }
  193. public static real_t Rad2Deg(real_t rad)
  194. {
  195. return rad * Rad2DegConst;
  196. }
  197. public static real_t Round(real_t s)
  198. {
  199. return (real_t)Math.Round(s);
  200. }
  201. public static int Sign(int s)
  202. {
  203. return s < 0 ? -1 : 1;
  204. }
  205. public static real_t Sign(real_t s)
  206. {
  207. return s < 0f ? -1f : 1f;
  208. }
  209. public static real_t Sin(real_t s)
  210. {
  211. return (real_t)Math.Sin(s);
  212. }
  213. public static real_t Sinh(real_t s)
  214. {
  215. return (real_t)Math.Sinh(s);
  216. }
  217. public static real_t Sqrt(real_t s)
  218. {
  219. return (real_t)Math.Sqrt(s);
  220. }
  221. public static real_t Stepify(real_t s, real_t step)
  222. {
  223. if (step != 0f)
  224. {
  225. s = Floor(s / step + 0.5f) * step;
  226. }
  227. return s;
  228. }
  229. public static real_t Tan(real_t s)
  230. {
  231. return (real_t)Math.Tan(s);
  232. }
  233. public static real_t Tanh(real_t s)
  234. {
  235. return (real_t)Math.Tanh(s);
  236. }
  237. public static int Wrap(int value, int min, int max)
  238. {
  239. int rng = max - min;
  240. return min + ((value - min) % rng + rng) % rng;
  241. }
  242. public static real_t Wrap(real_t value, real_t min, real_t max)
  243. {
  244. real_t rng = max - min;
  245. return min + ((value - min) % rng + rng) % rng;
  246. }
  247. }
  248. }