84 lines
1.8 KiB
C#
84 lines
1.8 KiB
C#
namespace GameCore.Math;
|
|
|
|
/// <summary>
|
|
/// A simple, engine-agnostic 3D vector struct.
|
|
/// We use our own to avoid dependencies on Godot or Unity's vector types
|
|
/// within the GameCore library, ensuring it remains portable.
|
|
/// </summary>
|
|
public struct Vector3
|
|
{
|
|
public float X;
|
|
public float Y;
|
|
public float Z;
|
|
|
|
public static readonly Vector3 Zero = new(0, 0, 0);
|
|
|
|
public Vector3(float x, float y, float z)
|
|
{
|
|
X = x;
|
|
Y = y;
|
|
Z = z;
|
|
}
|
|
|
|
public static Vector3 operator +(Vector3 a, Vector3 b)
|
|
{
|
|
return new Vector3(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
|
|
}
|
|
|
|
public static Vector3 operator -(Vector3 a, Vector3 b)
|
|
{
|
|
return new Vector3(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
|
|
}
|
|
|
|
public static Vector3 operator *(Vector3 a, float d)
|
|
{
|
|
return new Vector3(a.X * d, a.Y * d, a.Z * d);
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"({X}, {Y}, {Z})";
|
|
}
|
|
|
|
public override bool Equals(object obj)
|
|
{
|
|
if (obj is not Vector3 other)
|
|
return false;
|
|
|
|
return X == other.X && Y == other.Y && Z == other.Z;
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
return HashCode.Combine(X, Y, Z);
|
|
}
|
|
|
|
public static bool operator ==(Vector3 a, Vector3 b)
|
|
{
|
|
return a.Equals(b);
|
|
}
|
|
|
|
public static bool operator !=(Vector3 a, Vector3 b)
|
|
{
|
|
return !a.Equals(b);
|
|
}
|
|
|
|
public float Length()
|
|
{
|
|
return (float)System.Math.Sqrt(X * X + Y * Y + Z * Z);
|
|
}
|
|
|
|
public Vector3 Normalize()
|
|
{
|
|
var length = Length();
|
|
if (length == 0)
|
|
return Zero;
|
|
|
|
return new Vector3(X / length, Y / length, Z / length);
|
|
}
|
|
|
|
public static float Dot(Vector3 a, Vector3 b)
|
|
{
|
|
return a.X * b.X + a.Y * b.Y + a.Z * b.Z;
|
|
}
|
|
} |