Skip to content

Commit

Permalink
Improved ToEulerAngles
Browse files Browse the repository at this point in the history
Use const of Rad2Deg instead of calculating it every time.
  • Loading branch information
realQuartzi committed Jun 7, 2024
1 parent 993c611 commit 36b68cc
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions Influence.Core/Math/Vectors/Quaternion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public static Vector3 ToEulerAngles(Quaternion quaternion)
{
float sinr_cosp = 2f * (quaternion.x * quaternion.w + quaternion.y * quaternion.z);
float cosr_cosp = 1f - 2f * (quaternion.x * quaternion.x + quaternion.y * quaternion.y);
float roll = Trigq.Atan2(sinr_cosp, cosr_cosp) * 180f / Mathq.Pi;
float roll = Trigq.Atan2(sinr_cosp, cosr_cosp) * Mathq.Rad2Deg;

float sinp = 2f * (quaternion.y * quaternion.w - quaternion.x * quaternion.z);
float pitch;
Expand All @@ -346,22 +346,14 @@ public static Vector3 ToEulerAngles(Quaternion quaternion)
}
else
{
pitch = Trigq.Asin(sinp) * 180f / Mathq.Pi;
pitch = Trigq.Asin(sinp) * Mathq.Rad2Deg;
}

float siny_cosp = 2f * (quaternion.z * quaternion.w + quaternion.x * quaternion.y);
float cosy_cosp = 1f - 2f * (quaternion.y * quaternion.y + quaternion.z * quaternion.z);
float yaw = Trigq.Atan2(siny_cosp, cosy_cosp) * 180f/ Mathq.Pi;
float yaw = Trigq.Atan2(siny_cosp, cosy_cosp) * Mathq.Rad2Deg;

return new Vector3(pitch, yaw, roll);

// Old Implementation... doesnt have any near 90 checks.
/*
return new Vector3(
Trigq.Atan2(quaternion.y, quaternion.w) * Mathq.Rad2Deg,
Trigq.Acos(quaternion.x) * Mathq.Rad2Deg,
Trigq.Atan2(quaternion.z, quaternion.w) * Mathq.Rad2Deg);
*/
}
public Vector3 ToEuler() => ToEulerAngles(this);

Expand Down

0 comments on commit 36b68cc

Please sign in to comment.