As the AStar
node is implemented through the TileMap
it can be responsible for path-finding any child or sibling node. Children of the TileMap
have their position
property in local space meaning it takes the TileMap
's origin into account.
Assuming the TileMap
's parent is positioned at (0,0)
, if your TileMap
has its origin at (64,64)
, the formula for finding a child's global position would be child_node.position + Vector2(64,64)
*this is also calculated for you via the child_node.global_position
property. However, a sibling of the TileMap
at the exact same position would have its sibling_node.position == sibling_node.global_position
because its parent (same as TileMap
's) has its origin at (0,0)
.
At some point in your code you have a function that takes in two positions and returns a path between them. I don't know how you have your AStar
node implemented but seeing as you're using a TileMap
you're probably feeding it map coordinates. So your function looks something like this:
func find_path(world_start, world_end)
var map_start = world_to_map(world_start)
var map_end = world_to_map(world_end)
var start_point_index = get_point_index(map_start)
var end_point_index = get_point_index(map_end)
var map_path = astar_node.get_point_path(start_point_index, end_point_index)
var world_path = []
for map_point in map_path:
world_path.append(map_to_world(map_point.x, map_point.y))
return world_path
The trick of it is to always call this function with the same kind of coordinates. Either coordinates that local to the TileMap
, doing the conversion before you call the function - as in the example above - or global coordinates and take that into account when you're calling it. TileMap.get_used_rect().position
gets you the origin for your TileMap
if you decide to go the global route.