• 2D
  • [Beginner] How do I convert mouse position to a cells x & y coordinate? eg: So I can do a SET_CELL?

So I'm playing with the demo project supplied "Grid-based pathfinding with Astar" (navigation_astar).

And if you mouse click or SHIFT click, a start or end tile is place where you click. So you might click at mouse position 1000,500, but this is translated into a SET_CELL(18,9,1) for example.

I'm trying to alter the script so if I right click on a tile it toggles between tile 0 or -1 simply to make a "wall" appear and disappear at that position. But for the life of me I cannot workout how to convert the mouse positions to tile/cell positions.

Anyone able to tell me the magic required to convert the mouse X & Y values to the cell X & Y values below the mouse?

Following the demo, I can't work it out :(

First! Thanks for the tsunami of help from everyone! Oh!

In case another beginner comes looking for this sort of issue, the answer is to use WORLD_TO_MAP.

eg:-

var tile = get_parent().get_node('TileMap').world_to_map(target_position)

You can then use tile.x and tile.y to get the two values of the grid, or use it raw eg: set_cellv(tile, 1)

Where:-

target_position = get_global_mouse_position()
18 days later

Oh that's awesome! I was calculating that manually! Thanks :+1: