# Configure collisions between Physics Core 2D API objects

> Change which objects collide with which using layers and masks, and enable using 64 layers instead of 32.

> **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.

In the Physics Core 2D API, collisions are enabled by default.

By default the following applies:

* Objects use [GameObject layers](/engine/6000.5/manual/working-with-gameobjects/layers.md), and all objects are on the built-in layer called **Default**.
* Objects collide with objects on all other layers.
* You can use up to 32 layers.

To change this behaviour, do the following:

1. Set the Physics Core 2D API to use its own set of 64 layers, instead of GameObject layers.
2. Configure which layers objects are on, and create layer masks that set which layers an object interacts with.

> **Note:**
>
> To configure how a physics object reacts to collisions, for example how bouncy it is, refer to [Configure Physics Core 2D API scenes with definitions](/engine/6000.5/manual/unity2d/2d-physics-api/properties/definitions.md).

## Set the API to use its own set of layers

Follow these steps:

1. Create and assign a Physics Core Settings 2D asset. For more information, refer to [Configure global 2D physics settings](/engine/6000.5/manual/unity2d/2d-physics-api/properties/class-physics-core-settings2d.md).
2. In the Editor window, select **PhysicsCore 2D** > **Settings**.
3. In the **Global** section, enable **Use Physics Layers**.

The `PhysicsMask` API now uses the layers in the **Layers** section of the **Settings** window.

Use the **Layers** tab to add, edit, or remove layers. For more information, refer to [PhysicsCore 2D reference](/engine/6000.5/manual/unity-editor/editor-settings-reference/comp-manager-group/class-physics-core2dproject-settings.md).

## Change which objects collide in the Editor

Follow these steps:

1. Create the layers you need. For more information, refer to [PhysicsCore 2D reference](/engine/6000.5/manual/unity-editor/editor-settings-reference/comp-manager-group/class-physics-core2dproject-settings.md).

2. [Create a physics object](/engine/6000.5/manual/unity2d/2d-physics-api/create-objects/physics-object.md) with a public `PhysicsShapeDefinition` and attach it to a GameObject.

3. Select the GameObject, then in the Editor window open the **Contact Filter** dropdown.

4. Set **Categories** to the layers you want the object to be on. For example, the first built-in layer called **Default**.

5. Set **Contacts** to the layers you want the object to collide with. For example **Nothing** for no layers.

## Change which objects collide in a script

Follow these steps:

1. Create the layers you need. For more information, refer to [PhysicsCore 2D reference](/engine/6000.5/manual/unity-editor/editor-settings-reference/comp-manager-group/class-physics-core2dproject-settings.md).

2. Create a `PhysicsMask` object with the layer you want the object to be on. For example:

   ```csharp
   PhysicsMask objectLayer = PhysicsLayers.GetLayerMask("Car");
   ```

   `PhysicsLayers.GetLayerMask` gets the layer mask whether you're using GameObject layers or the Physics Core 2D API layers.

3. Create another `PhysicsMask` object with the layers you want the object to collide with.

   ```csharp
   PhysicsMask objectLayer = PhysicsLayers.GetLayerMask("Walls");
   ```

4. Create a `ContactFilter` object with the two physics layer masks. For example:

   ```csharp
   PhysicsShape.ContactFilter myContactFilter = new PhysicsShape.ContactFilter { categories = objectLayer, contacts = contactLayer },
   ```

   You can also use `PhysicsMask.All` to represent all layers, or `PhysicsMask.None` to represent no layers.

5. Assign the `ContactFilter` to the `PhysicsShape` when you create it. For example:

   ```csharp
   PhysicsShapeDefinition shapeDefinition = new PhysicsShapeDefinition{
       contactFilter = myContactFilter
   };
   ```

If `PhysicsMask` is a bitmask rather than a set of layers, add the `PhysicsMask.ShowAsPhysicsMask` attribute. Unity then displays the mask as bit values.

For more information, refer to [`PhysicsMask`](/engine/6000.5/script-reference/unity/u2d/physics/physicsmask.md).

## Prevent objects passing through each other

Because physics bodies move in steps, fast-moving objects can sometimes pass through other objects without activating a collision. This is called tunnelling.

The 2D physics system automatically uses continuous collision detection to prevent tunnelling when dynamic objects approach static objects. However if a very fast-moving object still passes through another object, enable the `fastCollisionsAllowed` property of the physics body to force continuous collision detection. Enabling this setting can reduce performance.

## Example

The following example sets a falling circle to be on the **MyNewLayer** layer and collide only with other **MyNewLayer** layer objects. It passes through the larger circle that's in the default layer.

To use the example:

1. Create a Physics Core Settings 2D asset, enable **Use Full Layers**, and add a layer called **MyNewLayer**.
2. Attach the script to a GameObject in your scene, and enter Play mode.

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

public class ContactFilters : MonoBehaviour
{
    void Start()
    {
        CircleGeometry smallCircleShape = new CircleGeometry{ radius = 0.5f };
        CircleGeometry largeCircleShape = new CircleGeometry{ radius = 3f };

        PhysicsWorld world = PhysicsWorld.defaultWorld;

        // Create a small falling circle
        PhysicsBody object1 = world.CreateBody(new PhysicsBodyDefinition
        { 
            position = new Vector2(0.5f, 8f),
            type = PhysicsBody.BodyType.Dynamic
        });

        // Create a shape on the MyNewLayer layer, which only collides with the MyNewLayer layer
        PhysicsMask objectLayer = PhysicsLayers.GetLayerMask("MyNewLayer");
        PhysicsMask collisionLayer = PhysicsLayers.GetLayerMask("MyNewLayer");
        PhysicsShape.ContactFilter contactFilter = new PhysicsShape.ContactFilter { 
            categories = objectLayer,
            contacts = collisionLayer
        };
        PhysicsShapeDefinition smallCircleDefinition = new PhysicsShapeDefinition{
            contactFilter = contactFilter
        };
        object1.CreateShape(smallCircleShape, smallCircleDefinition);

        // Create a larger static circle below, on the default layer
        PhysicsBody object2 = world.CreateBody(new PhysicsBodyDefinition
        { 
            position = new Vector2(0f, 0f),
            type = PhysicsBody.BodyType.Static
        });
        object2.CreateShape(largeCircleShape);

    }
}
```

## Additional resources

* [Introduction to collision in the Physics Core 2D API](/engine/6000.5/manual/unity2d/2d-physics-api/interactions/introduction.md)
* [Detect collisions between Physics Core 2D API objects](/engine/6000.5/manual/unity2d/2d-physics-api/interactions/collision-handle.md)
* [PhysicsCore2D repository](https://github.com/Unity-Technologies/PhysicsExamples2D/tree/master) on GitHub
* [Body definition reference for the Physics Core 2D API](/engine/6000.5/manual/unity2d/2d-physics-api/reference/body.md)
* [Configure global Physics Core 2D API settings](/engine/6000.5/manual/unity2d/2d-physics-api/properties/class-physics-core-settings2d.md)
