| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System.Runtime.CompilerServices; |
| | 6 | | using Unity.Mathematics; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace GDX.Mathematics |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// A set of functionality to extend on Unity's rotation based methods. |
| | 13 | | /// </summary> |
| | 14 | | [VisualScriptingCompatible(8)] |
| | 15 | | public static class Rotate |
| | 16 | | { |
| | 17 | | #if !UNITY_DOTSRUNTIME |
| | 18 | | /// <summary> |
| | 19 | | /// Create a quaternion based on a rotation from <paramref name="targetQuaternion" /> to |
| | 20 | | /// <paramref name="otherQuaternion" />. |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="targetQuaternion">The source <see cref="Quaternion" />.</param> |
| | 23 | | /// <param name="otherQuaternion">The destination <see cref="Quaternion" />.</param> |
| | 24 | | /// <param name="rotationRate">How fast should the rotation occur.</param> |
| | 25 | | /// <param name="elapsedTime">How long has elapsed since the rotation started.</param> |
| | 26 | | /// <returns>A rotational value.</returns> |
| | 27 | | /// <exception cref="UnsupportedRuntimeException">Not supported on DOTS Runtime.</exception> |
| | 28 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 29 | | public static Quaternion Towards(Quaternion targetQuaternion, Quaternion otherQuaternion, float rotationRate, |
| | 30 | | float elapsedTime) |
| 0 | 31 | | { |
| 0 | 32 | | float rotatedAmount = rotationRate * elapsedTime; |
| 0 | 33 | | Quaternion rotationDelta = Quaternion.Inverse(targetQuaternion) * otherQuaternion; |
| 0 | 34 | | rotationDelta.ToAngleAxis(out float rotatedAngle, out Vector3 axis); |
| 0 | 35 | | rotatedAngle = math.min(rotatedAngle, rotatedAmount); |
| 0 | 36 | | return targetQuaternion * Quaternion.AngleAxis(rotatedAngle, axis); |
| 0 | 37 | | } |
| | 38 | | #endif // !UNITY_DOTSRUNTIME |
| | 39 | | } |
| | 40 | | } |