Search code examples
algorithmsearchartificial-intelligencea-star

How to add more parameters to A* heuristic than only distance?


For pathfinding in a small 2D game I use an A* algorithm, by now based on a simple euclidean distance heuristic. My game world is represented as a grid of cells, either occuppied by an obstacle or not. The character for which a path has to be calculated using A* can move in any direction (if not blocked), not only N/E/S/W.

Ok, so that basically works fine. Now I need to add another parameter to the A* heuristic function, a cost value tied to each grid cell. The higher this cost value, the more our character should try to avoid that cell.

However, I can't change the heuristic function to solely use this cost value per grid cell, because the cell's distance to the A* target location is still important. The character should try to avoid any high cost cells, but at the same time it also must not get too far away from the target position. I therefore need kind of a "trade off" between the cell's distance and its cost value.

Ideally, I would like to find a solution that allows me to easilly adjust/optimize such a relation between the cell's distance and its cost value, so that I can fine tune the heuristic.

Any ideas how to achieve this?


Solution

  • When you use any shortest path algorithm (A* or Dijkstrs) you need one cost value for each node.
    So you must think yourself a formula how to combine distance (your cells) with obstacle costs.
    You could create a cost() function which takes the length cost plus adds obstacle costs.