# Connect Physics Core 2D API objects with joints

> Create a connection or constraint between two physics objects.

> **Note:**
>
> This documentation is about writing C# scripts using the `Unity.U2D.Physics` API. To use 2D physics in the Unity Editor using components like the Rigidbody 2D component, refer to [2D physics](/engine/6000.5/manual/unity2d/2d-physics.md.md) instead.

To create a connection or constraint between two Physics Core 2D API objects, create a joint that connects the two physics bodies.

For example, create a fixed joint that welds two bodies together, or a distance joint that keeps them a set distance apart. Use joints to simulate real-world mechanical behaviours like a spring or a hinge.

All joints allow you to set the anchor points on the bodies they connect, and limits on the force and torque they apply. For more information, refer to the [`PhysicsJoint`](/engine/6000.5/script-reference/unity/u2d/physics/physicsjoint.md) API.

Follow these steps:

1. Create two `PhysicsBody` objects. For more information, refer to [Create a physics object](/engine/6000.5/manual/unity2d/2d-physics-api/create-objects/physics-object.md).

2. Create a definition object for the type of joint you want. For example, a `PhysicsDistanceJointDefinition` object. Make it a `public` field so it displays its properties in the Editor window.

   For example:

   ```csharp
   public PhysicsDistanceJointDefinition jointDefinition = new PhysicsDistanceJointDefinition
   {
       // Specify the two bodies to connect
       bodyA = body1,
       bodyB = body2,

       // Set the distance
       distance = 2f
   };
   ```

   A new definition has a set of default values. For more information about definitions and changing the default values, refer to [Configure 2D physics properties using a definition](/engine/6000.5/manual/unity2d/2d-physics-api/properties/definitions.md).

   For the full list of joint types, refer to [`PhysicsJoint.JointType`](/engine/6000.5/script-reference/unity/u2d/physics/physicsjoint/jointtype.md).

3. To create the joint, use the `CreateJoint` method of your world object, and pass in the definition. For example:

   ```csharp
   PhysicsJoint distanceJoint = world.CreateJoint(jointDefinition);
   ```

4. If your script is in a `MonoBehaviour` class attached to a GameObject, adjust the properties in the **Inspector** window.

   To configure the properties in your script instead, set the properties of the definition object before you create the joint. For more information, refer to [Configure 2D physics properties using a definition](/engine/6000.5/manual/unity2d/2d-physics-api/properties/definitions.md).

If you destroy a body, any joints connected to that body are also destroyed. For more information, refer to [Destroy physics objects and manage memory](/engine/6000.5/manual/unity2d/2d-physics-api/create-objects/destroy.md).

## Example

The following example uses a fixed joint to create a fixed point that a large circle swings from.

```csharp
using UnityEngine;
using Unity.U2D.Physics; 

public class CreateJoint : MonoBehaviour
{
    public PhysicsDistanceJointDefinition jointDefinition = new PhysicsDistanceJointDefinition();

    void Awake()
    {
        PhysicsWorld world = PhysicsWorld.defaultWorld;

        // Create a static fixed body
        PhysicsBodyDefinition bodyDefinition1 = new PhysicsBodyDefinition{
            type = PhysicsBody.BodyType.Static,
            position = new Vector2(-5f, 0f),
        };
        PhysicsBody object1 = world.CreateBody(bodyDefinition1);

        // Create a large circle below the fixed body
        PhysicsBodyDefinition bodyDefinition2 = new PhysicsBodyDefinition{
            type = PhysicsBody.BodyType.Dynamic,
            position = new Vector2(5f, 0f),
        };
        PhysicsBody object2 = world.CreateBody(bodyDefinition2);
        object2.CreateShape(new CircleGeometry { radius = 3f });

        // Add the bodies to the joint definition
        jointDefinition.bodyA = object1;
        jointDefinition.bodyB = object2;
        
        // Add the joint to the world
        world.CreateJoint(jointDefinition);        
    }
}
```

## Additional resources

* [Collisions and interactions in the Physics Core 2D API](/engine/6000.5/manual/unity2d/2d-physics-api/interactions.md)
* [PhysicsCore2D repository](https://github.com/Unity-Technologies/PhysicsExamples2D/tree/master) on GitHub
