Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How to create Joints in 0.50.0

Discussion in 'Physics for ECS' started by Occuros, Mar 23, 2022.

  1. Occuros

    Occuros

    Joined:
    Sep 4, 2018
    Posts:
    305
    The ECS samples for physics haven't been updated just yet, and the documentation is a little sparse when it comes to joints.

    What is currently needed to create a joint (Fixed Joint for example) between two entities?

    I tried to add the component returned by:
    PhysicsJoint.CreateFixed
    but that seems to have no effect.

    A simple example to create a fixed joint would be appreciated.
     
  2. Occuros

    Occuros

    Joined:
    Sep 4, 2018
    Posts:
    305
    I tried the following:


    Code (CSharp):
    1.      
    2. var joint = PhysicsJoint.CreateFixed(new BodyFrame(), new BodyFrame());
    3. var jointEntity = ecb.CreateEntity();
    4. ecb.SetName(jointEntity, $"FixedJoint {e.EntityA.Index} - {e.EntityB.Index}");
    5. ecb.AddComponent(jointEntity, new PhysicsConstrainedBodyPair(e.EntityA, e.EntityB, false));
    6. ecb.AddComponent(jointEntity, joint);
    7.  
    But it seems that these constraints have no effect (although the entity is created.

    I probably am missing something small to make it work properly.
     
  3. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    Code (CSharp):
    1. EntityManager.AddSharedComponentData(jointEntity, new PhysicsWorldIndex(0));
    2. var bodyPair = new PhysicsConstainedBodyPair(bodyA, bodyB, collisionEnabled);
    3. EntityManager.SetComponentData(jointEntity, bodyPair);
    4. var joint = PhysicsJoint.CreateFixed(
    5.                     new RigidTransform(OrientationLocal, PositionLocal),
    6.                     new RigidTransform(OrientationInConnectedEntity, PositionInConnectedEntity)
    7.                 );
    8. EntityManager.SetComponentData(jointEntity, joint);
     
    DevViktoria and Occuros like this.
  4. Occuros

    Occuros

    Joined:
    Sep 4, 2018
    Posts:
    305
    Indeed forgot to set the `PhysicsWorldIndex`, Thank you!
     
  5. JMPM-UNITY

    JMPM-UNITY

    Unity Technologies

    Joined:
    Jun 16, 2021
    Posts:
    94
    NT_Ninetails likes this.