Created
May 28, 2021 12:24
-
-
Save Gustorvo/9afee8d074422e5917c8acbee6707618 to your computer and use it in GitHub Desktop.
Some of the Quaternion Extensions for Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public static class QuaternionExtensions | |
{ | |
/// <summary> | |
/// Converts rotation into its euler angles representaion (in degrees) in range [-180, 180]. | |
/// </summary> | |
/// <param name="q"></param> | |
/// <returns></returns> | |
public static Vector3 ToEulerAnglesInRange180(this Quaternion q) | |
{ | |
return new Vector3() | |
{ | |
x = WrapAngle(q.eulerAngles.x), | |
y = WrapAngle(q.eulerAngles.y), | |
z = WrapAngle(q.eulerAngles.z) | |
}; | |
float WrapAngle(float angle) | |
{ | |
angle %= 360; | |
return angle > 180 ? angle - 360 : angle; | |
} | |
} | |
public static Quaternion ShortestRotation(Quaternion a, Quaternion b) | |
{ | |
if (Quaternion.Dot(a, b) < 0) | |
{ | |
return a * Quaternion.Inverse(Multiply(b, -1)); | |
} | |
else return a * Quaternion.Inverse(b); | |
} | |
public static Quaternion Multiply(Quaternion input, float scalar) | |
{ | |
return new Quaternion(input.x * scalar, input.y * scalar, input.z * scalar, input.w * scalar); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment