1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-10 02:25:41 +00:00

Add Quaternion.Multiply(Vector3)

Functions as an equivalent to Unity's Quaternion*Vector3 operator
This commit is contained in:
Oliver Booth 2022-06-01 18:34:12 +01:00
parent ae82b92f23
commit 2b8f763184
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
2 changed files with 42 additions and 0 deletions

View File

@ -12,6 +12,7 @@
- X10D: Added `Size.ToPoint()`
- X10D: Added `Size.ToPointF()`
- X10D: Added `Size.ToVector2()`
- X10D: Added `Quaternion.Multiply(Vector3)` - this functions as an equivalent to Unity's `Quaternion * Vector3` operator
- X10D: Added `Vector2.Deconstruct()`
- X10D: Added `Vector2.ToPointF()`
- X10D: Added `Vector2.ToSizeF()`

View File

@ -0,0 +1,41 @@
using System.Numerics;
namespace X10D.Numerics;
/// <summary>
/// Numeric-related extension methods for <see cref="Quaternion" />.
/// </summary>
public static class QuaternionExtensions
{
/// <summary>
/// Rotates the specified point with the specified rotation.
/// </summary>
/// <param name="rotation">The rotation.</param>
/// <param name="point">The point.</param>
/// <returns>The rotated point.</returns>
public static Vector3 Multiply(this in Quaternion rotation, in Vector3 point)
{
// the internet wrote it, I just handed it in.
// https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/Math/Quaternion.cs
float x = rotation.X * 2.0f;
float y = rotation.Y * 2.0f;
float z = rotation.Z * 2.0f;
float xx = rotation.X * x;
float yy = rotation.Y * y;
float zz = rotation.Z * z;
float xy = rotation.X * y;
float xz = rotation.X * z;
float yz = rotation.Y * z;
float wx = rotation.W * x;
float wy = rotation.W * y;
float wz = rotation.W * z;
(float px, float py, float pz) = point;
return new Vector3(
(1.0f - (yy + zz)) * px + (xy - wz) * py + (xz + wy) * pz,
(xy + wz) * px + (1.0f - (xx + zz)) * py + (yz - wx) * pz,
(xz - wy) * px + (yz + wx) * py + (1.0f - (xx + yy)) * pz
);
}
}