---
title: "Load Tiled map collision shapes in C#"
url: https://recursiveai.org/onephysics/cookbook/import-tiled-map-colliders/
description: "Load Tiled map collision shapes into a 2D physics world in C#. Import a TMJ object layer, create its colliders and drop a ball onto the imported ground."
version: "1.3.0"
---

# Load Tiled map collision shapes in C#

**OnePhysics** is a free, open-source physics library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePhysics.Interchange) · [GitHub](https://github.com/recursive-ai-oss/onephysics-csharp) · [Docs](https://recursiveai.org/onephysics/docs/) · [Download example (.zip)](https://recursiveai.org/onephysics/cookbook/import-tiled-map-colliders/import-tiled-map-colliders.zip)

Use a fixed-size map with square tiles in straight rows and columns, not an infinite, isometric, or hex map. The download includes `level.tmj`.

```shell
dotnet add package RecursiveAI.OnePhysics.Interchange
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OnePhysics.Interchange;
using RecursiveAI.OnePhysics.Physics2D;

var text = File.ReadAllText("level.tmj");
var map = TiledImporter.Import(text);
var world = new PhysicsWorld2D();
var level = map.Apply(world);
var count = level.ObjectBodies.Length;
Console.WriteLine($"Colliders: {count}");
```

Unzip [the example](https://recursiveai.org/onephysics/cookbook/import-tiled-map-colliders/import-tiled-map-colliders.zip), then `dotnet run`.

The snippet creates one static rectangle from the map:

```text
Colliders: 1
```

## The full program

`level.tmj` is in the download:

```json
{
  "type": "map", "orientation": "orthogonal",
  "infinite": false, "width": 8, "height": 6,
  "tilewidth": 16, "tileheight": 16,
  "layers": [{
    "id": 1, "name": "Collision", "type": "objectgroup",
    "objects": [{
      "id": 1, "name": "Ground", "rotation": 0,
      "x": 0, "y": 80, "width": 128, "height": 16
    }]
  }],
  "tilesets": []
}
```

```csharp
using RecursiveAI.OneMath;
using RecursiveAI.OnePhysics.Interchange;
using RecursiveAI.OnePhysics.Physics2D;

var text = File.ReadAllText("level.tmj");
var map = TiledImporter.Import(text);
var world = new PhysicsWorld2D();
var level = map.Apply(world);
Console.WriteLine($"Colliders: {level.ObjectBodies.Length}");
var ball = world.AddCircle(0.25f, at: new Vector2D(4, 3));
for (int frame = 0; frame < 120; frame++)
    world.Step(1f / 60f);
Console.WriteLine(FormattableString.Invariant($"Ball center: y={ball.Position.Y:F2} m"));
```

`dotnet run` prints:

```text
Colliders: 1
Ball center: y=1.25 m
```

Interchange brings in `RecursiveAI.OneMath` through `RecursiveAI.OnePhysics`, so there’s no separate install for `Vector2D`. The full program drops a ball onto the imported ground. The ground’s top is at y = 1 m; the center of the ball settles about one radius above it.

An invalid or unsupported map, such as an isometric map, throws `InterchangeException` during import. If a valid map gives you no colliders, check the layer names and properties before calling `Apply`.

## Variations

**Change the scale.** Replace the import line with these lines to make 32 map pixels equal one meter:

```csharp
var map = TiledImporter.Import(text,
    new MapOptions { PixelsPerMeter = 32 });
```

After applying the map, `Console.WriteLine($"Map width: {map.Size.X} m");` prints:

```text
Map width: 4 m
```

## Related recipes

- [Build a 2D physics world](https://recursiveai.org/onephysics/cookbook/2d-physics/)
- [Make a one-way platform](https://recursiveai.org/onephysics/cookbook/make-one-way-platform/)

## The short answer

Pass the contents of a Tiled `.tmj` or `.tmx` file to `TiledImporter.Import`, then pass your `PhysicsWorld2D` to `map.Apply`. The sample uses a rectangle on an object layer called `Collision`. `Apply` creates the bodies; importing alone just returns definitions, points and warnings. Neither step draws the map.

## How do you add collision shapes to a Tiled map?

For a floor or wall drawn independently of the tiles, add an object layer called `Collision` and draw rectangles on it. OnePhysics also recognizes `Collisions`, a boolean custom layer property `collision`, or an explicit name supplied through `MapOptions.CollisionLayers`. The names are matched without regard to case. A decorative tile layer with none of those markers does not create colliders.

For a tile-based floor, mark the tile layer and define the shapes in Tiled’s [Tile Collision Editor](https://doc.mapeditor.org/en/stable/manual/editing-tilesets/#tile-collision-editor). OnePhysics recognizes a full-tile rectangle, a bottom-half rectangle and the two simple right-triangle slopes. A tile in a collision layer with no collision shape group counts as solid; an empty shape group or the custom string property `kind` set to `none` gives no collider. If the map points to an external tileset, provide its text through `MapOptions.Tilesets`. If it’s unavailable, OnePhysics treats its tiles as solid and reports `tileset-missing`, which matters when you expected slopes or gaps.

## How do Tiled pixel coordinates translate to physics coordinates?

Tiled’s y coordinate grows downward. The importer puts the map’s bottom left at the world origin by default and flips y so it grows upward. At 16 pixels per meter, the sample ground rectangle’s top is `(96 − 80) / 16 = 1` meter above the bottom. If your drawing code still uses pixels from the top left, convert positions the same way when lining up sprites with bodies. The [importer rules](https://github.com/recursive-ai-oss/onephysics-csharp/blob/a5f45b1c8e34091892655a5f5536abb14d32528b/docs/spec/interchange.md#tiled-and-ldtk--map2d-2d) cover offsets, layer conventions and shapes the importer leaves out.
