Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Creating and destroing physics joints during runtime.

Discussion in 'Physics for ECS' started by l_Lobsternator, Jan 13, 2021.

  1. l_Lobsternator

    l_Lobsternator

    Joined:
    Aug 31, 2019
    Posts:
    12
    Recently I've been working on a simulation where I need to be able to dynamically create and destroy joints between multiple objects during runtime. I've looked around and I've tried to find a way to create joints in this way. However, the only thing I've come across is the physics examples by unity, which use an authoring component and the GameObjectConversionSystem, which as far as I can tell, only runs at the start of the simulation, and has to at that as that is the only time when the DstEntityManager is available, which is used by the EndJointConversionSystem in the CreateJointEntities method. So I'm a little bit stumped, I'm not really sure how else to create the joints other than what is shown in those examples. Any sort of help or advice would be greatly appreciated.

    I'm using unity version 2020.2.1f1.
     
    Iron-Warrior likes this.
  2. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Basically, in order to create a joint you need to create an Entity with PhysicsConstrainedBodyPair and PhysicsJoint components, so it can be done at runtime. PhysicsJoint.Create*() and SceneCreationSystem<T>.CreateJoint() can help with that. You can find some examples in UnityPhysicsSamples\Assets\Demos\4. Joints\Scripts\RagdollDemo.cs, RagdollDemoSystem.CreateRagdoll() .

    Side note about joint lifecycle: If you delete a body Entity that takes part in a joint, you should make sure to delete the joint Entity as well (it won't happen automatically).

    This should help you get started, but please let me know if you need any further help down the line.
     
    l_Lobsternator and Sab_Rango like this.
  3. l_Lobsternator

    l_Lobsternator

    Joined:
    Aug 31, 2019
    Posts:
    12
    Ohhh, okay thank you that makes a lot more sense.
     
  4. xacce

    xacce

    Joined:
    Nov 10, 2013
    Posts:
    6
    For people from the future. How it should actually be done and what to consider, thanks to unity for the documentation (no, go get some work done)
    PhysicsJoint, PhysicsConstrainedBodyPair are necessary components. but you also need the Shared component PhysicsWorldIndex. It is likely that PhysicsVelocity /Graivity/Damping are also needed (I have them by default) on the entities to be linked.
    And a working example, may help someone. Because BodyFrameA-B are not starting positions, oh no.


    Code (CSharp):
    1.   var leftParentRt = new RigidTransform(leftParentLtw.Rotation, leftParentLtw.Position);
    2.                     var rightRightRt = new RigidTransform(rightTransform.Rotation, rightTransform.Position);
    3.                     RigidTransform bFromA = math.mul(math.inverse(rightRightRt), leftParentRt);
    4.                     var kekwait = math.mul(bFromA.rot, quaternion.identity);
    5.                     var kekwait2 = math.transform(bFromA, float3.zero);
    6.                     var joint = PhysicsJoint.CreateFixed(
    7.                         // new BodyFrame(new RigidTransform(leftParentLtw.Rotation, leftParentLtw.Position)),
    8.                         new BodyFrame(new RigidTransform()),
    9.                         new BodyFrame(new RigidTransform(bFromA.rot, kekwait2))
    10.                     );
    11.                     joint.SetImpulseEventThresholdAllConstraints(new float3(1f, 1f, 1f));
     
    unity_1stmanleader likes this.