fix: Quaternion.RotateVector method not working properly on some angles fixed
This commit is contained in:
@@ -187,8 +187,12 @@ public readonly struct Quaternion(float x, float y, float z, float w) : IEquatab
|
||||
/// <returns>The rotated <see cref="Vector3D"/>.</returns>
|
||||
public static Vector3D RotateVector(Vector3D vector, Quaternion quaternion)
|
||||
{
|
||||
Quaternion rotation = quaternion * new Quaternion(vector.X, vector.Y, vector.Z, 0) * Invert(quaternion);
|
||||
return new(rotation.X, rotation.Y, rotation.Z);
|
||||
// https://blog.molecular-matters.com/2013/05/24/a-faster-quaternion-vector-multiplication/
|
||||
// t = 2 * cross(q.xyz, v)
|
||||
// v' = v + q.w * t + cross(q.xyz, t)
|
||||
Vector3D quaternionVector = new(quaternion.X, quaternion.Y, quaternion.Z);
|
||||
Vector3D t = 2f * quaternionVector.Cross(vector);
|
||||
return vector + quaternion.W * t + quaternionVector.Cross(t);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
Reference in New Issue
Block a user