2017-10-02 23:24:00 +02:00
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
// file: core/math/vector3.h
|
|
|
|
// commit: bd282ff43f23fe845f29a3e25c8efc01bd65ffb0
|
|
|
|
// file: core/math/vector3.cpp
|
|
|
|
// commit: 7ad14e7a3e6f87ddc450f7e34621eb5200808451
|
|
|
|
// file: core/variant_call.cpp
|
|
|
|
// commit: 5ad9be4c24e9d7dc5672fdc42cea896622fe5685
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
#if REAL_T_IS_DOUBLE
|
|
|
|
using real_t = System.Double;
|
|
|
|
#else
|
|
|
|
using real_t = System.Single;
|
|
|
|
#endif
|
|
|
|
|
2017-10-02 23:24:00 +02:00
|
|
|
namespace Godot
|
|
|
|
{
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
public struct Vector3 : IEquatable<Vector3>
|
|
|
|
{
|
|
|
|
public enum Axis
|
|
|
|
{
|
|
|
|
X = 0,
|
|
|
|
Y,
|
|
|
|
Z
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t x;
|
|
|
|
public real_t y;
|
|
|
|
public real_t z;
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t this[int index]
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
switch (index)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return x;
|
|
|
|
case 1:
|
|
|
|
return y;
|
|
|
|
case 2:
|
|
|
|
return z;
|
|
|
|
default:
|
|
|
|
throw new IndexOutOfRangeException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
switch (index)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
x = value;
|
|
|
|
return;
|
|
|
|
case 1:
|
|
|
|
y = value;
|
|
|
|
return;
|
|
|
|
case 2:
|
|
|
|
z = value;
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
throw new IndexOutOfRangeException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
internal void Normalize()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2018-03-01 08:51:35 +01:00
|
|
|
real_t length = this.Length();
|
2017-10-02 23:24:00 +02:00
|
|
|
|
|
|
|
if (length == 0f)
|
|
|
|
{
|
|
|
|
x = y = z = 0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x /= length;
|
|
|
|
y /= length;
|
|
|
|
z /= length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector3 Abs()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return new Vector3(Mathf.Abs(x), Mathf.Abs(y), Mathf.Abs(z));
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t AngleTo(Vector3 to)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return Mathf.Atan2(Cross(to).Length(), Dot(to));
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector3 Bounce(Vector3 n)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return -Reflect(n);
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector3 Ceil()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return new Vector3(Mathf.Ceil(x), Mathf.Ceil(y), Mathf.Ceil(z));
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector3 Cross(Vector3 b)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return new Vector3
|
|
|
|
(
|
|
|
|
(y * b.z) - (z * b.y),
|
|
|
|
(z * b.x) - (x * b.z),
|
|
|
|
(x * b.y) - (y * b.x)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public Vector3 CubicInterpolate(Vector3 b, Vector3 preA, Vector3 postB, real_t t)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
Vector3 p0 = preA;
|
|
|
|
Vector3 p1 = this;
|
|
|
|
Vector3 p2 = b;
|
|
|
|
Vector3 p3 = postB;
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
real_t t2 = t * t;
|
|
|
|
real_t t3 = t2 * t;
|
2017-10-02 23:24:00 +02:00
|
|
|
|
|
|
|
return 0.5f * (
|
|
|
|
(p1 * 2.0f) + (-p0 + p2) * t +
|
|
|
|
(2.0f * p0 - 5.0f * p1 + 4f * p2 - p3) * t2 +
|
|
|
|
(-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t DistanceSquaredTo(Vector3 b)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return (b - this).LengthSquared();
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t DistanceTo(Vector3 b)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return (b - this).Length();
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t Dot(Vector3 b)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return x * b.x + y * b.y + z * b.z;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector3 Floor()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return new Vector3(Mathf.Floor(x), Mathf.Floor(y), Mathf.Floor(z));
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector3 Inverse()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return new Vector3(1.0f / x, 1.0f / y, 1.0f / z);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public bool IsNormalized()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return Mathf.Abs(LengthSquared() - 1.0f) < Mathf.Epsilon;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t Length()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2018-03-01 08:51:35 +01:00
|
|
|
real_t x2 = x * x;
|
|
|
|
real_t y2 = y * y;
|
|
|
|
real_t z2 = z * z;
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
return Mathf.Sqrt(x2 + y2 + z2);
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t LengthSquared()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2018-03-01 08:51:35 +01:00
|
|
|
real_t x2 = x * x;
|
|
|
|
real_t y2 = y * y;
|
|
|
|
real_t z2 = z * z;
|
2017-10-02 23:24:00 +02:00
|
|
|
|
|
|
|
return x2 + y2 + z2;
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public Vector3 LinearInterpolate(Vector3 b, real_t t)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return new Vector3
|
|
|
|
(
|
|
|
|
x + (t * (b.x - x)),
|
|
|
|
y + (t * (b.y - y)),
|
|
|
|
z + (t * (b.z - z))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Axis MaxAxis()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return x < y ? (y < z ? Axis.Z : Axis.Y) : (x < z ? Axis.Z : Axis.X);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Axis MinAxis()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return x < y ? (x < z ? Axis.X : Axis.Z) : (y < z ? Axis.Y : Axis.Z);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector3 Normalized()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
Vector3 v = this;
|
2017-11-21 23:32:19 +01:00
|
|
|
v.Normalize();
|
2017-10-02 23:24:00 +02:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Basis Outer(Vector3 b)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return new Basis(
|
|
|
|
new Vector3(x * b.x, x * b.y, x * b.z),
|
|
|
|
new Vector3(y * b.x, y * b.y, y * b.z),
|
|
|
|
new Vector3(z * b.x, z * b.y, z * b.z)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector3 Reflect(Vector3 n)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
#if DEBUG
|
2017-11-21 23:32:19 +01:00
|
|
|
if (!n.IsNormalized())
|
2017-10-02 23:24:00 +02:00
|
|
|
throw new ArgumentException(String.Format("{0} is not normalized", n), nameof(n));
|
|
|
|
#endif
|
2017-11-21 23:32:19 +01:00
|
|
|
return 2.0f * n * Dot(n) - this;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public Vector3 Rotated(Vector3 axis, real_t phi)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return new Basis(axis, phi).Xform(this);
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector3 Slide(Vector3 n)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return this - n * Dot(n);
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector3 Snapped(Vector3 by)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return new Vector3
|
|
|
|
(
|
2017-11-21 23:32:19 +01:00
|
|
|
Mathf.Stepify(x, by.x),
|
|
|
|
Mathf.Stepify(y, by.y),
|
|
|
|
Mathf.Stepify(z, by.z)
|
2017-10-02 23:24:00 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Basis ToDiagonalMatrix()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return new Basis(
|
|
|
|
x, 0f, 0f,
|
|
|
|
0f, y, 0f,
|
|
|
|
0f, 0f, z
|
|
|
|
);
|
|
|
|
}
|
2018-03-01 08:51:35 +01:00
|
|
|
|
|
|
|
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)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
this.z = z;
|
|
|
|
}
|
2018-03-01 08:51:35 +01:00
|
|
|
public Vector3(Vector3 v)
|
|
|
|
{
|
|
|
|
this.x = v.x;
|
|
|
|
this.y = v.y;
|
|
|
|
this.z = v.z;
|
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
|
|
|
|
public static Vector3 operator +(Vector3 left, Vector3 right)
|
|
|
|
{
|
|
|
|
left.x += right.x;
|
|
|
|
left.y += right.y;
|
|
|
|
left.z += right.z;
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Vector3 operator -(Vector3 left, Vector3 right)
|
|
|
|
{
|
|
|
|
left.x -= right.x;
|
|
|
|
left.y -= right.y;
|
|
|
|
left.z -= right.z;
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Vector3 operator -(Vector3 vec)
|
|
|
|
{
|
|
|
|
vec.x = -vec.x;
|
|
|
|
vec.y = -vec.y;
|
|
|
|
vec.z = -vec.z;
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public static Vector3 operator *(Vector3 vec, real_t scale)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
vec.x *= scale;
|
|
|
|
vec.y *= scale;
|
|
|
|
vec.z *= scale;
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public static Vector3 operator *(real_t scale, Vector3 vec)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
vec.x *= scale;
|
|
|
|
vec.y *= scale;
|
|
|
|
vec.z *= scale;
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Vector3 operator *(Vector3 left, Vector3 right)
|
|
|
|
{
|
|
|
|
left.x *= right.x;
|
|
|
|
left.y *= right.y;
|
|
|
|
left.z *= right.z;
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public static Vector3 operator /(Vector3 vec, real_t scale)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
vec.x /= scale;
|
|
|
|
vec.y /= scale;
|
|
|
|
vec.z /= scale;
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Vector3 operator /(Vector3 left, Vector3 right)
|
|
|
|
{
|
|
|
|
left.x /= right.x;
|
|
|
|
left.y /= right.y;
|
|
|
|
left.z /= right.z;
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator ==(Vector3 left, Vector3 right)
|
|
|
|
{
|
|
|
|
return left.Equals(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator !=(Vector3 left, Vector3 right)
|
|
|
|
{
|
|
|
|
return !left.Equals(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator <(Vector3 left, Vector3 right)
|
|
|
|
{
|
|
|
|
if (left.x == right.x)
|
|
|
|
{
|
|
|
|
if (left.y == right.y)
|
|
|
|
return left.z < right.z;
|
|
|
|
else
|
|
|
|
return left.y < right.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
return left.x < right.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator >(Vector3 left, Vector3 right)
|
|
|
|
{
|
|
|
|
if (left.x == right.x)
|
|
|
|
{
|
|
|
|
if (left.y == right.y)
|
|
|
|
return left.z > right.z;
|
|
|
|
else
|
|
|
|
return left.y > right.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
return left.x > right.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator <=(Vector3 left, Vector3 right)
|
|
|
|
{
|
|
|
|
if (left.x == right.x)
|
|
|
|
{
|
|
|
|
if (left.y == right.y)
|
|
|
|
return left.z <= right.z;
|
|
|
|
else
|
|
|
|
return left.y < right.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
return left.x < right.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator >=(Vector3 left, Vector3 right)
|
|
|
|
{
|
|
|
|
if (left.x == right.x)
|
|
|
|
{
|
|
|
|
if (left.y == right.y)
|
|
|
|
return left.z >= right.z;
|
|
|
|
else
|
|
|
|
return left.y > right.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
return left.x > right.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
{
|
|
|
|
if (obj is Vector3)
|
|
|
|
{
|
|
|
|
return Equals((Vector3)obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Equals(Vector3 other)
|
|
|
|
{
|
|
|
|
return x == other.x && y == other.y && z == other.z;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
{
|
|
|
|
return y.GetHashCode() ^ x.GetHashCode() ^ z.GetHashCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return String.Format("({0}, {1}, {2})", new object[]
|
|
|
|
{
|
|
|
|
this.x.ToString(),
|
|
|
|
this.y.ToString(),
|
|
|
|
this.z.ToString()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public string ToString(string format)
|
|
|
|
{
|
|
|
|
return String.Format("({0}, {1}, {2})", new object[]
|
|
|
|
{
|
|
|
|
this.x.ToString(format),
|
|
|
|
this.y.ToString(format),
|
|
|
|
this.z.ToString(format)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|