Search Unity

Question Unity DOTS Physics constraint linear and angular x,y,z

Discussion in 'Physics for ECS' started by QuebecDev, Dec 19, 2021.

  1. QuebecDev

    QuebecDev

    Joined:
    May 9, 2020
    Posts:
    37
    Hi,

    I am trying to constraint my entity physic as per MonoBehaviour Rigidbody.
    190120-capture.png

    I have found some posts that helped me, but I cant make this work. Here is what I have done so far...

    This is a component that contains bool3 for the constraint:
    Code (CSharp):
    1.  
    2. [GenerateAuthoringComponent]
    3. public struct PhysicsConstraintComponent : IComponentData
    4. {
    5.      public bool3 linearConstrains;
    6.      public bool3 angularConstrains;
    7. }
    This is the system that creates the PhysicJoint:
    Code (CSharp):
    1.          
    2. Entities
    3.          .ForEach((Entity entity, in PhysicsConstraintComponent constraint, in Translation translation, in Rotation rotation) => {
    4.              var rigidTransform = new RigidTransform( rotation.Value, translation.Value);
    5.              var body = new BodyFrame(rigidTransform);
    6.              PhysicsJoint joint = new PhysicsJoint
    7.              {
    8.                  BodyAFromJoint = body,
    9.              };
    10.              FixedList128<Constraint> constraints = new FixedList128<Constraint> {
    11.                  new Constraint
    12.                  {
    13.                      ConstrainedAxes = constraint.linearConstrains,
    14.                      Type = ConstraintType.Linear,
    15.                      Min = 0,
    16.                      Max = 0,
    17.                      SpringFrequency = Constraint.DefaultSpringFrequency,
    18.                      SpringDamping = Constraint.DefaultSpringDamping
    19.                  },
    20.                  new Constraint
    21.                  {
    22.                      ConstrainedAxes = constraint.angularConstrains,
    23.                      Type = ConstraintType.Angular,
    24.                      Min = 0,
    25.                      Max = 0,
    26.                      SpringFrequency = Constraint.DefaultSpringFrequency,
    27.                      SpringDamping = Constraint.DefaultSpringDamping
    28.                  }
    29.              };
    30.              joint.SetConstraints(constraints);
    31.              ecb.AddComponent(entity, joint);
    32.              ecb.RemoveComponent<PhysicsConstraintComponent>(entity);
    33.          }).WithoutBurst().Run();
    My entity is receiving the PhysicJoint but it ignores its constraints, the entity falls on the ground and rotates x,y,z. What am I doing wrong?

    190121-capture1.png
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    This is the wrong forum. The DOTS Physics forum is here. I'll move your post for you.
     
  3. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    Your entity also needs a "PhysicsConstrainedBodyPair" component. BodyA should be set to the entity itself, and Body B should be set to Entity.Null to constrain the entity to world space.
     
    steveeHavok likes this.
  4. QuebecDev

    QuebecDev

    Joined:
    May 9, 2020
    Posts:
    37
    Thanks!