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.
dotnet add package RecursiveAI.OnePhysics.InterchangeRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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, then dotnet run.
The snippet creates one static rectangle from the map:
Colliders: 1The full program
level.tmj is in the download:
{ "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": [] }
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:
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:
var map = TiledImporter.Import(text, new MapOptions { PixelsPerMeter = 32 });
After applying the map, Console.WriteLine($"Map width: {map.Size.X} m"); prints:
Map width: 4 mRelated recipes
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. 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 cover offsets, layer conventions and shapes the importer leaves out.