Created
February 7, 2023 14:06
-
-
Save khyperia/018158a2af626562a9c13eff90c8b9e8 to your computer and use it in GitHub Desktop.
Construct a matrix from unity's inertiaTensor and inertiaTensorRotation fields
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
// Returns the INVERSE inertia tensor in GLOBAL space (not local!) | |
// Returns a Matrix3x3, but there's no type for that, so waste some space with a Matrix4x4 | |
// Equivalent to GetInertiaTensorGlobal(rb).inverse, but faster | |
private static Matrix4x4 GetInverseInertiaTensorGlobal(Rigidbody rb) { | |
Vector3 inertiaTensor = rb.inertiaTensor; | |
Vector3 inverseInertiaTensor = new(1 / inertiaTensor.x, 1 / inertiaTensor.y, 1 / inertiaTensor.z); | |
Matrix4x4 rotation = Matrix4x4.Rotate(rb.rotation * rb.inertiaTensorRotation); | |
// rotation is orthonormal, so inverse=transpose | |
return rotation * Matrix4x4.Scale(inverseInertiaTensor) * rotation.transpose; | |
} | |
// -- some other similar helpers -- | |
private static Matrix4x4 GetInverseInertiaTensorLocal(Rigidbody rb) { | |
Vector3 inertiaTensor = rb.inertiaTensor; | |
Vector3 inverseInertiaTensor = new(1 / inertiaTensor.x, 1 / inertiaTensor.y, 1 / inertiaTensor.z); | |
Matrix4x4 rotation = Matrix4x4.Rotate(rb.inertiaTensorRotation); | |
return rotation * Matrix4x4.Scale(inverseInertiaTensor) * rotation.transpose; | |
} | |
private static Matrix4x4 GetInertiaTensorGlobal(Rigidbody rb) { | |
Matrix4x4 rotation = Matrix4x4.Rotate(rb.rotation * rb.inertiaTensorRotation); | |
return rotation * Matrix4x4.Scale(rb.inertiaTensor) * rotation.transpose; | |
} | |
private static Matrix4x4 GetInertiaTensorLocal(Rigidbody rb) { | |
Matrix4x4 rotation = Matrix4x4.Rotate(rb.inertiaTensorRotation); | |
return rotation * Matrix4x4.Scale(rb.inertiaTensor) * rotation.transpose; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment