summaryrefslogtreecommitdiff
path: root/modules/mono/glue/cs_files/Mathf.cs
diff options
context:
space:
mode:
Diffstat (limited to 'modules/mono/glue/cs_files/Mathf.cs')
-rw-r--r--modules/mono/glue/cs_files/Mathf.cs104
1 files changed, 64 insertions, 40 deletions
diff --git a/modules/mono/glue/cs_files/Mathf.cs b/modules/mono/glue/cs_files/Mathf.cs
index 8b9c264d0d..0d20a12563 100644
--- a/modules/mono/glue/cs_files/Mathf.cs
+++ b/modules/mono/glue/cs_files/Mathf.cs
@@ -1,5 +1,4 @@
using System;
-
#if REAL_T_IS_DOUBLE
using real_t = System.Double;
#else
@@ -8,16 +7,14 @@ using real_t = System.Single;
namespace Godot
{
- public static class Mathf
+ public static partial class Mathf
{
- // Define constants with Decimal precision and cast down to double or float.
- public const real_t PI = (real_t) 3.1415926535897932384626433833M; // 3.1415927f and 3.14159265358979
+ // Define constants with Decimal precision and cast down to double or float.
- #if REAL_T_IS_DOUBLE
- public const real_t Epsilon = 1e-14; // Epsilon size should depend on the precision used.
- #else
- public const real_t Epsilon = 1e-06f;
- #endif
+ public const real_t Tau = (real_t) 6.2831853071795864769252867666M; // 6.2831855f and 6.28318530717959
+ public const real_t Pi = (real_t) 3.1415926535897932384626433833M; // 3.1415927f and 3.14159265358979
+ public const real_t Inf = real_t.PositiveInfinity;
+ public const real_t NaN = real_t.NaN;
private const real_t Deg2RadConst = (real_t) 0.0174532925199432957692369077M; // 0.0174532924f and 0.0174532925199433
private const real_t Rad2DegConst = (real_t) 57.295779513082320876798154814M; // 57.29578f and 57.2957795130823
@@ -27,6 +24,11 @@ namespace Godot
return Math.Abs(s);
}
+ public static int Abs(int s)
+ {
+ return Math.Abs(s);
+ }
+
public static real_t Acos(real_t s)
{
return (real_t)Math.Acos(s);
@@ -57,18 +59,14 @@ namespace Godot
return (real_t)Math.Ceiling(s);
}
- public static real_t Clamp(real_t val, real_t min, real_t max)
+ public static int Clamp(int value, int min, int max)
{
- if (val < min)
- {
- return min;
- }
- else if (val > max)
- {
- return max;
- }
+ return value < min ? min : value > max ? max : value;
+ }
- return val;
+ public static real_t Clamp(real_t value, real_t min, real_t max)
+ {
+ return value < min ? min : value > max ? max : value;
}
public static real_t Cos(real_t s)
@@ -116,7 +114,8 @@ namespace Godot
return Pow(s, curve);
}
- else if (curve < 0f)
+
+ if (curve < 0f)
{
if (s < 0.5f)
{
@@ -145,10 +144,23 @@ namespace Godot
{
return x % y;
}
- else
- {
- return y - (-x % y);
- }
+
+ return y - -x % y;
+ }
+
+ public static real_t InverseLerp(real_t from, real_t to, real_t weight)
+ {
+ return (Clamp(weight, 0f, 1f) - from) / (to - from);
+ }
+
+ public static bool IsInf(real_t s)
+ {
+ return real_t.IsInfinity(s);
+ }
+
+ public static bool IsNaN(real_t s)
+ {
+ return real_t.IsNaN(s);
}
public static real_t Lerp(real_t from, real_t to, real_t weight)
@@ -163,34 +175,34 @@ namespace Godot
public static int Max(int a, int b)
{
- return (a > b) ? a : b;
+ return a > b ? a : b;
}
public static real_t Max(real_t a, real_t b)
{
- return (a > b) ? a : b;
+ return a > b ? a : b;
}
public static int Min(int a, int b)
{
- return (a < b) ? a : b;
+ return a < b ? a : b;
}
public static real_t Min(real_t a, real_t b)
{
- return (a < b) ? a : b;
+ return a < b ? a : b;
}
- public static int NearestPo2(int val)
+ public static int NearestPo2(int value)
{
- val--;
- val |= val >> 1;
- val |= val >> 2;
- val |= val >> 4;
- val |= val >> 8;
- val |= val >> 16;
- val++;
- return val;
+ value--;
+ value |= value >> 1;
+ value |= value >> 2;
+ value |= value >> 4;
+ value |= value >> 8;
+ value |= value >> 16;
+ value++;
+ return value;
}
public static Vector2 Polar2Cartesian(real_t r, real_t th)
@@ -213,14 +225,14 @@ namespace Godot
return (real_t)Math.Round(s);
}
- public static int RoundToInt(real_t s)
+ public static int Sign(int s)
{
- return (int)Math.Round(s);
+ return s < 0 ? -1 : 1;
}
public static real_t Sign(real_t s)
{
- return (s < 0f) ? -1f : 1f;
+ return s < 0f ? -1f : 1f;
}
public static real_t Sin(real_t s)
@@ -257,5 +269,17 @@ namespace Godot
{
return (real_t)Math.Tanh(s);
}
+
+ public static int Wrap(int value, int min, int max)
+ {
+ int rng = max - min;
+ return min + ((value - min) % rng + rng) % rng;
+ }
+
+ public static real_t Wrap(real_t value, real_t min, real_t max)
+ {
+ real_t rng = max - min;
+ return min + ((value - min) % rng + rng) % rng;
+ }
}
}