Search Unity

Constraints (Freeze Position & Rotations)

Discussion in 'Physics for ECS' started by cranky, Mar 27, 2020.

  1. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Hi everyone,

    I'm looking for a way to freeze rotation of a PhysicsBody like we could with Rigidbody's Constraints property. How can this be done with DOTS physics?

    Thank you!
     
  2. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    The PhysicsMass.InverseInertia can be set to float3.zero - i.e. infinite inertia. This is the same as the freeze rotation option. Unfortunately there is no UI for this yet in the Unity Physics package, though if you look at the '2b6. Motion Properties - Inertia Tensor' sample uses a demo script 'Set Inertia Inverse Behavior' that you might find useful.

    If you want a Joint approach rather than manipulating the tensor check out this post.

    We are looking into an adding the equivalent of the Freeze Position and Freeze Rotation options as full class options in the physics material but I don't have a timeframe for that yet.
     
    Orimay, NotaNaN and cranky like this.
  3. Bas-Smit

    Bas-Smit

    Joined:
    Dec 23, 2012
    Posts:
    274
    right now I freeze position by removing physics velocity, which I would like to avoid. Should I be able to set InverseMass to 0? In my project I get NaNs and crashes and when I try it in an empty project with a plane and a sphere my sphere falls through the floor

    Code (CSharp):
    1. public GameObject Prefab;
    2.  
    3. void Start()
    4. {
    5.     var world = World.DefaultGameObjectInjectionWorld;
    6.     var em = world.EntityManager;
    7.     var settings = new GameObjectConversionSettings(
    8.         world,
    9.         GameObjectConversionUtility.ConversionFlags.AssignName,
    10.         world.GetExistingSystem<ConvertToEntitySystem>().BlobAssetStore);
    11.     var prefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(Prefab, settings);
    12.     var e = em.Instantiate(prefab);
    13.     em.SetComponentData(e, new Translation {Value = new float3(10, .5f, 10)});
    14.     {
    15.         var d = em.GetComponentData<PhysicsMass>(e);
    16.         d.InverseMass = 0;
    17.         d.InverseInertia = 0;
    18.         em.SetComponentData(e, d);
    19.     }
    20. }
     
  4. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    Setting
    InverseMass 
    to 0 should be fine. When you create a PhysicsBody as kinematic that is exactly what happens. We certainly shouldn't be crash. I'll try for a repro locally.
     
  5. Bas-Smit

    Bas-Smit

    Joined:
    Dec 23, 2012
    Posts:
    274
    Pretty sure the crashes are the result of the Nan's further down the line. Submitting repro project now
     
  6. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    Just tried this in the latest released UnityPhysicsSamples project. I can reproduce the crash locally. Let me know when you get a repro project.
     
  7. Bas-Smit

    Bas-Smit

    Joined:
    Dec 23, 2012
    Posts:
    274
    (Case 1232765) Unity Physics Setting InverseMass to 0 makes sphere lose collision instead of being frozen. Im guessing this is the same issue as whats causing the crashes in my project, I'll submit that too if it turns out otherwise. If you could share the fix if its minor I'll test it
     
  8. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    This absolutely works and answers my question -- thanks!
     
    steveeHavok likes this.
  9. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    Ah, ok, there might be a slight miss understanding here.
    Infinite Mass and Inertia stop any collisions from imparting impulses to the body i.e. a stoppable force meeting an immovable body. However, if you have already assigned a linear or angular velocity to the body it will continuing moving in the assigned direction or rotating about the assigned axes.
    If you have an infinite inertia but a finite mass the body will receive collision impulses through its center of mass, i.e. it will bounce but with no rotation changes.
    If you have an infinite mass but a finite inertia the body will receive only angular collision impulses and so will pass through other bodies.
    A crash shouldn't happen though.
     
  10. Bas-Smit

    Bas-Smit

    Joined:
    Dec 23, 2012
    Posts:
    274
    Ok, I'll have another look at the crash
     
  11. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    FYI, I checked the sample in your case and didn't get any crash locally!
     
  12. hauwing

    hauwing

    Joined:
    Dec 13, 2017
    Posts:
    69
    hi, from the "2b6. Motion Properties" demo it looks like that setting inertia will lock axis in the world perspective, is it possible to lock the physic body according to his own local axis?
     
  13. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    What are you seeing? Setting the inertia inverse already works in the motion space of the body.
    Note that motion space and body space may be different if you have overridden the default mass distribution and moved the center of mass or motion orientation. The last Cube in 2b6 looks like it is locked in world space because the mass properties are re-oriented.
    upload_2020-6-30_11-19-34.png
     
  14. hauwing

    hauwing

    Joined:
    Dec 13, 2017
    Posts:
    69
    Thanks, will take a look at the orientation.
     
  15. Endlesser

    Endlesser

    Joined:
    Nov 11, 2015
    Posts:
    89
    Works for me exactly:):)