Merge pull request #18038 from Chaosus/mathf

[Mono] Improvements for Mathf
This commit is contained in:
Ignacio Etcheverry 2018-04-13 22:22:28 +02:00 committed by GitHub
commit e3590a2522
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 91 additions and 33 deletions

View file

@ -206,13 +206,13 @@ namespace Godot
} }
else else
{ {
euler.x = Mathf.PI * 0.5f; euler.x = Mathf.Pi * 0.5f;
euler.y = -Mathf.Atan2(-m[0, 1], m[0, 0]); euler.y = -Mathf.Atan2(-m[0, 1], m[0, 0]);
} }
} }
else else
{ {
euler.x = -Mathf.PI * 0.5f; euler.x = -Mathf.Pi * 0.5f;
euler.y = -Mathf.Atan2(-m[0, 1], m[0, 0]); euler.y = -Mathf.Atan2(-m[0, 1], m[0, 0]);
} }

View file

@ -8,16 +8,14 @@ using real_t = System.Single;
namespace Godot namespace Godot
{ {
public static class Mathf public static partial class Mathf
{ {
// Define constants with Decimal precision and cast down to double or float. // 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
#if REAL_T_IS_DOUBLE public const real_t Tau = (real_t) 6.2831853071795864769252867666M; // 6.2831855f and 6.28318530717959
public const real_t Epsilon = 1e-14; // Epsilon size should depend on the precision used. public const real_t Pi = (real_t) 3.1415926535897932384626433833M; // 3.1415927f and 3.14159265358979
#else public const real_t Inf = real_t.PositiveInfinity;
public const real_t Epsilon = 1e-06f; public const real_t NaN = real_t.NaN;
#endif
private const real_t Deg2RadConst = (real_t) 0.0174532925199432957692369077M; // 0.0174532924f and 0.0174532925199433 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 private const real_t Rad2DegConst = (real_t) 57.295779513082320876798154814M; // 57.29578f and 57.2957795130823
@ -27,6 +25,11 @@ namespace Godot
return Math.Abs(s); return Math.Abs(s);
} }
public static int Abs(int s)
{
return Math.Abs(s);
}
public static real_t Acos(real_t s) public static real_t Acos(real_t s)
{ {
return (real_t)Math.Acos(s); return (real_t)Math.Acos(s);
@ -57,18 +60,14 @@ namespace Godot
return (real_t)Math.Ceiling(s); 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 value < min ? min : value > max ? max : value;
{ }
return min;
}
else if (val > max)
{
return max;
}
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) public static real_t Cos(real_t s)
@ -151,6 +150,21 @@ namespace Godot
} }
} }
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) public static real_t Lerp(real_t from, real_t to, real_t weight)
{ {
return from + (to - from) * Clamp(weight, 0f, 1f); return from + (to - from) * Clamp(weight, 0f, 1f);
@ -181,16 +195,16 @@ namespace Godot
return (a < b) ? a : b; return (a < b) ? a : b;
} }
public static int NearestPo2(int val) public static int NearestPo2(int value)
{ {
val--; value--;
val |= val >> 1; value |= value >> 1;
val |= val >> 2; value |= value >> 2;
val |= val >> 4; value |= value >> 4;
val |= val >> 8; value |= value >> 8;
val |= val >> 16; value |= value >> 16;
val++; value++;
return val; return value;
} }
public static Vector2 Polar2Cartesian(real_t r, real_t th) public static Vector2 Polar2Cartesian(real_t r, real_t th)
@ -218,6 +232,11 @@ namespace Godot
return (int)Math.Round(s); return (int)Math.Round(s);
} }
public static int Sign(int s)
{
return (s < 0) ? -1 : 1;
}
public static real_t Sign(real_t s) public static real_t Sign(real_t s)
{ {
return (s < 0f) ? -1f : 1f; return (s < 0f) ? -1f : 1f;
@ -258,16 +277,16 @@ namespace Godot
return (real_t)Math.Tanh(s); return (real_t)Math.Tanh(s);
} }
public static int Wrap(int val, int min, int max) public static int Wrap(int value, int min, int max)
{ {
int rng = max - min; int rng = max - min;
return min + ((((val - min) % rng) + rng) % rng); return min + ((((value - min) % rng) + rng) % rng);
} }
public static real_t Wrap(real_t val, real_t min, real_t max) public static real_t Wrap(real_t value, real_t min, real_t max)
{ {
real_t rng = max - min; real_t rng = max - min;
return min + (val - min) - (rng * Floor((val - min) / rng)); return min + ((((value - min) % rng) + rng) % rng);
} }
} }
} }

View file

@ -0,0 +1,39 @@
using System;
#if REAL_T_IS_DOUBLE
using real_t = System.Double;
#else
using real_t = System.Single;
#endif
namespace Godot
{
public static partial class Mathf
{
// Define constants with Decimal precision and cast down to double or float.
public const real_t E = (real_t) 2.7182818284590452353602874714M; // 2.7182817f and 2.718281828459045
public const real_t Sqrt2 = (real_t) 1.4142135623730950488016887242M; // 1.4142136f and 1.414213562373095
#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 static int CeilToInt(real_t s)
{
return (int)Math.Ceiling(s);
}
public static int FloorToInt(real_t s)
{
return (int)Math.Floor(s);
}
public static int RoundToInt(real_t s)
{
return (int)Math.Round(s);
}
}
}