2017-10-02 23:24:00 +02:00
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace Godot
|
|
|
|
{
|
|
|
|
public static class Mathf
|
|
|
|
{
|
|
|
|
public const float PI = 3.14159274f;
|
|
|
|
public const float Epsilon = 1e-06f;
|
|
|
|
|
|
|
|
private const float Deg2RadConst = 0.0174532924f;
|
|
|
|
private const float Rad2DegConst = 57.29578f;
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Abs(float s)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return Math.Abs(s);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Acos(float s)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (float)Math.Acos(s);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Asin(float s)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (float)Math.Asin(s);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Atan(float s)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (float)Math.Atan(s);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Atan2(float x, float y)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (float)Math.Atan2(x, y);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static Vector2 Cartesian2Polar(float x, float y)
|
2017-10-13 01:15:45 +02:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return new Vector2(Sqrt(x * x + y * y), Atan2(y, x));
|
2017-10-13 01:15:45 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Ceil(float s)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (float)Math.Ceiling(s);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Clamp(float val, float min, float max)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
if (val < min)
|
|
|
|
{
|
|
|
|
return min;
|
|
|
|
}
|
|
|
|
else if (val > max)
|
|
|
|
{
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Cos(float s)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (float)Math.Cos(s);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Cosh(float s)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (float)Math.Cosh(s);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static int Decimals(float step)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return Decimals(step);
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static int Decimals(decimal step)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return BitConverter.GetBytes(decimal.GetBits(step)[3])[2];
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Deg2Rad(float deg)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return deg * Deg2RadConst;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Ease(float s, float curve)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
if (s < 0f)
|
|
|
|
{
|
|
|
|
s = 0f;
|
|
|
|
}
|
|
|
|
else if (s > 1.0f)
|
|
|
|
{
|
|
|
|
s = 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (curve > 0f)
|
|
|
|
{
|
|
|
|
if (curve < 1.0f)
|
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return 1.0f - Pow(1.0f - s, 1.0f / curve);
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
return Pow(s, curve);
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
else if (curve < 0f)
|
|
|
|
{
|
|
|
|
if (s < 0.5f)
|
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return Pow(s * 2.0f, -curve) * 0.5f;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
return (1.0f - Pow(1.0f - (s - 0.5f) * 2.0f, -curve)) * 0.5f + 0.5f;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0f;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Exp(float s)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (float)Math.Exp(s);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Floor(float s)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (float)Math.Floor(s);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Fposmod(float x, float y)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
if (x >= 0f)
|
|
|
|
{
|
|
|
|
return x % y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return y - (-x % y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Lerp(float from, float to, float weight)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return from + (to - from) * Clamp(weight, 0f, 1f);
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Log(float s)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (float)Math.Log(s);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static int Max(int a, int b)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (a > b) ? a : b;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Max(float a, float b)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (a > b) ? a : b;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static int Min(int a, int b)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (a < b) ? a : b;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Min(float a, float b)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (a < b) ? a : b;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static int NearestPo2(int val)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
val--;
|
|
|
|
val |= val >> 1;
|
|
|
|
val |= val >> 2;
|
|
|
|
val |= val >> 4;
|
|
|
|
val |= val >> 8;
|
|
|
|
val |= val >> 16;
|
|
|
|
val++;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static Vector2 Polar2Cartesian(float r, float th)
|
2017-10-13 01:15:45 +02:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return new Vector2(r * Cos(th), r * Sin(th));
|
2017-10-13 01:15:45 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Pow(float x, float y)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (float)Math.Pow(x, y);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Rad2Deg(float rad)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return rad * Rad2DegConst;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Round(float s)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (float)Math.Round(s);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Sign(float s)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (s < 0f) ? -1f : 1f;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Sin(float s)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (float)Math.Sin(s);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Sinh(float s)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (float)Math.Sinh(s);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Sqrt(float s)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (float)Math.Sqrt(s);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Stepify(float s, float step)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
if (step != 0f)
|
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
s = Floor(s / step + 0.5f) * step;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Tan(float s)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (float)Math.Tan(s);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public static float Tanh(float s)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (float)Math.Tanh(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|