This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

[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 2019 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 2019 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

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.