Engine_silk.NET/Nebulix/Vector.cs
Daniel 3912f2bd9a
All checks were successful
Gitea Actions Demo / Scan the project (push) Successful in 21s
Added IcoSphere for testing. Replaced Vector3D<T> with Vector3 since it is more performant
2024-02-04 22:53:58 +01:00

35 lines
1.3 KiB
C#

using System.Numerics;
namespace Nebulix;
public static class VectorExtensions
{
public static Vector3 Up(this Vector3 _) => Vector3.UnitY;
public static Vector3 Down(this Vector3 _) => -Vector3.UnitY;
public static Vector3 Left(this Vector3 _) => Vector3.UnitX;
public static Vector3 Right(this Vector3 _) => -Vector3.UnitX;
public static Vector3 Front(this Vector3 _) => -Vector3.UnitZ;
public static Vector3 Back(this Vector3 _) => -Vector3.UnitZ;
// Thanks to: https://stackoverflow.com/a/67920029
public static Vector3 Slerp(this Vector3 start, Vector3 end, float percent)
{
// the cosine of the angle between 2 vectors.
float dot = Vector3.Dot(start, end);
// Clamp it to be in the range of Acos()
// This may be unnecessary, but floating point precision can be a fickle mistress.
Math.Clamp(dot, -1.0f, 1.0f);
// Acos(dot) returns the angle between start and end,
// And multiplying that by percent returns the angle between start and the final result.
float theta = (float)Math.Acos(dot) * percent;
Vector3 relativeVec = end - start * dot;
relativeVec = Vector3.Normalize(relativeVec);
// Orthonormal basis
// The final result.
return ((start * (float)Math.Cos(theta)) + (relativeVec * (float)Math.Sin(theta)));
}
}