How to make an astar id function that supports negative values

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By laedo

I am having issues coming up with an id system that can work with negative positions in a TileMap, so something like (-1, -1).

This is how it looks like right now and I’m wondering what my options are.

Generally, how would you make AStar work with negative values? I’m hitting a wall since p_id in AStar only accepts values >= 0.

func calculate_point_index(point):
  return point.x + map_size.x * point.y
:bust_in_silhouette: Reply From: leonidboykov


You have to calculate an offset of the map and subtract it from point coordinates, i.e. if your script extends TileMap, you may use get_used_rect():

# get map limits
onready var map_limits = get_used_rect()

# calculate cell index
func calculate_point_index(point):
    # subtract offset from position
    point -= map_limits.position
    return point.y * map_limits.size.x + point.x