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

NeilNeil Posts: 2Member

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 :(

Best Answer

  • NeilNeil Posts: 2
    edited August 23 Accepted Answer

    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()
    

Answers

  • NeilNeil Posts: 2Member
    edited August 23 Accepted Answer

    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()
    
  • nathanjwtxnathanjwtx Posts: 30Member

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

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file