How do you create a physics joint?

I’m new to Dots and have been trying to create a fixed joint for about 2 weeks now and nothing that I have tried seems to work. This is the code I have so far.

‘’’
Entity wheelEntity = spinQuery.GetSingletonEntity();
Entity anchorEntity = anchorQuery.GetSingletonEntity();

        // Create a Fixed joint between entityA and entityB
        RigidTransform jointFrameA = new RigidTransform(quaternion.identity, float3.zero);
        RigidTransform jointFrameB = new RigidTransform(quaternion.identity, float3.zero);

        PhysicsJoint fixedJoint = PhysicsJoint.CreateFixed(jointFrameA, jointFrameB);

        fixedJoint.SetImpulseEventThresholdAllConstraints(80f);

        // Create the joint entity
        Entity jointEntity = state.EntityManager.CreateEntity();

        // Add the constrained body pair (linking entity A and B)
        state.EntityManager.AddComponentData(jointEntity, new PhysicsConstrainedBodyPair(wheelEntity, anchorEntity, false));

        // Add the PhysicsJoint to the joint entity
        state.EntityManager.AddComponentData(jointEntity, fixedJoint);

‘’’

I have 2 entities that I have used the baker method to convert them from gameobjects to entities. Then Im trying to add a joint to them at runtime. Currently the wheel entity that has the physics mass component falls to the ground and does not get held by the anchor with a joint. Can anyone see what I am doing wrong. Any help would be greatly appreciated.

You almost got it! You also need to add the PhysicsWorldIndex component to the joint entity.

Also, for joint creation code, have a look at the PhysicsSamples:

In most of the joint demo scenes, we use the Custom Physics Authoring components for joint creation. The bakers of the corresponding joint authoring components contain all the underlying Unity Physics joint creation code you would need.

A huge thank you for that. I finally got it to work by using this code

Entity wheelEntity = spinQuery.GetSingletonEntity();
Entity anchorEntity = anchorQuery.GetSingletonEntity();

// Create a Fixed joint between entityA and entityB
        RigidTransform jointFrameA = new RigidTransform(quaternion.identity, float3.zero);
        RigidTransform jointFrameB = new RigidTransform(quaternion.identity, float3.zero);

        PhysicsJoint fixedJoint = PhysicsJoint.CreateFixed(jointFrameA, jointFrameB);

        fixedJoint.SetImpulseEventThresholdAllConstraints(80f);

        // Create the joint entity
        Entity jointEntity = state.EntityManager.CreateEntity();

        // Add the constrained body pair (linking entity A and B)
        state.EntityManager.AddComponentData(jointEntity, new PhysicsConstrainedBodyPair(wheelEntity, anchorEntity, false));

        // Add the PhysicsJoint to the joint entity
        state.EntityManager.AddComponentData(jointEntity, fixedJoint);

        state.EntityManager.AddSharedComponent(jointEntity, new PhysicsWorldIndex(0));

I still have a lot to learn with DOTS which is exciting, I hope this code can help someone else who has been struggling to create a joint.

Brilliant! I am glad to hear that.

Have fun learning, and don’t hesitate to post new questions in the forum. :slightly_smiling_face: