diff --git a/src/Angle.cs b/src/Angle.cs index ca03a50..c3dd711 100644 --- a/src/Angle.cs +++ b/src/Angle.cs @@ -12,8 +12,8 @@ namespace LinearAlgebra // public static float Rad2Deg = 360.0f / ((float)Math.PI * 2); // public static float Deg2Rad = ((float)Math.PI * 2) / 360.0f; - public const float Deg2Rad = 360.0f / ((float)Math.PI * 2); //0.0174532924F; - public const float Rad2Deg = ((float)Math.PI * 2) / 360.0f; //57.29578F; + public const float Rad2Deg = 360.0f / ((float)Math.PI * 2); //0.0174532924F; + public const float Deg2Rad = ((float)Math.PI * 2) / 360.0f; //57.29578F; /// /// Clamp the angle between the given min and max values diff --git a/src/Direction.cs b/src/Direction.cs index 258e27e..d16ac72 100644 --- a/src/Direction.cs +++ b/src/Direction.cs @@ -1,3 +1,8 @@ +using System; +#if UNITY_5_3_OR_NEWER +using Vector3Float = UnityEngine.Vector3; +#endif + namespace LinearAlgebra { @@ -32,6 +37,23 @@ namespace LinearAlgebra this.horizontal += 180; this.vertical = 180 - this.vertical; } + } + + public Vector3Float ToVector3() + { + float verticalRad = (Angle.pi / 2) - this.vertical * Angle.Deg2Rad; + float horizontalRad = this.horizontal * Angle.Deg2Rad; + float cosVertical = (float)Math.Cos(verticalRad); + float sinVertical = (float)Math.Sin(verticalRad); + float cosHorizontal = (float)Math.Cos(horizontalRad); + float sinHorizontal = (float)Math.Sin(horizontalRad); + + float x = sinVertical * sinHorizontal; + float y = cosVertical; + float z = sinVertical * cosHorizontal; + + Vector3Float v = new(x, y, z); + return v; } } diff --git a/src/SwingTwist.cs b/src/SwingTwist.cs index a7a73da..acf8978 100644 --- a/src/SwingTwist.cs +++ b/src/SwingTwist.cs @@ -1,3 +1,8 @@ +using System.Numerics; +#if UNITY_5_3_OR_NEWER +using Quaternion = UnityEngine.Quaternion; +#endif + namespace LinearAlgebra { @@ -27,6 +32,15 @@ namespace LinearAlgebra SwingTwist r = new SwingTwist(up, right, forward); return r; } + +#if UNITY_5_3_OR_NEWER + public Quaternion ToQuaternion() { + Quaternion q = Quaternion.Euler(-this.swing.vertical, + this.swing.horizontal, + this.twist); + return q; + } +#endif } } \ No newline at end of file diff --git a/test/AngleTest.cs b/test/AngleTest.cs index 75522f3..34b107a 100644 --- a/test/AngleTest.cs +++ b/test/AngleTest.cs @@ -1,3 +1,4 @@ +#if !UNITY_5_6_OR_NEWER using NUnit.Framework; namespace LinearAlgebra.Test @@ -166,4 +167,5 @@ namespace LinearAlgebra.Test } } -} \ No newline at end of file +} +#endif \ No newline at end of file