Search code examples
c#xnagrid2dtile

Get Rectangle from index in a tilemap


I have some tilemaps where all tiles are 64x64 pixels. I need to know the position of an tile if know the index in the tilemap. How I can do this in C#?


Solution

  • This should help you to find the x and y coords of the tile:

    int tileY = tileIndex / numberOfTiles;
    int tileX = (tileIndex % numberOfTiles) - 1;
    

    numberOfTiles refers to the number of tiles in the x direction of the tile map, e.g. if the tile map was 16 tiles wide and 20 tiles high, numberOfTiles would be 16. to get the other 2 coords just add the tile height to tileY and the tile width to tileX.