Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Baking the inertia tensor for Rigidbody

Discussion in 'Physics for ECS' started by bl4cksun, Aug 5, 2023.

  1. bl4cksun

    bl4cksun

    Joined:
    Aug 15, 2015
    Posts:
    6
    Unity Physics Version 1.0.11:

    When using the 'built-in' Rigidbody with Colliders instead of 'custom' PhysicsBody with PhysicsShape, the inertia tensor does not seem to be baked into the physics mass component correctly.

    Following screenshot displays PhysicsBody with two PhysicsShapes (LHS) and Rigidbody with two BoxColliders (RHS). Only for the PhysicsBody the inertia tensor (mass properties in purple) seems to be baked correctly.

    upload_2023-8-5_11-10-29.png

    I would expect the same outcome here. Am I missing something or is this not implemented yet?

    My current Workaround:

    Code (CSharp):
    1. [TemporaryBakingType]
    2. public struct RigidbodyTemporaryBakingData : IComponentData {
    3.  
    4.     public float3 inertiaTensor;
    5.     public quaternion inertiaTensorRotation;
    6.  
    7.     public static RigidbodyTemporaryBakingData From (Rigidbody rigidbody) {
    8.         return new () {
    9.             inertiaTensor = rigidbody.inertiaTensor,
    10.             inertiaTensorRotation = rigidbody.inertiaTensorRotation
    11.         };
    12.     }
    13.  
    14. }
    15.  
    16. [WorldSystemFilter(WorldSystemFilterFlags.BakingSystem)]
    17. [UpdateAfter(typeof(PhysicsBodyBakingSystem))]
    18. [RequireMatchingQueriesForUpdate]
    19. public partial struct RigidbodyTemporaryBakingSystem : ISystem {
    20.  
    21.    public readonly void OnUpdate(ref SystemState state) {
    22.         foreach (var(physicsMassRef, temporaryBakingData) in SystemAPI.Query<RefRW<PhysicsMass>, RigidbodyTemporaryBakingData>()) {
    23.             physicsMassRef.ValueRW.InverseInertia = 1f / temporaryBakingData.inertiaTensor;
    24.             physicsMassRef.ValueRW.InertiaOrientation = temporaryBakingData.inertiaTensorRotation;
    25.         }
    26.     }
    27.  
    28. }
    and in the Baker:

    Code (CSharp):
    1. AddComponent(entity, RigidbodyTemporaryBakingData.From(rigidbody));
     
    Last edited: Aug 5, 2023
  2. bl4cksun

    bl4cksun

    Joined:
    Aug 15, 2015
    Posts:
    6