# Combine Physics Core 2D API shapes

> To combine physics shapes into a compound shape, create a composer using a PhysicsComposer object.

> **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.6/manual/unity2d/2d-physics.md.md) instead.

To combine physics shapes into a compound shape, create a composer using a `PhysicsComposer` object. For example, create a car shape by combining a car body and two wheels.

Follow these steps:

1. At the top of your script, import the `Unity.Collections` library that contains the allocator APIs the composer uses:

   ```csharp
   using Unity.Collections;
   ```

2. Create the `Geometry` shapes you want to combine. For more information, refer to [Create a physics object](/engine/6000.6/manual/unity2d/2d-physics-api/create-objects/physics-object.md).

3. Create a new `PhysicsComposer` object. Pass in an allocator, which creates temporary memory for building up the combined object. For example:

   ```csharp
   PhysicsComposer physicsComposer = PhysicsComposer.Create(Allocator.Temp);
   ```

   For more information about allocators, refer to [Allocator](/engine/6000.6/script-reference/unity/collections/allocator.md).

4. Add each object to the composer as a layer, specifying its geometry, its position, and how to combine the shape with the existing layers. For example:

   ```csharp
   physicsComposer.AddLayer(circleGeometry, PhysicsTransform.identity, PhysicsComposer.Operation.OR);
   ```

   For more information about `Operation`, refer to the [Layer operations](#layer-operations) section.

5. Create the combined shape using `CreatePolygonGeometry`. Pass in a scale, and an allocator that creates the memory to store the combined shape. For example:

   ```csharp
   using NativeArray<PolygonGeometry> combinedShape = composer.CreatePolygonGeometry(new Vector2(1f, 1f), Allocator.Temp);
   ```

   > **Note:**
   >
   > `using` automatically disposes of the memory allocated for the `NativeArray` when it goes out of scope. For more information, refer to [Destroy Physics Core 2D API objects and manage memory](/engine/6000.6/manual/unity2d/2d-physics-api/create-objects/destroy.md).

6. Create a body with the combined shape using the `CreateShapeBatch` method. For example:

   ```csharp
   body.CreateShapeBatch(combinedShape, PhysicsShapeDefinition.defaultDefinition);
   ```

The `PhysicsComposer` API combines `CircleGeometry`, `CapsuleGeometry`, `PolygonGeometry`, `PhysicsShape`, or any closed region defined by a set of `Vector2` points.

## Layer operations

The following `PhysicsComposer.Operation` operations are available:

* `OR`: Adds the shape.
* `AND`: Keeps only the overlapping areas between the new shape and the existing shape.
* `NOT`: Subtracts the new shape from the existing shapes.
* `XOR`: Removes overlapping areas, but keeps non-overlapping areas.

## Example

The following example combines a circle and a capsule shape. Attach this script to a GameObject in a scene, then enter Play mode to check the combined shape.

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

public class CombineShapes : MonoBehaviour
{
    // Create the definition for a circle object
    public CircleGeometry MyCircleGeometry = CircleGeometry.defaultGeometry;
    public PhysicsTransform CircleTransform = new Vector2(0f, 0f);

    // Create the definition for a capsule object
    public CapsuleGeometry MyCapsuleGeometry = CapsuleGeometry.defaultGeometry;
    public PhysicsTransform CapsuleTransform = new Vector2(0.75f, 0f);

    private void Awake()
    {
        PhysicsWorld m_PhysicsWorld = PhysicsWorld.defaultWorld;
        
        // Create a composer to combine shapes
        PhysicsComposer composer = PhysicsComposer.Create(Allocator.Temp);
        
        // Add both shapes to the composer
        composer.AddLayer(MyCircleGeometry, CircleTransform);
        composer.AddLayer(MyCapsuleGeometry, CapsuleTransform, PhysicsComposer.Operation.OR);

        // Combine the shapes
        using NativeArray<PolygonGeometry> combinedShape = composer.CreatePolygonGeometry(new Vector2(1f, 1f), Allocator.Temp);
        
        // Create a body with the combined shapes
        PhysicsBody body = m_PhysicsWorld.CreateBody();       
        body.CreateShapeBatch(combinedShape, PhysicsShapeDefinition.defaultDefinition);

        // Destroy the composer to also destroy the temporary allocator.
        composer.Destroy();
    }   
}
```

## Additional resources

* [Connect Physics Core 2D API objects with joints](/engine/6000.6/manual/unity2d/2d-physics-api/create-objects/joints.md)
* [PhysicsCore2D repository](https://github.com/Unity-Technologies/PhysicsExamples2D/tree/master) on GitHub.
