How to get Cell position in Global Coordinates?

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

Hi all.

Just a little question regarding TileMap. I’m trying to get navigation working, but I need to get the centers of my tiles in Global Coordinates so the vehicle can move to each one on its way to the destination tile. I also need to convert the Global Coordinates of the mouse cursor to the nearest Cell position on the TileMap, so I can set a destination tile.

I ask this because my tank goes flying off with a positive Vector2 at Ludicrous Speed. (Well, not exactly, but if it could, it’d be plaid.)

Oh, and it makes a fuss if the tile’s Global Position is negative in either X or Y.

:bust_in_silhouette: Reply From: kidscancode

TileMap has functions for this:

If you have a tile coordinate, you can find the position in pixels of the tile’s center like so:

# tile_pos is your tile coordinate
var tile_center_pos = map_to_world(tile_pos) + cell_size / 2

For your other question, the reverse is also possible:

var mouse_tile = world_to_map(get_global_mouse_position())

See TileMap docs for details)

Ah, got it. Thank you.

System_Error | 2020-04-07 15:06