A *寻路需要绕过拐角,而不是穿过它们

时间:2018-03-08 19:46:53

标签: java path-finding a-star

我有一个问题,我希望寻路计算绕过“墙”的一角,如下:

* []|
[][]|
____|

但截至目前,它正在穿过墙壁。我无法完全消除对角线运动,我希望寻路允许在开放区域进行对角线移动。到目前为止,我只能通过在那里创建另一个“墙壁”来说服它不要占据一席之地。这是所需的代码:

public static void Ai() {
    open.add(grid[startX][startY]);

    Cell current;

    while (true) {
        current = open.poll();
        //If the cell is blocked, move on
        if (current == null)break;
        closed[current.x][current.y] = true;

        //If cell is where you want to end up, stop the loop
        if (current.equals(grid[endX][endY])) {
            return;
        }

        Cell t;
        if(current.x-1>=0){
            t = grid[current.x-1][current.y];
            checkAndUpdateCost(current, t, current.finalCost+V_H_COST); 

            if(current.y-1>=0){                      
                t = grid[current.x-1][current.y-1];
                if (checkAndReturnWall(grid[current.x-1][current.y]) || checkAndReturnWall(grid[current.x][current.y - 1])) {
                    checkAndUpdateCost(current, t, (current.finalCost + DIAGONAL_COST) * 500);
                }
                else checkAndUpdateCost(current, t, current.finalCost+DIAGONAL_COST); 
            }

            if(current.y+1<grid[0].length){
                t = grid[current.x-1][current.y+1];
                if (checkAndReturnWall(grid[current.x-1][current.y]) || checkAndReturnWall(grid[current.x][current.y + 1])) {
                    checkAndUpdateCost(current, t, (current.finalCost + DIAGONAL_COST) * 500);
                }
                else checkAndUpdateCost(current, t, current.finalCost+DIAGONAL_COST); 
            }
        } 

        if(current.y-1>=0){
            t = grid[current.x][current.y-1];
            checkAndUpdateCost(current, t, current.finalCost+V_H_COST); 
        }

        if(current.y+1<grid[0].length){
            t = grid[current.x][current.y+1];
            checkAndUpdateCost(current, t, current.finalCost+V_H_COST); 
        }

        if(current.x+1<grid.length){
            t = grid[current.x+1][current.y];
            checkAndUpdateCost(current, t, current.finalCost+V_H_COST); 

            if(current.y-1>=0){
                t = grid[current.x+1][current.y-1];
                if (checkAndReturnWall(grid[current.x+1][current.y]) || checkAndReturnWall(grid[current.x][current.y - 1])) {
                    checkAndUpdateCost(current, t, (current.finalCost + DIAGONAL_COST) * 500);
                }
                else checkAndUpdateCost(current, t, current.finalCost+DIAGONAL_COST); 
            }

            if(current.y+1<grid[0].length){
               t = grid[current.x+1][current.y+1];
                if (checkAndReturnWall(grid[current.x+1][current.y]) || checkAndReturnWall(grid[current.x][current.y + 1])) {
                    checkAndUpdateCost(current, t, (current.finalCost + DIAGONAL_COST) * 500);
                }
                else checkAndUpdateCost(current, t, current.finalCost+DIAGONAL_COST); 
            }  
        }
    }

Walls返回null作为单元格,checkAndReturnWall在所选单元格中存在wall时返回true。

1 个答案:

答案 0 :(得分:1)

假设您的游戏区域是方格,那么您可以将运动评估为2个运动(x轴上的一个运动,y轴上的一个运动),然后将其作为一个运动。详细说明:

你正试图向东北方向移动。请原谅我的伪代码:

if(checkEastCell() != wall){
   moveNorthEast();
}
else if(checkNorthCell() != wall){
   moveNorthEast();
}
else{
   pathingFailure(); 
}