| | 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 UnityEngine; |
| | 6 | |
|
| | 7 | | #if !UNITY_DOTSRUNTIME |
| | 8 | | namespace GDX |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// <see cref="UnityEngine.Rigidbody" /> Based Extension Methods |
| | 12 | | /// </summary> |
| | 13 | | /// <exception cref="UnsupportedRuntimeException">Not supported on DOTS Runtime.</exception> |
| | 14 | | [VisualScriptingCompatible(2)] |
| | 15 | | public static class RigidbodyExtensions |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Get a <see cref="Rigidbody" />'s moment of inertia for a <paramref name="targetAxis" />. |
| | 19 | | /// </summary> |
| | 20 | | /// <remarks> |
| | 21 | | /// Provided <paramref name="targetAxis" /> must not be <see cref="Vector3.zero" />. |
| | 22 | | /// </remarks> |
| | 23 | | /// <param name="targetRigidbody">The <see cref="Rigidbody" /> to evaluate.</param> |
| | 24 | | /// <param name="targetAxis">The axis use to calculate the moment of inertia.</param> |
| | 25 | | /// <returns>The moment of inertia for the <paramref name="targetAxis" />.</returns> |
| | 26 | | public static float MomentOfInertia(this Rigidbody targetRigidbody, Vector3 targetAxis) |
| 0 | 27 | | { |
| | 28 | | // Normalize axis |
| 0 | 29 | | targetAxis = targetAxis.normalized; |
| | 30 | |
|
| 0 | 31 | | return targetAxis.sqrMagnitude == 0f |
| | 32 | | ? float.NaN |
| | 33 | | : Vector3.Scale( |
| | 34 | | Quaternion.Inverse(targetRigidbody.transform.rotation * targetRigidbody.inertiaTensorRotation) * |
| | 35 | | targetAxis, targetRigidbody.inertiaTensor).magnitude; |
| 0 | 36 | | } |
| | 37 | | } |
| | 38 | | } |
| | 39 | | #endif // !UNITY_DOTSRUNTIME |