2017-10-02 23:24:00 +02:00
|
|
|
// file: core/math/math_2d.h
|
|
|
|
// commit: 7ad14e7a3e6f87ddc450f7e34621eb5200808451
|
|
|
|
// file: core/math/math_2d.cpp
|
|
|
|
// commit: 7ad14e7a3e6f87ddc450f7e34621eb5200808451
|
|
|
|
// file: core/variant_call.cpp
|
|
|
|
// commit: 5ad9be4c24e9d7dc5672fdc42cea896622fe5685
|
2018-04-08 05:28:24 +02:00
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
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 Vector2 : IEquatable<Vector2>
|
|
|
|
{
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t x;
|
|
|
|
public real_t y;
|
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;
|
|
|
|
default:
|
|
|
|
throw new IndexOutOfRangeException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
switch (index)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
x = value;
|
|
|
|
return;
|
|
|
|
case 1:
|
|
|
|
y = 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
|
|
|
{
|
2019-04-02 00:13:38 +02:00
|
|
|
real_t lengthsq = LengthSquared();
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2019-04-02 00:13:38 +02:00
|
|
|
if (lengthsq == 0)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2019-04-02 00:13:38 +02:00
|
|
|
x = y = 0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
real_t length = Mathf.Sqrt(lengthsq);
|
2017-10-02 23:24:00 +02:00
|
|
|
x /= length;
|
|
|
|
y /= length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-20 12:29:54 +02:00
|
|
|
public real_t Cross(Vector2 b)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return x * b.y - y * b.x;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector2 Abs()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return new Vector2(Mathf.Abs(x), Mathf.Abs(y));
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t Angle()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return Mathf.Atan2(y, x);
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t AngleTo(Vector2 to)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return Mathf.Atan2(Cross(to), Dot(to));
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t AngleToPoint(Vector2 to)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2019-02-23 23:28:27 +01:00
|
|
|
return Mathf.Atan2(y - to.y, x - to.x);
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t Aspect()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return x / y;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector2 Bounce(Vector2 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
|
|
|
}
|
|
|
|
|
2018-05-04 14:17:10 +02:00
|
|
|
public Vector2 Ceil()
|
|
|
|
{
|
|
|
|
return new Vector2(Mathf.Ceil(x), Mathf.Ceil(y));
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public Vector2 Clamped(real_t length)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2018-04-08 05:30:43 +02:00
|
|
|
var v = this;
|
2018-04-17 00:53:27 +02:00
|
|
|
real_t l = Length();
|
2017-10-02 23:24:00 +02:00
|
|
|
|
|
|
|
if (l > 0 && length < l)
|
|
|
|
{
|
|
|
|
v /= l;
|
|
|
|
v *= length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public Vector2 CubicInterpolate(Vector2 b, Vector2 preA, Vector2 postB, real_t t)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2018-04-08 05:30:43 +02:00
|
|
|
var p0 = preA;
|
|
|
|
var p1 = this;
|
|
|
|
var p2 = b;
|
|
|
|
var p3 = postB;
|
2017-10-02 23:24:00 +02:00
|
|
|
|
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
|
|
|
|
2018-04-08 05:39:35 +02:00
|
|
|
return 0.5f * (p1 * 2.0f +
|
2017-10-02 23:24:00 +02:00
|
|
|
(-p0 + p2) * t +
|
|
|
|
(2.0f * p0 - 5.0f * p1 + 4 * p2 - p3) * t2 +
|
|
|
|
(-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3);
|
|
|
|
}
|
|
|
|
|
2019-03-27 11:51:05 +01:00
|
|
|
public Vector2 DirectionTo(Vector2 b)
|
|
|
|
{
|
|
|
|
return new Vector2(b.x - x, b.y - y).Normalized();
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t DistanceSquaredTo(Vector2 to)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return (x - to.x) * (x - to.x) + (y - to.y) * (y - to.y);
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t DistanceTo(Vector2 to)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return Mathf.Sqrt((x - to.x) * (x - to.x) + (y - to.y) * (y - to.y));
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t Dot(Vector2 with)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return x * with.x + y * with.y;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector2 Floor()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return new Vector2(Mathf.Floor(x), Mathf.Floor(y));
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return Mathf.Sqrt(x * x + y * y);
|
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
|
|
|
{
|
|
|
|
return x * x + y * y;
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public Vector2 LinearInterpolate(Vector2 b, real_t t)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2018-04-08 05:30:43 +02:00
|
|
|
var res = this;
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2018-04-08 05:39:35 +02:00
|
|
|
res.x += t * (b.x - x);
|
|
|
|
res.y += t * (b.y - y);
|
2017-10-02 23:24:00 +02:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector2 Normalized()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2019-04-02 00:13:38 +02:00
|
|
|
var v = this;
|
|
|
|
v.Normalize();
|
|
|
|
return v;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 19:27:35 +02:00
|
|
|
public Vector2 Project(Vector2 onNormal)
|
|
|
|
{
|
|
|
|
return onNormal * (Dot(onNormal) / onNormal.LengthSquared());
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector2 Reflect(Vector2 n)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
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 Vector2 Rotated(real_t phi)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2018-03-01 08:51:35 +01:00
|
|
|
real_t rads = Angle() + phi;
|
2017-11-21 23:32:19 +01:00
|
|
|
return new Vector2(Mathf.Cos(rads), Mathf.Sin(rads)) * Length();
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2018-05-04 14:17:10 +02:00
|
|
|
public Vector2 Round()
|
|
|
|
{
|
|
|
|
return new Vector2(Mathf.Round(x), Mathf.Round(y));
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public void Set(real_t x, real_t y)
|
|
|
|
{
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
}
|
|
|
|
public void Set(Vector2 v)
|
|
|
|
{
|
2018-04-08 05:28:24 +02:00
|
|
|
x = v.x;
|
|
|
|
y = v.y;
|
2018-03-01 08:51:35 +01:00
|
|
|
}
|
2018-11-20 11:14:07 +01:00
|
|
|
|
2018-05-20 12:29:54 +02:00
|
|
|
public Vector2 Slerp(Vector2 b, real_t t)
|
|
|
|
{
|
|
|
|
real_t theta = AngleTo(b);
|
|
|
|
return Rotated(theta * t);
|
|
|
|
}
|
2018-03-01 08:51:35 +01:00
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector2 Slide(Vector2 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 Vector2 Snapped(Vector2 by)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return new Vector2(Mathf.Stepify(x, by.x), Mathf.Stepify(y, by.y));
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector2 Tangent()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return new Vector2(y, -x);
|
|
|
|
}
|
2018-08-10 20:46:03 +02:00
|
|
|
|
|
|
|
// Constants
|
|
|
|
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 _inf = new Vector2(Mathf.Inf, Mathf.Inf);
|
2018-11-20 11:14:07 +01:00
|
|
|
|
2018-08-10 20:46:03 +02:00
|
|
|
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 NegOne { get { return _negOne; } }
|
|
|
|
public static Vector2 One { get { return _one; } }
|
|
|
|
public static Vector2 Inf { get { return _inf; } }
|
|
|
|
|
|
|
|
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; } }
|
2018-03-01 08:51:35 +01:00
|
|
|
|
|
|
|
// Constructors
|
|
|
|
public Vector2(real_t x, real_t y)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
}
|
2018-03-01 08:51:35 +01:00
|
|
|
public Vector2(Vector2 v)
|
|
|
|
{
|
2018-04-08 05:28:24 +02:00
|
|
|
x = v.x;
|
|
|
|
y = v.y;
|
2018-03-01 08:51:35 +01:00
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
|
|
|
|
public static Vector2 operator +(Vector2 left, Vector2 right)
|
|
|
|
{
|
|
|
|
left.x += right.x;
|
|
|
|
left.y += right.y;
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Vector2 operator -(Vector2 left, Vector2 right)
|
|
|
|
{
|
|
|
|
left.x -= right.x;
|
|
|
|
left.y -= right.y;
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Vector2 operator -(Vector2 vec)
|
|
|
|
{
|
|
|
|
vec.x = -vec.x;
|
|
|
|
vec.y = -vec.y;
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public static Vector2 operator *(Vector2 vec, real_t scale)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
vec.x *= scale;
|
|
|
|
vec.y *= scale;
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public static Vector2 operator *(real_t scale, Vector2 vec)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
vec.x *= scale;
|
|
|
|
vec.y *= scale;
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Vector2 operator *(Vector2 left, Vector2 right)
|
|
|
|
{
|
|
|
|
left.x *= right.x;
|
|
|
|
left.y *= right.y;
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public static Vector2 operator /(Vector2 vec, real_t scale)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
vec.x /= scale;
|
|
|
|
vec.y /= scale;
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Vector2 operator /(Vector2 left, Vector2 right)
|
|
|
|
{
|
|
|
|
left.x /= right.x;
|
|
|
|
left.y /= right.y;
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator ==(Vector2 left, Vector2 right)
|
|
|
|
{
|
|
|
|
return left.Equals(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator !=(Vector2 left, Vector2 right)
|
|
|
|
{
|
|
|
|
return !left.Equals(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator <(Vector2 left, Vector2 right)
|
|
|
|
{
|
2019-04-02 00:13:38 +02:00
|
|
|
if (Mathf.IsEqualApprox(left.x, right.x))
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return left.y < right.y;
|
|
|
|
}
|
2018-04-08 05:28:24 +02:00
|
|
|
|
|
|
|
return left.x < right.x;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator >(Vector2 left, Vector2 right)
|
|
|
|
{
|
2019-04-02 00:13:38 +02:00
|
|
|
if (Mathf.IsEqualApprox(left.x, right.x))
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return left.y > right.y;
|
|
|
|
}
|
2018-04-08 05:28:24 +02:00
|
|
|
|
|
|
|
return left.x > right.x;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator <=(Vector2 left, Vector2 right)
|
|
|
|
{
|
2019-04-02 00:13:38 +02:00
|
|
|
if (Mathf.IsEqualApprox(left.x, right.x))
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return left.y <= right.y;
|
|
|
|
}
|
2018-04-08 05:28:24 +02:00
|
|
|
|
|
|
|
return left.x <= right.x;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator >=(Vector2 left, Vector2 right)
|
|
|
|
{
|
2019-04-02 00:13:38 +02:00
|
|
|
if (Mathf.IsEqualApprox(left.x, right.x))
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return left.y >= right.y;
|
|
|
|
}
|
2018-04-08 05:28:24 +02:00
|
|
|
|
|
|
|
return left.x >= right.x;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
{
|
|
|
|
if (obj is Vector2)
|
|
|
|
{
|
|
|
|
return Equals((Vector2)obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Equals(Vector2 other)
|
|
|
|
{
|
2019-04-02 00:13:38 +02:00
|
|
|
return Mathf.IsEqualApprox(x, other.x) && Mathf.IsEqualApprox(y, other.y);
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
{
|
|
|
|
return y.GetHashCode() ^ x.GetHashCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return String.Format("({0}, {1})", new object[]
|
|
|
|
{
|
2018-04-08 05:28:24 +02:00
|
|
|
x.ToString(),
|
|
|
|
y.ToString()
|
2017-10-02 23:24:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public string ToString(string format)
|
|
|
|
{
|
|
|
|
return String.Format("({0}, {1})", new object[]
|
|
|
|
{
|
2018-04-08 05:28:24 +02:00
|
|
|
x.ToString(format),
|
|
|
|
y.ToString(format)
|
2017-10-02 23:24:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|