How to handle tilemap-based collisions

I’m making a simple platformer with a tilemap-based world. My levels are laid out like so:

image

For every wall block, I create a Cuboid shape and attach it to the Ground body, as recommended in the guide:

if WALL_TILE_TYPES.contains(&tile.type_) {
    let shape = ShapeHandle::new(Cuboid::new(Vector2::new(
        Tile::SIZE / 2. - 0.01,
        Tile::SIZE / 2. - 0.01,
    )));
    let translation = vector;
    physics.build_collider(
        ColliderDesc::new(shape).translation(translation),
        physics.ground_handle,
        false,
    );
} 

My game starts lagging when I have more than 50 blocks, which seems like a completely reasonable amount. Is there a better way to handle tilemap-based collisions? Are there any ways I can optimize this, aside from writing an algorithm that decreases the number of colliders in my world?

Hi! It’s surprising that 50 blocks makes the game lag. Are you compiling in release mode? Because debug is much, much, slower than release.

You’re absolutely right — release mode runs perfectly. In general, would you say that I’m using nphysics in the correct way for this problem?

I’m curious as well to know if that is the correct way to implement tile-based collison using nphysics/ncollide. :slight_smile:

And (assuming your player collision shape is a cuboid too), what if your player collision shape is a sphere instead, would that still work?

I have never written a tile-based game myself so I don’t really know what the best-practice are. But I can see two options:

  1. Do as it is done in this thread, with one Cuboid collider per tile.
  2. Or extract the contour of the ground and generate a single Polyline representing this contour.

I’m not sure which is preferred in what situation.