Search code examples
javascriptpathdashboardpath-finding

Find path avoid soft collisions


I have x,y dashboard 10x10. I have array of destinations x,y from which I need to find closest and its path.

I also have array of soft collisions and array of pernament collisions.

I'm using pathfinding package to find the path. I'm setting pernament collisions like so:

//set collisions
for(let i = 0; i < mapData.collisions.length; i++) {
    grid.setWalkableAt(mapData.collisions[i].x, mapData.collisions[i].y, false);
}

and I'm finding actual closest coordinate and its path like so:

//find actual closest coordinate and its path
for(let i = 0; i < destinationCoords.length; i++) {
    currentGrid = grid.clone();
    currentPath = finder.findPath(heroCoords.x, heroCoords.y, destinationCoords[i].x, destinationCoords[i].y, currentGrid);

    if(currentPath && currentPath.length !== 0 && currentPath.length < closestPathLength) {
        closestPathLength = currentPath.length;
        closestPath = currentPath;
        closestCoords = destinationCoords[i];
    }
}

now the problem is that if i have following dashboard:

enter image description here

where yellow is start point, green is end point and pink is soft collision it will just go through pink tile.

But what I want to achieve is to avoid soft collisions. So the path would be:

enter image description here

But when there is no other option the path would be:

enter image description here

Any ideas?


Solution

  • It seems like the appropriate strategy would be two run two passes:

    1) Mark both permanent and soft collisions as unwalkable.

    2) If there are no solutions, mark only permanent collisions as unwalkable.

    If there are still no solutions, there is no walkable path.