I'm very new to Godot and... well, everything. I started with the 2d intro project and then started looking at the astar demo, and am now trying to expand this by using an isometric grid instead of the square one.

In AstarGrid2D it calculates the isometric grid for you and this seems to work well. I can use get_point_position(...) to convert a path into coordinates to have my player character move towards. This works well.

I want to extend this by having the player click the mouse, but I'm having trouble converting the mouse to the isometric coordinates. I tried using local_to_map(Vector2) but this uses the coordinates of the isometric TileMapLayer, but the TileMapLayer seems to use a completely different (and a little bit odd) system to number the tiles.

So when you use AstarGrid2D, what's the expected method to convert global coordinates to a particular cell?

    Chump8933 use raycasts, very common, there is even tutorial in the docs.

    https://docs.godotengine.org/en/stable/tutorials/physics/ray-casting.html

    const RAY_LENGTH = 1000
    
    func _physics_process(delta):
    	var space_state = get_world_3d().direct_space_state
    	var cam = $Camera3D
    	var mousepos = get_viewport().get_mouse_position()
    
    	var origin = cam.project_ray_origin(mousepos)
    	var end = origin + cam.project_ray_normal(mousepos) * RAY_LENGTH
    	var query = PhysicsRayQueryParameters3D.create(origin, end)
    	query.collide_with_areas = true
    
    	var result = space_state.intersect_ray(query)

      kuligs2 Thanks, as mentioned I'm new to Godot and so not really following this.

      I'm trying to identify what the cell coordinates of an AstarGrid2D isometric grid is, and I'm working in 2D. I've read through the info you linked on raycasting, but I don't understand how I can use this to identify the cell coordinates. The cell doesn't seem to exist as a thing so I don't understand how I can raycast to find it.

      I have a function find_path on my TileMapLayer, called like:

      destination = Vector2i(get_global_mouse_position())
      path = _tile_map.find_path(position, destination)

      Then the function is:

      func find_path(start, end):
          path = astar_grid.get_point_path(start, end, true)
          return path.duplicate()

      Currently 'start' and 'end' coordinates are screen coordinates, and I'm trying to work out how I convert these values to be the grid coordinates. e.g. they are currently something like (345, 233) and I need them to be more like (0,1) or (2,3).

      I haven't been able to work out how ray casting fits into this.

      I think I've worked it out. I was hoping that I had missed a built in function since it seems like it should be there, but my solution uses math and trial and error. This seems to work:

      func _nearest_point(vector):
      	var base = Vector2(vector) - astar_grid.offset 
      	base.x -= astar_grid.cell_size.x/2
      	var grid_x = (base.x / astar_grid.cell_size.x) + (base.y / astar_grid.cell_size.y)
      	var grid_y = (base.y / astar_grid.cell_size.y) - (base.x / astar_grid.cell_size.x)
      	return Vector2i(grid_x, grid_y)
      8 days later

      This is for Godot4, not 3
      Your destination is in Local coordinates.
      You're using a tilemap which uses Map coordinates.
      AstarGrid2D uses both coords in arrays. ID path is MAP, point path is LOCAL.
      to change same location from local to map use tilemap.local_to_map
      to change same location from map to local use tilemap.map_to_local
      So map coord of Vector2i(11,2) is the same place as local coord Vector2i(368,80)