2017-10-02 23:24:00 +02:00
|
|
|
// file: core/math/vector3.h
|
|
|
|
// commit: bd282ff43f23fe845f29a3e25c8efc01bd65ffb0
|
|
|
|
// file: core/math/vector3.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
|
|
|
|
{
|
2019-08-05 03:50:28 +02:00
|
|
|
/// <summary>
|
|
|
|
/// 3-element structure that can be used to represent positions in 3D space or any other pair of numeric values.
|
|
|
|
/// </summary>
|
2019-08-08 03:29:40 +02:00
|
|
|
[Serializable]
|
2017-10-02 23:24:00 +02:00
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
public struct Vector3 : IEquatable<Vector3>
|
|
|
|
{
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Enumerated index values for the axes.
|
|
|
|
/// Returned by <see cref="MaxAxis"/> and <see cref="MinAxis"/>.
|
|
|
|
/// </summary>
|
2017-10-02 23:24:00 +02:00
|
|
|
public enum Axis
|
|
|
|
{
|
|
|
|
X = 0,
|
|
|
|
Y,
|
|
|
|
Z
|
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The vector's X component. Also accessible by using the index position `[0]`.
|
|
|
|
/// </summary>
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t x;
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The vector's Y component. Also accessible by using the index position `[1]`.
|
|
|
|
/// </summary>
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t y;
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The vector's Z component. Also accessible by using the index position `[2]`.
|
|
|
|
/// </summary>
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t z;
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Access vector components using their index.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>`[0]` is equivalent to `.x`, `[1]` is equivalent to `.y`, `[2]` is equivalent to `.z`.</value>
|
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
|
|
|
{
|
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
|
|
|
{
|
|
|
|
x = y = z = 0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-02 00:13:38 +02:00
|
|
|
real_t length = Mathf.Sqrt(lengthsq);
|
2017-10-02 23:24:00 +02:00
|
|
|
x /= length;
|
|
|
|
y /= length;
|
|
|
|
z /= length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns a new vector with all components in absolute values (i.e. positive).
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A vector with <see cref="Mathf.Abs(real_t)"/> called on each component.</returns>
|
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
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
2021-02-15 15:01:46 +01:00
|
|
|
/// Returns the unsigned minimum angle to the given vector, in radians.
|
2020-07-07 23:29:35 +02:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="to">The other vector to compare this vector to.</param>
|
2021-02-15 15:01:46 +01:00
|
|
|
/// <returns>The unsigned angle between the two vectors, in radians.</returns>
|
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
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns this vector "bounced off" from a plane defined by the given normal.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="normal">The normal vector defining the plane to bounce off. Must be normalized.</param>
|
|
|
|
/// <returns>The bounced vector.</returns>
|
|
|
|
public Vector3 Bounce(Vector3 normal)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2020-07-07 23:29:35 +02:00
|
|
|
return -Reflect(normal);
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns a new vector with all components rounded up (towards positive infinity).
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A vector with <see cref="Mathf.Ceil"/> called on each component.</returns>
|
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));
|
2021-02-01 06:10:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns a new vector with all components clamped between the
|
|
|
|
/// components of `min` and `max` using
|
|
|
|
/// <see cref="Mathf.Clamp(real_t, real_t, real_t)"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="min">The vector with minimum allowed values.</param>
|
|
|
|
/// <param name="max">The vector with maximum allowed values.</param>
|
|
|
|
/// <returns>The vector with all components clamped.</returns>
|
|
|
|
public Vector3 Clamp(Vector3 min, Vector3 max)
|
|
|
|
{
|
|
|
|
return new Vector3
|
|
|
|
(
|
|
|
|
Mathf.Clamp(x, min.x, max.x),
|
|
|
|
Mathf.Clamp(y, min.y, max.y),
|
|
|
|
Mathf.Clamp(z, min.z, max.z)
|
|
|
|
);
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the cross product of this vector and `b`.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="b">The other vector.</param>
|
|
|
|
/// <returns>The cross product vector.</returns>
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector3 Cross(Vector3 b)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return new Vector3
|
|
|
|
(
|
2018-04-08 05:39:35 +02:00
|
|
|
y * b.z - z * b.y,
|
|
|
|
z * b.x - x * b.z,
|
|
|
|
x * b.y - y * b.x
|
2017-10-02 23:24:00 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Performs a cubic interpolation between vectors `preA`, this vector,
|
|
|
|
/// `b`, and `postB`, by the given amount `t`.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="b">The destination vector.</param>
|
|
|
|
/// <param name="preA">A vector before this vector.</param>
|
|
|
|
/// <param name="postB">A vector after `b`.</param>
|
2020-12-07 09:16:31 +01:00
|
|
|
/// <param name="weight">A value on the range of 0.0 to 1.0, representing the amount of interpolation.</param>
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <returns>The interpolated vector.</returns>
|
2020-12-07 09:16:31 +01:00
|
|
|
public Vector3 CubicInterpolate(Vector3 b, Vector3 preA, Vector3 postB, real_t weight)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2020-07-07 23:29:35 +02:00
|
|
|
Vector3 p0 = preA;
|
|
|
|
Vector3 p1 = this;
|
|
|
|
Vector3 p2 = b;
|
|
|
|
Vector3 p3 = postB;
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2020-12-07 09:16:31 +01:00
|
|
|
real_t t = weight;
|
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 * (
|
2018-04-08 05:39:35 +02:00
|
|
|
p1 * 2.0f + (-p0 + p2) * t +
|
2017-10-02 23:24:00 +02:00
|
|
|
(2.0f * p0 - 5.0f * p1 + 4f * p2 - p3) * t2 +
|
|
|
|
(-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the normalized vector pointing from this vector to `b`.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="b">The other vector to point towards.</param>
|
|
|
|
/// <returns>The direction from this vector to `b`.</returns>
|
2019-03-27 11:51:05 +01:00
|
|
|
public Vector3 DirectionTo(Vector3 b)
|
|
|
|
{
|
|
|
|
return new Vector3(b.x - x, b.y - y, b.z - z).Normalized();
|
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the squared distance between this vector and `b`.
|
|
|
|
/// This method runs faster than <see cref="DistanceTo"/>, so prefer it if
|
|
|
|
/// you need to compare vectors or need the squared distance for some formula.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="b">The other vector to use.</param>
|
|
|
|
/// <returns>The squared distance between the two vectors.</returns>
|
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
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the distance between this vector and `b`.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="b">The other vector to use.</param>
|
|
|
|
/// <returns>The distance between the two vectors.</returns>
|
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
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the dot product of this vector and `b`.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="b">The other vector to use.</param>
|
|
|
|
/// <returns>The dot product of the two vectors.</returns>
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns a new vector with all components rounded down (towards negative infinity).
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A vector with <see cref="Mathf.Floor"/> called on each component.</returns>
|
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
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the inverse of this vector. This is the same as `new Vector3(1 / v.x, 1 / v.y, 1 / v.z)`.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The inverse of this vector.</returns>
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector3 Inverse()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2020-07-07 23:29:35 +02:00
|
|
|
return new Vector3(1 / x, 1 / y, 1 / z);
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns true if the vector is normalized, and false otherwise.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A bool indicating whether or not the vector is normalized.</returns>
|
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
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the length (magnitude) of this vector.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The length of this vector.</returns>
|
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
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the squared length (squared magnitude) of this vector.
|
|
|
|
/// This method runs faster than <see cref="Length"/>, so prefer it if
|
|
|
|
/// you need to compare vectors or need the squared length for some formula.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The squared length of this vector.</returns>
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the result of the linear interpolation between
|
|
|
|
/// this vector and `to` by amount `weight`.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="to">The destination vector for interpolation.</param>
|
|
|
|
/// <param name="weight">A value on the range of 0.0 to 1.0, representing the amount of interpolation.</param>
|
|
|
|
/// <returns>The resulting vector of the interpolation.</returns>
|
2020-03-23 05:55:42 +01:00
|
|
|
public Vector3 Lerp(Vector3 to, real_t weight)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return new Vector3
|
|
|
|
(
|
2020-03-23 05:55:42 +01:00
|
|
|
Mathf.Lerp(x, to.x, weight),
|
|
|
|
Mathf.Lerp(y, to.y, weight),
|
|
|
|
Mathf.Lerp(z, to.z, weight)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the result of the linear interpolation between
|
|
|
|
/// this vector and `to` by the vector amount `weight`.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="to">The destination vector for interpolation.</param>
|
|
|
|
/// <param name="weight">A vector with components on the range of 0.0 to 1.0, representing the amount of interpolation.</param>
|
|
|
|
/// <returns>The resulting vector of the interpolation.</returns>
|
2020-03-23 05:55:42 +01:00
|
|
|
public Vector3 Lerp(Vector3 to, Vector3 weight)
|
|
|
|
{
|
|
|
|
return new Vector3
|
|
|
|
(
|
|
|
|
Mathf.Lerp(x, to.x, weight.x),
|
|
|
|
Mathf.Lerp(y, to.y, weight.y),
|
|
|
|
Mathf.Lerp(z, to.z, weight.z)
|
2017-10-02 23:24:00 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-01 06:42:00 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the vector with a maximum length by limiting its length to `length`.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="length">The length to limit to.</param>
|
|
|
|
/// <returns>The vector with its length limited.</returns>
|
|
|
|
public Vector3 LimitLength(real_t length = 1.0f)
|
|
|
|
{
|
|
|
|
Vector3 v = this;
|
|
|
|
real_t l = Length();
|
|
|
|
|
|
|
|
if (l > 0 && length < l)
|
|
|
|
{
|
|
|
|
v /= l;
|
|
|
|
v *= length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the axis of the vector's largest value. See <see cref="Axis"/>.
|
|
|
|
/// If all components are equal, this method returns <see cref="Axis.X"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The index of the largest axis.</returns>
|
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);
|
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the axis of the vector's smallest value. See <see cref="Axis"/>.
|
|
|
|
/// If all components are equal, this method returns <see cref="Axis.Z"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The index of the smallest axis.</returns>
|
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);
|
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Moves this vector toward `to` by the fixed `delta` amount.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="to">The vector to move towards.</param>
|
|
|
|
/// <param name="delta">The amount to move towards by.</param>
|
|
|
|
/// <returns>The resulting vector.</returns>
|
|
|
|
public Vector3 MoveToward(Vector3 to, real_t delta)
|
|
|
|
{
|
|
|
|
var v = this;
|
|
|
|
var vd = to - v;
|
|
|
|
var len = vd.Length();
|
|
|
|
return len <= delta || len < Mathf.Epsilon ? to : v + vd / len * delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the vector scaled to unit length. Equivalent to `v / v.Length()`.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A normalized version of the vector.</returns>
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector3 Normalized()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2018-04-08 05:30:43 +02:00
|
|
|
var v = this;
|
2017-11-21 23:32:19 +01:00
|
|
|
v.Normalize();
|
2017-10-02 23:24:00 +02:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the outer product with `b`.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="b">The other vector.</param>
|
|
|
|
/// <returns>A <see cref="Basis"/> representing the outer product matrix.</returns>
|
2017-11-21 23:32:19 +01:00
|
|
|
public Basis Outer(Vector3 b)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return new Basis(
|
2018-11-20 22:23:09 +01:00
|
|
|
x * b.x, x * b.y, x * b.z,
|
|
|
|
y * b.x, y * b.y, y * b.z,
|
|
|
|
z * b.x, z * b.y, z * b.z
|
2017-10-02 23:24:00 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns a vector composed of the <see cref="Mathf.PosMod(real_t, real_t)"/> of this vector's components and `mod`.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="mod">A value representing the divisor of the operation.</param>
|
|
|
|
/// <returns>A vector with each component <see cref="Mathf.PosMod(real_t, real_t)"/> by `mod`.</returns>
|
2019-08-05 03:50:28 +02:00
|
|
|
public Vector3 PosMod(real_t mod)
|
|
|
|
{
|
|
|
|
Vector3 v;
|
|
|
|
v.x = Mathf.PosMod(x, mod);
|
|
|
|
v.y = Mathf.PosMod(y, mod);
|
|
|
|
v.z = Mathf.PosMod(z, mod);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns a vector composed of the <see cref="Mathf.PosMod(real_t, real_t)"/> of this vector's components and `modv`'s components.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="modv">A vector representing the divisors of the operation.</param>
|
|
|
|
/// <returns>A vector with each component <see cref="Mathf.PosMod(real_t, real_t)"/> by `modv`'s components.</returns>
|
2019-08-05 03:50:28 +02:00
|
|
|
public Vector3 PosMod(Vector3 modv)
|
|
|
|
{
|
|
|
|
Vector3 v;
|
|
|
|
v.x = Mathf.PosMod(x, modv.x);
|
|
|
|
v.y = Mathf.PosMod(y, modv.y);
|
|
|
|
v.z = Mathf.PosMod(z, modv.z);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns this vector projected onto another vector `b`.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="onNormal">The vector to project onto.</param>
|
|
|
|
/// <returns>The projected vector.</returns>
|
2018-08-22 19:27:35 +02:00
|
|
|
public Vector3 Project(Vector3 onNormal)
|
|
|
|
{
|
|
|
|
return onNormal * (Dot(onNormal) / onNormal.LengthSquared());
|
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns this vector reflected from a plane defined by the given `normal`.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="normal">The normal vector defining the plane to reflect from. Must be normalized.</param>
|
|
|
|
/// <returns>The reflected vector.</returns>
|
|
|
|
public Vector3 Reflect(Vector3 normal)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
#if DEBUG
|
2020-07-07 23:29:35 +02:00
|
|
|
if (!normal.IsNormalized())
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Argument is not normalized", nameof(normal));
|
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
#endif
|
2020-07-07 23:29:35 +02:00
|
|
|
return 2.0f * Dot(normal) * normal - this;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Rotates this vector around a given `axis` vector by `phi` radians.
|
|
|
|
/// The `axis` vector must be a normalized vector.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="axis">The vector to rotate around. Must be normalized.</param>
|
|
|
|
/// <param name="phi">The angle to rotate by, in radians.</param>
|
|
|
|
/// <returns>The rotated vector.</returns>
|
|
|
|
public Vector3 Rotated(Vector3 axis, real_t phi)
|
2018-05-04 14:17:10 +02:00
|
|
|
{
|
2020-07-07 23:29:35 +02:00
|
|
|
#if DEBUG
|
|
|
|
if (!axis.IsNormalized())
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Argument is not normalized", nameof(axis));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return new Basis(axis, phi).Xform(this);
|
2018-05-04 14:17:10 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns this vector with all components rounded to the nearest integer,
|
|
|
|
/// with halfway cases rounded towards the nearest multiple of two.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The rounded vector.</returns>
|
|
|
|
public Vector3 Round()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2020-07-07 23:29:35 +02:00
|
|
|
return new Vector3(Mathf.Round(x), Mathf.Round(y), Mathf.Round(z));
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns a vector with each component set to one or negative one, depending
|
|
|
|
/// on the signs of this vector's components, or zero if the component is zero,
|
|
|
|
/// by calling <see cref="Mathf.Sign(real_t)"/> on each component.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A vector with all components as either `1`, `-1`, or `0`.</returns>
|
2019-08-05 03:50:28 +02:00
|
|
|
public Vector3 Sign()
|
|
|
|
{
|
|
|
|
Vector3 v;
|
|
|
|
v.x = Mathf.Sign(x);
|
|
|
|
v.y = Mathf.Sign(y);
|
|
|
|
v.z = Mathf.Sign(z);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2021-02-15 15:01:46 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the signed angle to the given vector, in radians.
|
|
|
|
/// The sign of the angle is positive in a counter-clockwise
|
|
|
|
/// direction and negative in a clockwise direction when viewed
|
|
|
|
/// from the side specified by the `axis`.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="to">The other vector to compare this vector to.</param>
|
|
|
|
/// <param name="axis">The reference axis to use for the angle sign.</param>
|
|
|
|
/// <returns>The signed angle between the two vectors, in radians.</returns>
|
|
|
|
public real_t SignedAngleTo(Vector3 to, Vector3 axis)
|
|
|
|
{
|
|
|
|
Vector3 crossTo = Cross(to);
|
|
|
|
real_t unsignedAngle = Mathf.Atan2(crossTo.Length(), Dot(to));
|
|
|
|
real_t sign = crossTo.Dot(axis);
|
|
|
|
return (sign < 0) ? -unsignedAngle : unsignedAngle;
|
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the result of the spherical linear interpolation between
|
|
|
|
/// this vector and `to` by amount `weight`.
|
|
|
|
///
|
|
|
|
/// Note: Both vectors must be normalized.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="to">The destination vector for interpolation. Must be normalized.</param>
|
|
|
|
/// <param name="weight">A value on the range of 0.0 to 1.0, representing the amount of interpolation.</param>
|
|
|
|
/// <returns>The resulting vector of the interpolation.</returns>
|
|
|
|
public Vector3 Slerp(Vector3 to, real_t weight)
|
2018-05-20 12:29:54 +02:00
|
|
|
{
|
2020-01-23 15:54:34 +01:00
|
|
|
#if DEBUG
|
|
|
|
if (!IsNormalized())
|
2020-07-07 23:29:35 +02:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Vector3.Slerp: From vector is not normalized.");
|
|
|
|
}
|
|
|
|
if (!to.IsNormalized())
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Vector3.Slerp: `to` is not normalized.");
|
|
|
|
}
|
2020-01-23 15:54:34 +01:00
|
|
|
#endif
|
2020-07-07 23:29:35 +02:00
|
|
|
real_t theta = AngleTo(to);
|
|
|
|
return Rotated(Cross(to), theta * weight);
|
2018-05-20 12:29:54 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns this vector slid along a plane defined by the given normal.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="normal">The normal vector defining the plane to slide on.</param>
|
|
|
|
/// <returns>The slid vector.</returns>
|
|
|
|
public Vector3 Slide(Vector3 normal)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2020-07-07 23:29:35 +02:00
|
|
|
return this - normal * Dot(normal);
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns this vector with each component snapped to the nearest multiple of `step`.
|
|
|
|
/// This can also be used to round to an arbitrary number of decimals.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="step">A vector value representing the step size to snap to.</param>
|
|
|
|
/// <returns>The snapped vector.</returns>
|
|
|
|
public Vector3 Snapped(Vector3 step)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return new Vector3
|
|
|
|
(
|
2020-12-21 19:02:57 +01:00
|
|
|
Mathf.Snapped(x, step.x),
|
|
|
|
Mathf.Snapped(y, step.y),
|
|
|
|
Mathf.Snapped(z, step.z)
|
2017-10-02 23:24:00 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns a diagonal matrix with the vector as main diagonal.
|
|
|
|
///
|
|
|
|
/// This is equivalent to a Basis with no rotation or shearing and
|
|
|
|
/// this vector's components set as the scale.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A Basis with the vector as its main diagonal.</returns>
|
2017-11-21 23:32:19 +01:00
|
|
|
public Basis ToDiagonalMatrix()
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
return new Basis(
|
2020-07-07 23:29:35 +02:00
|
|
|
x, 0, 0,
|
|
|
|
0, y, 0,
|
|
|
|
0, 0, z
|
2017-10-02 23:24:00 +02:00
|
|
|
);
|
|
|
|
}
|
2018-11-20 11:14:07 +01:00
|
|
|
|
2018-08-10 20:46:03 +02:00
|
|
|
// Constants
|
|
|
|
private static readonly Vector3 _zero = new Vector3(0, 0, 0);
|
|
|
|
private static readonly Vector3 _one = new Vector3(1, 1, 1);
|
|
|
|
private static readonly Vector3 _inf = new Vector3(Mathf.Inf, Mathf.Inf, Mathf.Inf);
|
2018-11-20 11:14:07 +01:00
|
|
|
|
2018-08-10 20:46:03 +02:00
|
|
|
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);
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Zero vector, a vector with all components set to `0`.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>Equivalent to `new Vector3(0, 0, 0)`</value>
|
2018-08-10 20:46:03 +02:00
|
|
|
public static Vector3 Zero { get { return _zero; } }
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// One vector, a vector with all components set to `1`.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>Equivalent to `new Vector3(1, 1, 1)`</value>
|
2018-08-10 20:46:03 +02:00
|
|
|
public static Vector3 One { get { return _one; } }
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Infinity vector, a vector with all components set to `Mathf.Inf`.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>Equivalent to `new Vector3(Mathf.Inf, Mathf.Inf, Mathf.Inf)`</value>
|
2018-08-10 20:46:03 +02:00
|
|
|
public static Vector3 Inf { get { return _inf; } }
|
2018-11-20 11:14:07 +01:00
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Up unit vector.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>Equivalent to `new Vector3(0, 1, 0)`</value>
|
2018-08-10 20:46:03 +02:00
|
|
|
public static Vector3 Up { get { return _up; } }
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Down unit vector.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>Equivalent to `new Vector3(0, -1, 0)`</value>
|
2018-08-10 20:46:03 +02:00
|
|
|
public static Vector3 Down { get { return _down; } }
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Right unit vector. Represents the local direction of right,
|
|
|
|
/// and the global direction of east.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>Equivalent to `new Vector3(1, 0, 0)`</value>
|
2018-08-10 20:46:03 +02:00
|
|
|
public static Vector3 Right { get { return _right; } }
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Left unit vector. Represents the local direction of left,
|
|
|
|
/// and the global direction of west.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>Equivalent to `new Vector3(-1, 0, 0)`</value>
|
2018-08-10 20:46:03 +02:00
|
|
|
public static Vector3 Left { get { return _left; } }
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Forward unit vector. Represents the local direction of forward,
|
|
|
|
/// and the global direction of north.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>Equivalent to `new Vector3(0, 0, -1)`</value>
|
2018-08-10 20:46:03 +02:00
|
|
|
public static Vector3 Forward { get { return _forward; } }
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Back unit vector. Represents the local direction of back,
|
|
|
|
/// and the global direction of south.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>Equivalent to `new Vector3(0, 0, 1)`</value>
|
2018-08-10 20:46:03 +02:00
|
|
|
public static Vector3 Back { get { return _back; } }
|
2018-03-01 08:51:35 +01:00
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Constructs a new <see cref="Vector3"/> with the given components.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The vector's X component.</param>
|
|
|
|
/// <param name="y">The vector's Y component.</param>
|
|
|
|
/// <param name="z">The vector's Z component.</param>
|
2018-03-01 08:51:35 +01:00
|
|
|
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;
|
|
|
|
}
|
2020-07-07 23:29:35 +02:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Constructs a new <see cref="Vector3"/> from an existing <see cref="Vector3"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="v">The existing <see cref="Vector3"/>.</param>
|
2018-03-01 08:51:35 +01:00
|
|
|
public Vector3(Vector3 v)
|
|
|
|
{
|
2018-04-08 05:28:24 +02:00
|
|
|
x = v.x;
|
|
|
|
y = v.y;
|
|
|
|
z = v.z;
|
2018-03-01 08:51:35 +01:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2020-03-17 23:03:04 +01:00
|
|
|
public static Vector3 operator /(Vector3 vec, real_t divisor)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2020-03-17 23:03:04 +01:00
|
|
|
vec.x /= divisor;
|
|
|
|
vec.y /= divisor;
|
|
|
|
vec.z /= divisor;
|
2017-10-02 23:24:00 +02:00
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
2020-03-17 23:03:04 +01:00
|
|
|
public static Vector3 operator /(Vector3 vec, Vector3 divisorv)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2020-03-17 23:03:04 +01:00
|
|
|
vec.x /= divisorv.x;
|
|
|
|
vec.y /= divisorv.y;
|
|
|
|
vec.z /= divisorv.z;
|
|
|
|
return vec;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2019-08-05 03:50:28 +02:00
|
|
|
public static Vector3 operator %(Vector3 vec, real_t divisor)
|
|
|
|
{
|
|
|
|
vec.x %= divisor;
|
|
|
|
vec.y %= divisor;
|
|
|
|
vec.z %= divisor;
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Vector3 operator %(Vector3 vec, Vector3 divisorv)
|
|
|
|
{
|
|
|
|
vec.x %= divisorv.x;
|
|
|
|
vec.y %= divisorv.y;
|
|
|
|
vec.z %= divisorv.z;
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
2017-10-02 23:24:00 +02:00
|
|
|
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)
|
|
|
|
{
|
2020-09-09 00:10:58 +02:00
|
|
|
if (left.x == right.x)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2020-09-09 00:10:58 +02:00
|
|
|
if (left.y == right.y)
|
|
|
|
{
|
2017-10-02 23:24:00 +02:00
|
|
|
return left.z < right.z;
|
2020-09-09 00:10:58 +02:00
|
|
|
}
|
2018-04-08 05:28:24 +02:00
|
|
|
return left.y < right.y;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
return left.x < right.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator >(Vector3 left, Vector3 right)
|
|
|
|
{
|
2020-09-09 00:10:58 +02:00
|
|
|
if (left.x == right.x)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2020-09-09 00:10:58 +02:00
|
|
|
if (left.y == right.y)
|
|
|
|
{
|
2017-10-02 23:24:00 +02:00
|
|
|
return left.z > right.z;
|
2020-09-09 00:10:58 +02:00
|
|
|
}
|
2018-04-08 05:28:24 +02:00
|
|
|
return left.y > right.y;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
return left.x > right.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator <=(Vector3 left, Vector3 right)
|
|
|
|
{
|
2020-09-09 00:10:58 +02:00
|
|
|
if (left.x == right.x)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2020-09-09 00:10:58 +02:00
|
|
|
if (left.y == right.y)
|
|
|
|
{
|
2017-10-02 23:24:00 +02:00
|
|
|
return left.z <= right.z;
|
2020-09-09 00:10:58 +02:00
|
|
|
}
|
2018-04-08 05:28:24 +02:00
|
|
|
return left.y < right.y;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
return left.x < right.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator >=(Vector3 left, Vector3 right)
|
|
|
|
{
|
2020-09-09 00:10:58 +02:00
|
|
|
if (left.x == right.x)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2020-09-09 00:10:58 +02:00
|
|
|
if (left.y == right.y)
|
|
|
|
{
|
2017-10-02 23:24:00 +02:00
|
|
|
return left.z >= right.z;
|
2020-09-09 00:10:58 +02:00
|
|
|
}
|
2018-04-08 05:28:24 +02:00
|
|
|
return left.y > right.y;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2019-10-02 22:35:49 +02:00
|
|
|
return x == other.x && y == other.y && z == other.z;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 23:29:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns true if this vector and `other` are approximately equal, by running
|
|
|
|
/// <see cref="Mathf.IsEqualApprox(real_t, real_t)"/> on each component.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="other">The other vector to compare.</param>
|
|
|
|
/// <returns>Whether or not the vectors are approximately equal.</returns>
|
2019-10-14 22:33:45 +02:00
|
|
|
public bool IsEqualApprox(Vector3 other)
|
|
|
|
{
|
|
|
|
return Mathf.IsEqualApprox(x, other.x) && Mathf.IsEqualApprox(y, other.y) && Mathf.IsEqualApprox(z, other.z);
|
|
|
|
}
|
|
|
|
|
2017-10-02 23:24:00 +02:00
|
|
|
public override int GetHashCode()
|
|
|
|
{
|
|
|
|
return y.GetHashCode() ^ x.GetHashCode() ^ z.GetHashCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return String.Format("({0}, {1}, {2})", new object[]
|
|
|
|
{
|
2018-04-08 05:28:24 +02:00
|
|
|
x.ToString(),
|
|
|
|
y.ToString(),
|
|
|
|
z.ToString()
|
2017-10-02 23:24:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public string ToString(string format)
|
|
|
|
{
|
|
|
|
return String.Format("({0}, {1}, {2})", new object[]
|
|
|
|
{
|
2018-04-08 05:28:24 +02:00
|
|
|
x.ToString(format),
|
|
|
|
y.ToString(format),
|
|
|
|
z.ToString(format)
|
2017-10-02 23:24:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|