Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

best approach to Lock Angular Axes (X and Z) on Physics Entities

Discussion in 'Physics for ECS' started by Opeth001, Oct 8, 2019.

  1. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    986
    Hello Everyone,

    im looking for a good way to Lock X and Z Angular Axes for some Dynamic bodies (Pure Enities). (Characters)
    why does the Unity PhysicsBody do not support it like the Old Rogidbody ?

    Thank you.
     
  2. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    244
    In the Physics Mass IComponentData, there's a float3 field called InverseInertia. Set the X and Z components of that field to 0 to lock rotation around those axes.
     
    Opeth001 likes this.
  3. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    244
    AFAIK Unity Physics doesn't have an authoring component to set up rotation locking, so I made one myself:
    Code (CSharp):
    1. [DisallowMultipleComponent, RequiresEntityConversion, RequireComponent(typeof(PhysicsBodyAuthoring))]
    2. public class FreezeRotationAuthoring : MonoBehaviour
    3. {
    4.     public bool3 Flags;
    5. }
    6.  
    7. [UpdateAfter(typeof(PhysicsBodyConversionSystem))]
    8. public class FreezeRotationConversionSystem : GameObjectConversionSystem
    9. {
    10.     protected override void OnUpdate()
    11.     {
    12.         Entities.ForEach((FreezeRotationAuthoring freezeRotation) =>
    13.         {
    14.             Entity entity = GetPrimaryEntity(freezeRotation.gameObject);
    15.             PhysicsMass mass = DstEntityManager.GetComponentData<PhysicsMass>(entity);
    16.             if (freezeRotation.Flags.x)
    17.                 mass.InverseInertia.x = 0f;
    18.             if (freezeRotation.Flags.y)
    19.                 mass.InverseInertia.y = 0f;
    20.             if (freezeRotation.Flags.z)
    21.                 mass.InverseInertia.z = 0f;
    22.             DstEntityManager.SetComponentData(entity, mass);
    23.         });
    24.     }
    25. }
     
    Opeth001 likes this.
  4. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    986
    Thank you!
     
  5. starikcetin

    starikcetin

    Joined:
    Dec 7, 2017
    Posts:
    323
    What does
    InverseInertia
    do? Is it
    1 / Inertia
    ?
     
  6. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    244
    @starikcetin Pretty much. It's common for physics engines to opt for inverse values for mass and inertia for performance.
     
    Opeth001 and starikcetin like this.
  7. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    244
    Upon further inspection, this is not the case. If you check the "Override Default Mass Distribution" toggle in PhysicsBodyAuthoring, a float3 field called "InertiaTensor" will be visible. You can lock rotation around an axis by setting the corresponding component of that field to Infinity.