Replace float with real_t, default Vectors, other misc C# improvements

Replace float with real_t in most files, defined at the top of each file via using. Objects such as Vector3 now accept doubles as inputs, and convert to real_t internally. I've added default Vectors such as Vector3.Zero. Other misc C# improvements such as Mathf.RoundToInt(). Color continues to use float only because high precision is not needed for 8-bit color math and to keep things simple. Everything seems to compile and work fine, but testing is requested, as this is the first time I've ever contributed to Godot.

(cherry picked from commit ff97c97c93)
This commit is contained in:
Aaron Franke 2018-03-01 01:51:35 -06:00 committed by Hein-Pieter van Braam
parent bffd4f0548
commit 577f3ccaf9
13 changed files with 473 additions and 291 deletions

View File

@ -7,6 +7,12 @@ using System;
// file: core/variant_call.cpp
// commit: 5ad9be4c24e9d7dc5672fdc42cea896622fe5685
#if REAL_T_IS_DOUBLE
using real_t = System.Double;
#else
using real_t = System.Single;
#endif
namespace Godot
{
public struct AABB : IEquatable<AABB>
@ -75,7 +81,7 @@ namespace Godot
return new AABB(begin, end - begin);
}
public float GetArea()
public real_t GetArea()
{
return size.x * size.y * size.z;
}
@ -108,7 +114,7 @@ namespace Godot
public Vector3 GetLongestAxis()
{
Vector3 axis = new Vector3(1f, 0f, 0f);
float max_size = size.x;
real_t max_size = size.x;
if (size.y > max_size)
{
@ -128,7 +134,7 @@ namespace Godot
public Vector3.Axis GetLongestAxisIndex()
{
Vector3.Axis axis = Vector3.Axis.X;
float max_size = size.x;
real_t max_size = size.x;
if (size.y > max_size)
{
@ -145,9 +151,9 @@ namespace Godot
return axis;
}
public float GetLongestAxisSize()
public real_t GetLongestAxisSize()
{
float max_size = size.x;
real_t max_size = size.x;
if (size.y > max_size)
max_size = size.y;
@ -161,7 +167,7 @@ namespace Godot
public Vector3 GetShortestAxis()
{
Vector3 axis = new Vector3(1f, 0f, 0f);
float max_size = size.x;
real_t max_size = size.x;
if (size.y < max_size)
{
@ -181,7 +187,7 @@ namespace Godot
public Vector3.Axis GetShortestAxisIndex()
{
Vector3.Axis axis = Vector3.Axis.X;
float max_size = size.x;
real_t max_size = size.x;
if (size.y < max_size)
{
@ -198,9 +204,9 @@ namespace Godot
return axis;
}
public float GetShortestAxisSize()
public real_t GetShortestAxisSize()
{
float max_size = size.x;
real_t max_size = size.x;
if (size.y < max_size)
max_size = size.y;
@ -222,7 +228,7 @@ namespace Godot
(dir.z > 0f) ? -half_extents.z : half_extents.z);
}
public AABB Grow(float by)
public AABB Grow(real_t by)
{
AABB res = this;
@ -354,23 +360,23 @@ namespace Godot
public bool IntersectsSegment(Vector3 from, Vector3 to)
{
float min = 0f;
float max = 1f;
real_t min = 0f;
real_t max = 1f;
for (int i = 0; i < 3; i++)
{
float seg_from = from[i];
float seg_to = to[i];
float box_begin = position[i];
float box_end = box_begin + size[i];
float cmin, cmax;
real_t seg_from = from[i];
real_t seg_to = to[i];
real_t box_begin = position[i];
real_t box_end = box_begin + size[i];
real_t cmin, cmax;
if (seg_from < seg_to)
{
if (seg_from > box_end || seg_to < box_begin)
return false;
float length = seg_to - seg_from;
real_t length = seg_to - seg_from;
cmin = seg_from < box_begin ? (box_begin - seg_from) / length : 0f;
cmax = seg_to > box_end ? (box_end - seg_from) / length : 1f;
}
@ -379,7 +385,7 @@ namespace Godot
if (seg_to > box_end || seg_from < box_begin)
return false;
float length = seg_to - seg_from;
real_t length = seg_to - seg_from;
cmin = seg_from > box_end ? (box_end - seg_from) / length : 0f;
cmax = seg_to < box_begin ? (box_begin - seg_from) / length : 1f;
}
@ -420,6 +426,7 @@ namespace Godot
return new AABB(min, max - min);
}
// Constructors
public AABB(Vector3 position, Vector3 size)
{
this.position = position;

View File

@ -1,6 +1,12 @@
using System;
using System.Runtime.InteropServices;
#if REAL_T_IS_DOUBLE
using real_t = System.Double;
#else
using real_t = System.Single;
#endif
namespace Godot
{
[StructLayout(LayoutKind.Sequential)]
@ -98,7 +104,7 @@ namespace Godot
}
}
public float this[int index, int axis]
public real_t this[int index, int axis]
{
get
{
@ -143,7 +149,7 @@ namespace Godot
);
}
public float Determinant()
public real_t Determinant()
{
return this[0, 0] * (this[1, 1] * this[2, 2] - this[2, 1] * this[1, 2]) -
this[1, 0] * (this[0, 1] * this[2, 2] - this[2, 1] * this[0, 2]) +
@ -162,7 +168,7 @@ namespace Godot
Vector3 euler;
euler.z = 0.0f;
float mxy = m.y[2];
real_t mxy = m.y[2];
if (mxy < 1.0f)
@ -196,7 +202,7 @@ namespace Godot
{
for (int j = 0; j < 3; j++)
{
float v = orth[i, j];
real_t v = orth[i, j];
if (v > 0.5f)
v = 1.0f;
@ -222,26 +228,26 @@ namespace Godot
{
Basis inv = this;
float[] co = new float[3]
real_t[] co = new real_t[3]
{
inv[1, 1] * inv[2, 2] - inv[1, 2] * inv[2, 1],
inv[1, 2] * inv[2, 0] - inv[1, 0] * inv[2, 2],
inv[1, 0] * inv[2, 1] - inv[1, 1] * inv[2, 0]
};
float det = inv[0, 0] * co[0] + inv[0, 1] * co[1] + inv[0, 2] * co[2];
real_t det = inv[0, 0] * co[0] + inv[0, 1] * co[1] + inv[0, 2] * co[2];
if (det == 0)
{
return new Basis
(
float.NaN, float.NaN, float.NaN,
float.NaN, float.NaN, float.NaN,
float.NaN, float.NaN, float.NaN
real_t.NaN, real_t.NaN, real_t.NaN,
real_t.NaN, real_t.NaN, real_t.NaN,
real_t.NaN, real_t.NaN, real_t.NaN
);
}
float s = 1.0f / det;
real_t s = 1.0f / det;
inv = new Basis
(
@ -274,7 +280,7 @@ namespace Godot
return Basis.CreateFromAxes(xAxis, yAxis, zAxis);
}
public Basis Rotated(Vector3 axis, float phi)
public Basis Rotated(Vector3 axis, real_t phi)
{
return new Basis(axis, phi) * this;
}
@ -296,17 +302,17 @@ namespace Godot
return m;
}
public float Tdotx(Vector3 with)
public real_t Tdotx(Vector3 with)
{
return this[0, 0] * with[0] + this[1, 0] * with[1] + this[2, 0] * with[2];
}
public float Tdoty(Vector3 with)
public real_t Tdoty(Vector3 with)
{
return this[0, 1] * with[0] + this[1, 1] * with[1] + this[2, 1] * with[2];
}
public float Tdotz(Vector3 with)
public real_t Tdotz(Vector3 with)
{
return this[0, 2] * with[0] + this[1, 2] * with[1] + this[2, 2] * with[2];
}
@ -315,7 +321,7 @@ namespace Godot
{
Basis tr = this;
float temp = this[0, 1];
real_t temp = this[0, 1];
this[0, 1] = this[1, 0];
this[1, 0] = temp;
@ -350,76 +356,77 @@ namespace Godot
);
}
public Quat Quat() {
float trace = x[0] + y[1] + z[2];
public Quat Quat() {
real_t trace = x[0] + y[1] + z[2];
if (trace > 0.0f) {
float s = Mathf.Sqrt(trace + 1.0f) * 2f;
float inv_s = 1f / s;
return new Quat(
(z[1] - y[2]) * inv_s,
(x[2] - z[0]) * inv_s,
(y[0] - x[1]) * inv_s,
s * 0.25f
);
} else if (x[0] > y[1] && x[0] > z[2]) {
float s = Mathf.Sqrt(x[0] - y[1] - z[2] + 1.0f) * 2f;
float inv_s = 1f / s;
return new Quat(
s * 0.25f,
(x[1] + y[0]) * inv_s,
(x[2] + z[0]) * inv_s,
(z[1] - y[2]) * inv_s
);
} else if (y[1] > z[2]) {
float s = Mathf.Sqrt(-x[0] + y[1] - z[2] + 1.0f) * 2f;
float inv_s = 1f / s;
return new Quat(
(x[1] + y[0]) * inv_s,
s * 0.25f,
(y[2] + z[1]) * inv_s,
(x[2] - z[0]) * inv_s
);
} else {
float s = Mathf.Sqrt(-x[0] - y[1] + z[2] + 1.0f) * 2f;
float inv_s = 1f / s;
return new Quat(
(x[2] + z[0]) * inv_s,
(y[2] + z[1]) * inv_s,
s * 0.25f,
(y[0] - x[1]) * inv_s
);
}
}
if (trace > 0.0f) {
real_t s = Mathf.Sqrt(trace + 1.0f) * 2f;
real_t inv_s = 1f / s;
return new Quat(
(z[1] - y[2]) * inv_s,
(x[2] - z[0]) * inv_s,
(y[0] - x[1]) * inv_s,
s * 0.25f
);
} else if (x[0] > y[1] && x[0] > z[2]) {
real_t s = Mathf.Sqrt(x[0] - y[1] - z[2] + 1.0f) * 2f;
real_t inv_s = 1f / s;
return new Quat(
s * 0.25f,
(x[1] + y[0]) * inv_s,
(x[2] + z[0]) * inv_s,
(z[1] - y[2]) * inv_s
);
} else if (y[1] > z[2]) {
real_t s = Mathf.Sqrt(-x[0] + y[1] - z[2] + 1.0f) * 2f;
real_t inv_s = 1f / s;
return new Quat(
(x[1] + y[0]) * inv_s,
s * 0.25f,
(y[2] + z[1]) * inv_s,
(x[2] - z[0]) * inv_s
);
} else {
real_t s = Mathf.Sqrt(-x[0] - y[1] + z[2] + 1.0f) * 2f;
real_t inv_s = 1f / s;
return new Quat(
(x[2] + z[0]) * inv_s,
(y[2] + z[1]) * inv_s,
s * 0.25f,
(y[0] - x[1]) * inv_s
);
}
}
// Constructors
public Basis(Quat quat)
{
float s = 2.0f / quat.LengthSquared();
real_t s = 2.0f / quat.LengthSquared();
float xs = quat.x * s;
float ys = quat.y * s;
float zs = quat.z * s;
float wx = quat.w * xs;
float wy = quat.w * ys;
float wz = quat.w * zs;
float xx = quat.x * xs;
float xy = quat.x * ys;
float xz = quat.x * zs;
float yy = quat.y * ys;
float yz = quat.y * zs;
float zz = quat.z * zs;
real_t xs = quat.x * s;
real_t ys = quat.y * s;
real_t zs = quat.z * s;
real_t wx = quat.w * xs;
real_t wy = quat.w * ys;
real_t wz = quat.w * zs;
real_t xx = quat.x * xs;
real_t xy = quat.x * ys;
real_t xz = quat.x * zs;
real_t yy = quat.y * ys;
real_t yz = quat.y * zs;
real_t zz = quat.z * zs;
this.x = new Vector3(1.0f - (yy + zz), xy - wz, xz + wy);
this.y = new Vector3(xy + wz, 1.0f - (xx + zz), yz - wx);
this.z = new Vector3(xz - wy, yz + wx, 1.0f - (xx + yy));
}
public Basis(Vector3 axis, float phi)
public Basis(Vector3 axis, real_t phi)
{
Vector3 axis_sq = new Vector3(axis.x * axis.x, axis.y * axis.y, axis.z * axis.z);
float cosine = Mathf.Cos(phi);
float sine = Mathf.Sin(phi);
real_t cosine = Mathf.Cos( (real_t)phi);
real_t sine = Mathf.Sin( (real_t)phi);
this.x = new Vector3
(
@ -450,7 +457,7 @@ namespace Godot
this.z = zAxis;
}
public Basis(float xx, float xy, float xz, float yx, float yy, float yz, float zx, float zy, float zz)
public Basis(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz)
{
this.x = new Vector3(xx, xy, xz);
this.y = new Vector3(yx, yy, yz);

View File

@ -45,8 +45,8 @@ namespace Godot
{
get
{
float max = Mathf.Max(r, Mathf.Max(g, b));
float min = Mathf.Min(r, Mathf.Min(g, b));
float max = (float) Mathf.Max(r, (float) Mathf.Max(g, b));
float min = (float) Mathf.Min(r, (float) Mathf.Min(g, b));
float delta = max - min;
@ -79,8 +79,8 @@ namespace Godot
{
get
{
float max = Mathf.Max(r, Mathf.Max(g, b));
float min = Mathf.Min(r, Mathf.Min(g, b));
float max = (float) Mathf.Max(r, (float) Mathf.Max(g, b));
float min = (float) Mathf.Min(r, (float) Mathf.Min(g, b));
float delta = max - min;
@ -96,7 +96,7 @@ namespace Godot
{
get
{
return Mathf.Max(r, Mathf.Max(g, b));
return (float) Mathf.Max(r, (float) Mathf.Max(g, b));
}
set
{
@ -317,6 +317,7 @@ namespace Godot
return txt;
}
// Constructors
public Color(float r, float g, float b, float a = 1.0f)
{
this.r = r;
@ -375,7 +376,7 @@ namespace Godot
private String _to_hex(float val)
{
int v = (int)Mathf.Clamp(val * 255.0f, 0, 255);
int v = (int) Mathf.Clamp(val * 255.0f, 0, 255);
string ret = string.Empty;

View File

@ -1,5 +1,7 @@
using System;
// TODO: Add comments describing what this class does. It is not obvious.
namespace Godot
{
public static partial class GD

View File

@ -1,51 +1,63 @@
using System;
#if REAL_T_IS_DOUBLE
using real_t = System.Double;
#else
using real_t = System.Single;
#endif
namespace Godot
{
public static class Mathf
{
public const float PI = 3.14159274f;
public const float Epsilon = 1e-06f;
// 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
private const float Deg2RadConst = 0.0174532924f;
private const float Rad2DegConst = 57.29578f;
#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 float Abs(float s)
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
public static real_t Abs(real_t s)
{
return Math.Abs(s);
}
public static float Acos(float s)
public static real_t Acos(real_t s)
{
return (float)Math.Acos(s);
return (real_t)Math.Acos(s);
}
public static float Asin(float s)
public static real_t Asin(real_t s)
{
return (float)Math.Asin(s);
return (real_t)Math.Asin(s);
}
public static float Atan(float s)
public static real_t Atan(real_t s)
{
return (float)Math.Atan(s);
return (real_t)Math.Atan(s);
}
public static float Atan2(float x, float y)
public static real_t Atan2(real_t x, real_t y)
{
return (float)Math.Atan2(x, y);
return (real_t)Math.Atan2(x, y);
}
public static Vector2 Cartesian2Polar(float x, float y)
{
return new Vector2(Sqrt(x * x + y * y), Atan2(y, x));
}
public static float Ceil(float s)
public static Vector2 Cartesian2Polar(real_t x, real_t y)
{
return (float)Math.Ceiling(s);
return new Vector2(Sqrt(x * x + y * y), Atan2(y, x));
}
public static float Clamp(float val, float min, float max)
public static real_t Ceil(real_t s)
{
return (real_t)Math.Ceiling(s);
}
public static real_t Clamp(real_t val, real_t min, real_t max)
{
if (val < min)
{
@ -59,17 +71,17 @@ namespace Godot
return val;
}
public static float Cos(float s)
public static real_t Cos(real_t s)
{
return (float)Math.Cos(s);
return (real_t)Math.Cos(s);
}
public static float Cosh(float s)
public static real_t Cosh(real_t s)
{
return (float)Math.Cosh(s);
return (real_t)Math.Cosh(s);
}
public static int Decimals(float step)
public static int Decimals(real_t step)
{
return Decimals((decimal)step);
}
@ -79,12 +91,12 @@ namespace Godot
return BitConverter.GetBytes(decimal.GetBits(step)[3])[2];
}
public static float Deg2Rad(float deg)
public static real_t Deg2Rad(real_t deg)
{
return deg * Deg2RadConst;
}
public static float Ease(float s, float curve)
public static real_t Ease(real_t s, real_t curve)
{
if (s < 0f)
{
@ -117,17 +129,17 @@ namespace Godot
return 0f;
}
public static float Exp(float s)
public static real_t Exp(real_t s)
{
return (float)Math.Exp(s);
return (real_t)Math.Exp(s);
}
public static float Floor(float s)
public static real_t Floor(real_t s)
{
return (float)Math.Floor(s);
return (real_t)Math.Floor(s);
}
public static float Fposmod(float x, float y)
public static real_t Fposmod(real_t x, real_t y)
{
if (x >= 0f)
{
@ -139,14 +151,14 @@ namespace Godot
}
}
public static float Lerp(float from, float to, float weight)
public static real_t Lerp(real_t from, real_t to, real_t weight)
{
return from + (to - from) * Clamp(weight, 0f, 1f);
}
public static float Log(float s)
public static real_t Log(real_t s)
{
return (float)Math.Log(s);
return (real_t)Math.Log(s);
}
public static int Max(int a, int b)
@ -154,7 +166,7 @@ namespace Godot
return (a > b) ? a : b;
}
public static float Max(float a, float b)
public static real_t Max(real_t a, real_t b)
{
return (a > b) ? a : b;
}
@ -164,7 +176,7 @@ namespace Godot
return (a < b) ? a : b;
}
public static float Min(float a, float b)
public static real_t Min(real_t a, real_t b)
{
return (a < b) ? a : b;
}
@ -181,47 +193,52 @@ namespace Godot
return val;
}
public static Vector2 Polar2Cartesian(float r, float th)
{
return new Vector2(r * Cos(th), r * Sin(th));
}
public static float Pow(float x, float y)
public static Vector2 Polar2Cartesian(real_t r, real_t th)
{
return (float)Math.Pow(x, y);
return new Vector2(r * Cos(th), r * Sin(th));
}
public static float Rad2Deg(float rad)
public static real_t Pow(real_t x, real_t y)
{
return (real_t)Math.Pow(x, y);
}
public static real_t Rad2Deg(real_t rad)
{
return rad * Rad2DegConst;
}
public static float Round(float s)
public static real_t Round(real_t s)
{
return (float)Math.Round(s);
return (real_t)Math.Round(s);
}
public static float Sign(float s)
public static int RoundToInt(real_t s)
{
return (int)Math.Round(s);
}
public static real_t Sign(real_t s)
{
return (s < 0f) ? -1f : 1f;
}
public static float Sin(float s)
public static real_t Sin(real_t s)
{
return (float)Math.Sin(s);
return (real_t)Math.Sin(s);
}
public static float Sinh(float s)
public static real_t Sinh(real_t s)
{
return (float)Math.Sinh(s);
return (real_t)Math.Sinh(s);
}
public static float Sqrt(float s)
public static real_t Sqrt(real_t s)
{
return (float)Math.Sqrt(s);
return (real_t)Math.Sqrt(s);
}
public static float Stepify(float s, float step)
public static real_t Stepify(real_t s, real_t step)
{
if (step != 0f)
{
@ -231,14 +248,14 @@ namespace Godot
return s;
}
public static float Tan(float s)
public static real_t Tan(real_t s)
{
return (float)Math.Tan(s);
return (real_t)Math.Tan(s);
}
public static float Tanh(float s)
public static real_t Tanh(real_t s)
{
return (float)Math.Tanh(s);
return (real_t)Math.Tanh(s);
}
}
}

View File

@ -1,12 +1,18 @@
using System;
#if REAL_T_IS_DOUBLE
using real_t = System.Double;
#else
using real_t = System.Single;
#endif
namespace Godot
{
public struct Plane : IEquatable<Plane>
{
Vector3 normal;
public float x
public real_t x
{
get
{
@ -18,7 +24,7 @@ namespace Godot
}
}
public float y
public real_t y
{
get
{
@ -30,7 +36,7 @@ namespace Godot
}
}
public float z
public real_t z
{
get
{
@ -42,7 +48,7 @@ namespace Godot
}
}
float d;
real_t d;
public Vector3 Center
{
@ -52,7 +58,7 @@ namespace Godot
}
}
public float DistanceTo(Vector3 point)
public real_t DistanceTo(Vector3 point)
{
return normal.Dot(point) - d;
}
@ -62,15 +68,15 @@ namespace Godot
return normal * d;
}
public bool HasPoint(Vector3 point, float epsilon = Mathf.Epsilon)
public bool HasPoint(Vector3 point, real_t epsilon = Mathf.Epsilon)
{
float dist = normal.Dot(point) - d;
real_t dist = normal.Dot(point) - d;
return Mathf.Abs(dist) <= epsilon;
}
public Vector3 Intersect3(Plane b, Plane c)
{
float denom = normal.Cross(b.normal).Dot(c.normal);
real_t denom = normal.Cross(b.normal).Dot(c.normal);
if (Mathf.Abs(denom) <= Mathf.Epsilon)
return new Vector3();
@ -84,12 +90,12 @@ namespace Godot
public Vector3 IntersectRay(Vector3 from, Vector3 dir)
{
float den = normal.Dot(dir);
real_t den = normal.Dot(dir);
if (Mathf.Abs(den) <= Mathf.Epsilon)
return new Vector3();
float dist = (normal.Dot(from) - d) / den;
real_t dist = (normal.Dot(from) - d) / den;
// This is a ray, before the emitting pos (from) does not exist
if (dist > Mathf.Epsilon)
@ -101,12 +107,12 @@ namespace Godot
public Vector3 IntersectSegment(Vector3 begin, Vector3 end)
{
Vector3 segment = begin - end;
float den = normal.Dot(segment);
real_t den = normal.Dot(segment);
if (Mathf.Abs(den) <= Mathf.Epsilon)
return new Vector3();
float dist = (normal.Dot(begin) - d) / den;
real_t dist = (normal.Dot(begin) - d) / den;
if (dist < -Mathf.Epsilon || dist > (1.0f + Mathf.Epsilon))
return new Vector3();
@ -121,7 +127,7 @@ namespace Godot
public Plane Normalized()
{
float len = normal.Length();
real_t len = normal.Length();
if (len == 0)
return new Plane(0, 0, 0, 0);
@ -134,13 +140,13 @@ namespace Godot
return point - normal * DistanceTo(point);
}
public Plane(float a, float b, float c, float d)
// Constructors
public Plane(real_t a, real_t b, real_t c, real_t d)
{
normal = new Vector3(a, b, c);
this.d = d;
}
public Plane(Vector3 normal, float d)
public Plane(Vector3 normal, real_t d)
{
this.normal = normal;
this.d = d;

View File

@ -1,6 +1,12 @@
using System;
using System.Runtime.InteropServices;
#if REAL_T_IS_DOUBLE
using real_t = System.Double;
#else
using real_t = System.Single;
#endif
namespace Godot
{
[StructLayout(LayoutKind.Sequential)]
@ -8,17 +14,17 @@ namespace Godot
{
private static readonly Quat identity = new Quat(0f, 0f, 0f, 1f);
public float x;
public float y;
public float z;
public float w;
public real_t x;
public real_t y;
public real_t z;
public real_t w;
public static Quat Identity
{
get { return identity; }
}
public float this[int index]
public real_t this[int index]
{
get
{
@ -58,15 +64,15 @@ namespace Godot
}
}
public Quat CubicSlerp(Quat b, Quat preA, Quat postB, float t)
public Quat CubicSlerp(Quat b, Quat preA, Quat postB, real_t t)
{
float t2 = (1.0f - t) * t * 2f;
real_t t2 = (1.0f - t) * t * 2f;
Quat sp = Slerp(b, t);
Quat sq = preA.Slerpni(postB, t);
return sp.Slerpni(sq, t2);
}
public float Dot(Quat b)
public real_t Dot(Quat b)
{
return x * b.x + y * b.y + z * b.z + w * b.w;
}
@ -76,12 +82,12 @@ namespace Godot
return new Quat(-x, -y, -z, w);
}
public float Length()
public real_t Length()
{
return Mathf.Sqrt(LengthSquared());
}
public float LengthSquared()
public real_t LengthSquared()
{
return Dot(this);
}
@ -91,20 +97,27 @@ namespace Godot
return this / Length();
}
public void Set(float x, float y, float z, float w)
public void Set(real_t x, real_t y, real_t z, real_t w)
{
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public void Set(Quat q)
{
this.x = q.x;
this.y = q.y;
this.z = q.z;
this.w = q.w;
}
public Quat Slerp(Quat b, float t)
public Quat Slerp(Quat b, real_t t)
{
// Calculate cosine
float cosom = x * b.x + y * b.y + z * b.z + w * b.w;
real_t cosom = x * b.x + y * b.y + z * b.z + w * b.w;
float[] to1 = new float[4];
real_t[] to1 = new real_t[4];
// Adjust signs if necessary
if (cosom < 0.0)
@ -122,13 +135,13 @@ namespace Godot
to1[3] = b.w;
}
float sinom, scale0, scale1;
real_t sinom, scale0, scale1;
// Calculate coefficients
if ((1.0 - cosom) > Mathf.Epsilon)
{
// Standard case (Slerp)
float omega = Mathf.Acos(cosom);
real_t omega = Mathf.Acos(cosom);
sinom = Mathf.Sin(omega);
scale0 = Mathf.Sin((1.0f - t) * omega) / sinom;
scale1 = Mathf.Sin(t * omega) / sinom;
@ -150,19 +163,19 @@ namespace Godot
);
}
public Quat Slerpni(Quat b, float t)
public Quat Slerpni(Quat b, real_t t)
{
float dot = this.Dot(b);
real_t dot = this.Dot(b);
if (Mathf.Abs(dot) > 0.9999f)
{
return this;
}
float theta = Mathf.Acos(dot);
float sinT = 1.0f / Mathf.Sin(theta);
float newFactor = Mathf.Sin(t * theta) * sinT;
float invFactor = Mathf.Sin((1.0f - t) * theta) * sinT;
real_t theta = Mathf.Acos(dot);
real_t sinT = 1.0f / Mathf.Sin(theta);
real_t newFactor = Mathf.Sin(t * theta) * sinT;
real_t invFactor = Mathf.Sin((1.0f - t) * theta) * sinT;
return new Quat
(
@ -180,17 +193,26 @@ namespace Godot
return new Vector3(q.x, q.y, q.z);
}
public Quat(float x, float y, float z, float w)
// Constructors
public Quat(real_t x, real_t y, real_t z, real_t w)
{
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public Quat(Vector3 axis, float angle)
public Quat(Quat q)
{
float d = axis.Length();
this.x = q.x;
this.y = q.y;
this.z = q.z;
this.w = q.w;
}
public Quat(Vector3 axis, real_t angle)
{
real_t d = axis.Length();
real_t angle_t = angle;
if (d == 0f)
{
@ -201,12 +223,12 @@ namespace Godot
}
else
{
float s = Mathf.Sin(angle * 0.5f) / d;
real_t s = Mathf.Sin(angle_t * 0.5f) / d;
x = axis.x * s;
y = axis.y * s;
z = axis.z * s;
w = Mathf.Cos(angle * 0.5f);
w = Mathf.Cos(angle_t * 0.5f);
}
}
@ -258,17 +280,17 @@ namespace Godot
);
}
public static Quat operator *(Quat left, float right)
public static Quat operator *(Quat left, real_t right)
{
return new Quat(left.x * right, left.y * right, left.z * right, left.w * right);
}
public static Quat operator *(float left, Quat right)
public static Quat operator *(real_t left, Quat right)
{
return new Quat(right.x * left, right.y * left, right.z * left, right.w * left);
}
public static Quat operator /(Quat left, float right)
public static Quat operator /(Quat left, real_t right)
{
return left * (1.0f / right);
}

View File

@ -1,6 +1,12 @@
using System;
using System.Runtime.InteropServices;
#if REAL_T_IS_DOUBLE
using real_t = System.Double;
#else
using real_t = System.Single;
#endif
namespace Godot
{
[StructLayout(LayoutKind.Sequential)]
@ -26,7 +32,7 @@ namespace Godot
get { return position + size; }
}
public float Area
public real_t Area
{
get { return GetArea(); }
}
@ -80,12 +86,12 @@ namespace Godot
return expanded;
}
public float GetArea()
public real_t GetArea()
{
return size.x * size.y;
}
public Rect2 Grow(float by)
public Rect2 Grow(real_t by)
{
Rect2 g = this;
@ -97,7 +103,7 @@ namespace Godot
return g;
}
public Rect2 GrowIndividual(float left, float top, float right, float bottom)
public Rect2 GrowIndividual(real_t left, real_t top, real_t right, real_t bottom)
{
Rect2 g = this;
@ -109,7 +115,7 @@ namespace Godot
return g;
}
public Rect2 GrowMargin(Margin margin, float by)
public Rect2 GrowMargin(Margin margin, real_t by)
{
Rect2 g = this;
@ -170,13 +176,23 @@ namespace Godot
return newRect;
}
// Constructors
public Rect2(Vector2 position, Vector2 size)
{
this.position = position;
this.size = size;
}
public Rect2(float x, float y, float width, float height)
public Rect2(Vector2 position, real_t width, real_t height)
{
this.position = position;
this.size = new Vector2(width, height);
}
public Rect2(real_t x, real_t y, Vector2 size)
{
this.position = new Vector2(x, y);
this.size = size;
}
public Rect2(real_t x, real_t y, real_t width, real_t height)
{
this.position = new Vector2(x, y);
this.size = new Vector2(width, height);

View File

@ -1,6 +1,12 @@
using System;
using System.Runtime.InteropServices;
#if REAL_T_IS_DOUBLE
using real_t = System.Double;
#else
using real_t = System.Single;
#endif
namespace Godot
{
[StructLayout(LayoutKind.Sequential)]
@ -33,7 +39,7 @@ namespace Godot
return new Transform(basis.Orthonormalized(), origin);
}
public Transform Rotated(Vector3 axis, float phi)
public Transform Rotated(Vector3 axis, real_t phi)
{
return new Transform(new Basis(axis, phi), new Vector3()) * this;
}
@ -98,6 +104,7 @@ namespace Godot
);
}
// Constructors
public Transform(Vector3 xAxis, Vector3 yAxis, Vector3 zAxis, Vector3 origin)
{
this.basis = Basis.CreateFromAxes(xAxis, yAxis, zAxis);

View File

@ -1,6 +1,12 @@
using System;
using System.Runtime.InteropServices;
#if REAL_T_IS_DOUBLE
using real_t = System.Double;
#else
using real_t = System.Single;
#endif
namespace Godot
{
[StructLayout(LayoutKind.Sequential)]
@ -27,7 +33,7 @@ namespace Godot
get { return o; }
}
public float Rotation
public real_t Rotation
{
get { return Mathf.Atan2(y.x, o.y); }
}
@ -73,7 +79,7 @@ namespace Godot
}
public float this[int index, int axis]
public real_t this[int index, int axis]
{
get
{
@ -107,7 +113,7 @@ namespace Godot
{
Transform2D inv = this;
float det = this[0, 0] * this[1, 1] - this[1, 0] * this[0, 1];
real_t det = this[0, 0] * this[1, 1] - this[1, 0] * this[0, 1];
if (det == 0)
{
@ -119,9 +125,9 @@ namespace Godot
);
}
float idet = 1.0f / det;
real_t idet = 1.0f / det;
float temp = this[0, 0];
real_t temp = this[0, 0];
this[0, 0] = this[1, 1];
this[1, 1] = temp;
@ -143,10 +149,10 @@ namespace Godot
return new Vector2(x.Dot(v), y.Dot(v));
}
public Transform2D InterpolateWith(Transform2D m, float c)
public Transform2D InterpolateWith(Transform2D m, real_t c)
{
float r1 = Rotation;
float r2 = m.Rotation;
real_t r1 = Rotation;
real_t r2 = m.Rotation;
Vector2 s1 = Scale;
Vector2 s2 = m.Scale;
@ -155,7 +161,7 @@ namespace Godot
Vector2 v1 = new Vector2(Mathf.Cos(r1), Mathf.Sin(r1));
Vector2 v2 = new Vector2(Mathf.Cos(r2), Mathf.Sin(r2));
float dot = v1.Dot(v2);
real_t dot = v1.Dot(v2);
// Clamp dot to [-1, 1]
dot = (dot < -1.0f) ? -1.0f : ((dot > 1.0f) ? 1.0f : dot);
@ -169,7 +175,7 @@ namespace Godot
}
else
{
float angle = c * Mathf.Acos(dot);
real_t angle = c * Mathf.Acos(dot);
Vector2 v3 = (v2 - v1 * dot).Normalized();
v = v1 * Mathf.Cos(angle) + v3 * Mathf.Sin(angle);
}
@ -192,7 +198,7 @@ namespace Godot
Transform2D inv = this;
// Swap
float temp = inv.x.y;
real_t temp = inv.x.y;
inv.x.y = inv.y.x;
inv.y.x = temp;
@ -218,7 +224,7 @@ namespace Godot
return on;
}
public Transform2D Rotated(float phi)
public Transform2D Rotated(real_t phi)
{
return this * new Transform2D(phi, new Vector2());
}
@ -232,12 +238,12 @@ namespace Godot
return copy;
}
private float Tdotx(Vector2 with)
private real_t Tdotx(Vector2 with)
{
return this[0, 0] * with[0] + this[1, 0] * with[1];
}
private float Tdoty(Vector2 with)
private real_t Tdoty(Vector2 with)
{
return this[0, 1] * with[0] + this[1, 1] * with[1];
}
@ -260,23 +266,25 @@ namespace Godot
return new Vector2(x.Dot(vInv), y.Dot(vInv));
}
// Constructors
public Transform2D(Vector2 xAxis, Vector2 yAxis, Vector2 origin)
{
this.x = xAxis;
this.y = yAxis;
this.o = origin;
}
public Transform2D(float xx, float xy, float yx, float yy, float ox, float oy)
public Transform2D(real_t xx, real_t xy, real_t yx, real_t yy, real_t ox, real_t oy)
{
this.x = new Vector2(xx, xy);
this.y = new Vector2(yx, yy);
this.o = new Vector2(ox, oy);
}
public Transform2D(float rot, Vector2 pos)
public Transform2D(real_t rot, Vector2 pos)
{
float cr = Mathf.Cos(rot);
float sr = Mathf.Sin(rot);
real_t cr = Mathf.Cos( (real_t)rot);
real_t sr = Mathf.Sin( (real_t)rot);
x.x = cr;
y.y = cr;
x.y = -sr;
@ -288,7 +296,7 @@ namespace Godot
{
left.o = left.Xform(right.o);
float x0, x1, y0, y1;
real_t x0, x1, y0, y1;
x0 = left.Tdotx(right.x);
x1 = left.Tdoty(right.x);

View File

@ -1 +1 @@
1
2

View File

@ -8,15 +8,21 @@ using System.Runtime.InteropServices;
// file: core/variant_call.cpp
// commit: 5ad9be4c24e9d7dc5672fdc42cea896622fe5685
#if REAL_T_IS_DOUBLE
using real_t = System.Double;
#else
using real_t = System.Single;
#endif
namespace Godot
{
[StructLayout(LayoutKind.Sequential)]
public struct Vector2 : IEquatable<Vector2>
{
public float x;
public float y;
public real_t x;
public real_t y;
public float this[int index]
public real_t this[int index]
{
get
{
@ -48,7 +54,7 @@ namespace Godot
internal void Normalize()
{
float length = x * x + y * y;
real_t length = x * x + y * y;
if (length != 0f)
{
@ -58,7 +64,7 @@ namespace Godot
}
}
private float Cross(Vector2 b)
private real_t Cross(Vector2 b)
{
return x * b.y - y * b.x;
}
@ -68,22 +74,22 @@ namespace Godot
return new Vector2(Mathf.Abs(x), Mathf.Abs(y));
}
public float Angle()
public real_t Angle()
{
return Mathf.Atan2(y, x);
}
public float AngleTo(Vector2 to)
public real_t AngleTo(Vector2 to)
{
return Mathf.Atan2(Cross(to), Dot(to));
}
public float AngleToPoint(Vector2 to)
public real_t AngleToPoint(Vector2 to)
{
return Mathf.Atan2(x - to.x, y - to.y);
}
public float Aspect()
public real_t Aspect()
{
return x / y;
}
@ -93,10 +99,10 @@ namespace Godot
return -Reflect(n);
}
public Vector2 Clamped(float length)
public Vector2 Clamped(real_t length)
{
Vector2 v = this;
float l = this.Length();
real_t l = this.Length();
if (l > 0 && length < l)
{
@ -107,15 +113,15 @@ namespace Godot
return v;
}
public Vector2 CubicInterpolate(Vector2 b, Vector2 preA, Vector2 postB, float t)
public Vector2 CubicInterpolate(Vector2 b, Vector2 preA, Vector2 postB, real_t t)
{
Vector2 p0 = preA;
Vector2 p1 = this;
Vector2 p2 = b;
Vector2 p3 = postB;
float t2 = t * t;
float t3 = t2 * t;
real_t t2 = t * t;
real_t t3 = t2 * t;
return 0.5f * ((p1 * 2.0f) +
(-p0 + p2) * t +
@ -123,17 +129,17 @@ namespace Godot
(-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3);
}
public float DistanceSquaredTo(Vector2 to)
public real_t DistanceSquaredTo(Vector2 to)
{
return (x - to.x) * (x - to.x) + (y - to.y) * (y - to.y);
}
public float DistanceTo(Vector2 to)
public real_t DistanceTo(Vector2 to)
{
return Mathf.Sqrt((x - to.x) * (x - to.x) + (y - to.y) * (y - to.y));
}
public float Dot(Vector2 with)
public real_t Dot(Vector2 with)
{
return x * with.x + y * with.y;
}
@ -148,17 +154,17 @@ namespace Godot
return Mathf.Abs(LengthSquared() - 1.0f) < Mathf.Epsilon;
}
public float Length()
public real_t Length()
{
return Mathf.Sqrt(x * x + y * y);
}
public float LengthSquared()
public real_t LengthSquared()
{
return x * x + y * y;
}
public Vector2 LinearInterpolate(Vector2 b, float t)
public Vector2 LinearInterpolate(Vector2 b, real_t t)
{
Vector2 res = this;
@ -180,12 +186,23 @@ namespace Godot
return 2.0f * n * Dot(n) - this;
}
public Vector2 Rotated(float phi)
public Vector2 Rotated(real_t phi)
{
float rads = Angle() + phi;
real_t rads = Angle() + phi;
return new Vector2(Mathf.Cos(rads), Mathf.Sin(rads)) * Length();
}
public void Set(real_t x, real_t y)
{
this.x = x;
this.y = y;
}
public void Set(Vector2 v)
{
this.x = v.x;
this.y = v.y;
}
public Vector2 Slide(Vector2 n)
{
return this - n * Dot(n);
@ -201,11 +218,35 @@ namespace Godot
return new Vector2(y, -x);
}
public Vector2(float x, float y)
private static readonly Vector2 zero = new Vector2 (0, 0);
private static readonly Vector2 one = new Vector2 (1, 1);
private static readonly Vector2 negOne = new Vector2 (-1, -1);
private static readonly Vector2 up = new Vector2 (0, 1);
private static readonly Vector2 down = new Vector2 (0, -1);
private static readonly Vector2 right = new Vector2 (1, 0);
private static readonly Vector2 left = new Vector2 (-1, 0);
public static Vector2 Zero { get { return zero; } }
public static Vector2 One { get { return one; } }
public static Vector2 NegOne { get { return negOne; } }
public static Vector2 Up { get { return up; } }
public static Vector2 Down { get { return down; } }
public static Vector2 Right { get { return right; } }
public static Vector2 Left { get { return left; } }
// Constructors
public Vector2(real_t x, real_t y)
{
this.x = x;
this.y = y;
}
public Vector2(Vector2 v)
{
this.x = v.x;
this.y = v.y;
}
public static Vector2 operator +(Vector2 left, Vector2 right)
{
@ -228,14 +269,14 @@ namespace Godot
return vec;
}
public static Vector2 operator *(Vector2 vec, float scale)
public static Vector2 operator *(Vector2 vec, real_t scale)
{
vec.x *= scale;
vec.y *= scale;
return vec;
}
public static Vector2 operator *(float scale, Vector2 vec)
public static Vector2 operator *(real_t scale, Vector2 vec)
{
vec.x *= scale;
vec.y *= scale;
@ -249,7 +290,7 @@ namespace Godot
return left;
}
public static Vector2 operator /(Vector2 vec, float scale)
public static Vector2 operator /(Vector2 vec, real_t scale)
{
vec.x /= scale;
vec.y /= scale;

View File

@ -8,6 +8,12 @@ using System.Runtime.InteropServices;
// file: core/variant_call.cpp
// commit: 5ad9be4c24e9d7dc5672fdc42cea896622fe5685
#if REAL_T_IS_DOUBLE
using real_t = System.Double;
#else
using real_t = System.Single;
#endif
namespace Godot
{
[StructLayout(LayoutKind.Sequential)]
@ -20,11 +26,11 @@ namespace Godot
Z
}
public float x;
public float y;
public float z;
public real_t x;
public real_t y;
public real_t z;
public float this[int index]
public real_t this[int index]
{
get
{
@ -61,7 +67,7 @@ namespace Godot
internal void Normalize()
{
float length = this.Length();
real_t length = this.Length();
if (length == 0f)
{
@ -80,7 +86,7 @@ namespace Godot
return new Vector3(Mathf.Abs(x), Mathf.Abs(y), Mathf.Abs(z));
}
public float AngleTo(Vector3 to)
public real_t AngleTo(Vector3 to)
{
return Mathf.Atan2(Cross(to).Length(), Dot(to));
}
@ -105,15 +111,15 @@ namespace Godot
);
}
public Vector3 CubicInterpolate(Vector3 b, Vector3 preA, Vector3 postB, float t)
public Vector3 CubicInterpolate(Vector3 b, Vector3 preA, Vector3 postB, real_t t)
{
Vector3 p0 = preA;
Vector3 p1 = this;
Vector3 p2 = b;
Vector3 p3 = postB;
float t2 = t * t;
float t3 = t2 * t;
real_t t2 = t * t;
real_t t3 = t2 * t;
return 0.5f * (
(p1 * 2.0f) + (-p0 + p2) * t +
@ -122,17 +128,17 @@ namespace Godot
);
}
public float DistanceSquaredTo(Vector3 b)
public real_t DistanceSquaredTo(Vector3 b)
{
return (b - this).LengthSquared();
}
public float DistanceTo(Vector3 b)
public real_t DistanceTo(Vector3 b)
{
return (b - this).Length();
}
public float Dot(Vector3 b)
public real_t Dot(Vector3 b)
{
return x * b.x + y * b.y + z * b.z;
}
@ -152,25 +158,25 @@ namespace Godot
return Mathf.Abs(LengthSquared() - 1.0f) < Mathf.Epsilon;
}
public float Length()
public real_t Length()
{
float x2 = x * x;
float y2 = y * y;
float z2 = z * z;
real_t x2 = x * x;
real_t y2 = y * y;
real_t z2 = z * z;
return Mathf.Sqrt(x2 + y2 + z2);
}
public float LengthSquared()
public real_t LengthSquared()
{
float x2 = x * x;
float y2 = y * y;
float z2 = z * z;
real_t x2 = x * x;
real_t y2 = y * y;
real_t z2 = z * z;
return x2 + y2 + z2;
}
public Vector3 LinearInterpolate(Vector3 b, float t)
public Vector3 LinearInterpolate(Vector3 b, real_t t)
{
return new Vector3
(
@ -215,11 +221,24 @@ namespace Godot
return 2.0f * n * Dot(n) - this;
}
public Vector3 Rotated(Vector3 axis, float phi)
public Vector3 Rotated(Vector3 axis, real_t phi)
{
return new Basis(axis, phi).Xform(this);
}
public void Set(real_t x, real_t y, real_t z)
{
this.x = x;
this.y = y;
this.z = z;
}
public void Set(Vector3 v)
{
this.x = v.x;
this.y = v.y;
this.z = v.z;
}
public Vector3 Slide(Vector3 n)
{
return this - n * Dot(n);
@ -244,12 +263,41 @@ namespace Godot
);
}
public Vector3(float x, float y, float z)
private static readonly Vector3 zero = new Vector3 (0, 0, 0);
private static readonly Vector3 one = new Vector3 (1, 1, 1);
private static readonly Vector3 negOne = new Vector3 (-1, -1, -1);
private static readonly Vector3 up = new Vector3 (0, 1, 0);
private static readonly Vector3 down = new Vector3 (0, -1, 0);
private static readonly Vector3 right = new Vector3 (1, 0, 0);
private static readonly Vector3 left = new Vector3 (-1, 0, 0);
private static readonly Vector3 forward = new Vector3 (0, 0, -1);
private static readonly Vector3 back = new Vector3 (0, 0, 1);
public static Vector3 Zero { get { return zero; } }
public static Vector3 One { get { return one; } }
public static Vector3 NegOne { get { return negOne; } }
public static Vector3 Up { get { return up; } }
public static Vector3 Down { get { return down; } }
public static Vector3 Right { get { return right; } }
public static Vector3 Left { get { return left; } }
public static Vector3 Forward { get { return forward; } }
public static Vector3 Back { get { return back; } }
// Constructors
public Vector3(real_t x, real_t y, real_t z)
{
this.x = x;
this.y = y;
this.z = z;
}
public Vector3(Vector3 v)
{
this.x = v.x;
this.y = v.y;
this.z = v.z;
}
public static Vector3 operator +(Vector3 left, Vector3 right)
{
@ -275,7 +323,7 @@ namespace Godot
return vec;
}
public static Vector3 operator *(Vector3 vec, float scale)
public static Vector3 operator *(Vector3 vec, real_t scale)
{
vec.x *= scale;
vec.y *= scale;
@ -283,7 +331,7 @@ namespace Godot
return vec;
}
public static Vector3 operator *(float scale, Vector3 vec)
public static Vector3 operator *(real_t scale, Vector3 vec)
{
vec.x *= scale;
vec.y *= scale;
@ -299,7 +347,7 @@ namespace Godot
return left;
}
public static Vector3 operator /(Vector3 vec, float scale)
public static Vector3 operator /(Vector3 vec, real_t scale)
{
vec.x /= scale;
vec.y /= scale;