Translate the size of a tilemap into world coordinates

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ugly_cat

I want to cover a TileMap with a TextureRect. I’m having trouble getting the size and position of the TileMap in my scene.

I see that I can get the get_total_rect() of the TileMap, but I haven’t successfully translated this value into my scene.

:bust_in_silhouette: Reply From: kidscancode

get_used_rect() will return the full rectangle in tile space. For example, if you have a map that is 10 tiles wide and 10 tiles tall, you’ll get (0, 0, 10, 10). You then need to convert these into pixel space using map_to_world():

var tile_rect = $TileMap.get_used_rect()
var topleft = $TileMap.map_to_world(tile_rect.position)
var size = $TileMap.map_to_world(tile_rect.size)

If the TileMap is moved, you also need to add its position to place your TextureRect:

$TextureRect.rect_position = topleft + $TileMap.position
$TextureRect.rect_size = size

This works well for square tiles. It gets a bit more complicated for hex or isometric tiles.

Ah, that must be why I’m having difficulty. I’m using Isometric tiles. Can you explain how I would go about calculating the proper size for the TextureRect from an isometric grid?

ugly_cat | 2019-02-05 01:59