Extended Tiled - Position of Tile

I’m working with tiled and have a question about collision detection with the map. It’s easy enough to check to see if my object is colliding with a tile on a blocked layer with something like:

var blockLayer = map.GetLayer<TiledMapTileLayer>("Block");

TiledMapTile? tile;
int tx = (int)(position.X / map.TileWidth);
int ty = (int)(position.Y / map.TileHeight);
if (blockLayer.TryGetTile(tx, ty, out tile))
        {
            if (!tile.Value.IsBlank)
            {
                var id = tile.Value.GlobalIdentifier;
                return true;
            }
        }

But that leaves me without a few bits of crucial information that I need and I’m wondering if they’re available in the tiled extension and I’m just not finding them?

The first is the X,Y coordinates of the tile I’m colliding with. If my object is falling at a rate of 10px per update cycle it’s possible I’ve fallen several pixels into the tile when the collision is detected, and I’d like to reset the objects Y position to the top of the tile. (for example) If I go back in the GitHub repo, I see that the tile (X,Y) coordinates were available a few years ago when the extension was housed under Extended.Maps.Tiled, but seem to have disappeared with a major rewrite at some point… but I honestly don’t fully understand the new implementation of Tile.cs, so I might just be missing if those values are available.

If not, any suggestions on the best way to find them? My best(?) ideas are to run some math using world size, tile size and index of tile, but I think I’m going to have problems making that work consistently, or to back my object up along the vector it’s moving and re-run the collision check until it’s false and setting the position there… which seems needlessly expensive.

Just to circle back on this… I wanted to post what I ended up doing in case someone else has this question and stumbles upon this in the future. The answer is embarrassingly easy and I’m kind of ashamed it took me so long to figure it out!

  • I know the Y position of the bottom of my character
  • I know the height of my tiles
  • I know my tiles are all the same height.

So… I can pretty easily tell how far into the tile my character is just by taking the mod of the characters bottom and the tile height. Then it’s just a matter of subtracting that from the character’s Y position, which will pull them back up to be standing on the tile not into the tile.

Ground = characterBottom.Y - (characterBottom.Y % tileHeight);

1 Like