Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java A*寻路-停止在平铺中对角移动_Java_Path Finding_2d Games - Fatal编程技术网

Java A*寻路-停止在平铺中对角移动

Java A*寻路-停止在平铺中对角移动,java,path-finding,2d-games,Java,Path Finding,2d Games,我试着让我的头绕过A*搜索算法,我看到一个正方形在迷宫中移动,但是这个正方形会穿过两个角落接触的瓷砖。以下是一个例子: 是否存在阻止算法将此添加到路径的方法 public List<Node> findPath(Vector2i start, Vector2i goal){ List<Node> openList = new ArrayList<Node>(); List<Node> closeList = new ArrayL

我试着让我的头绕过A*搜索算法,我看到一个正方形在迷宫中移动,但是这个正方形会穿过两个角落接触的瓷砖。以下是一个例子:

是否存在阻止算法将此添加到路径的方法

public List<Node> findPath(Vector2i start, Vector2i goal){

    List<Node> openList = new ArrayList<Node>();
    List<Node> closeList = new ArrayList<Node>();

    Node current = new Node(start, null, 0, getDistance(start, goal));
    openList.add(current);
    while(openList.size() > 0){
        Collections.sort(openList, nodeSorter);
        current = openList.get(0);
        if(current.tile.equals(goal)){
            List<Node> path = new ArrayList<Node>();
            while(current.parent != null){
                path.add(current);
                current = current.parent;
            }
            openList.clear();
            closeList.clear();
            return path;
        }
        openList.remove(current);
        closeList.add(current);
        for(int i = 0; i < 9; i++){
            if(i == 4) continue;
            int x = current.tile.getX();
            int y = current.tile.getY();
            int xi = (i % 3) - 1;
            int yi = (i / 3) - 1;
            Tile at = getTile(x + xi, y + yi);
            if(at == null || at == Tile.voidTile) continue;
            if(at.isSolid()) continue;

            Vector2i a = new Vector2i(x + xi, y + yi);
            double gCost = current.gCost + getDistance(current.tile, a);
            double hCost = getDistance(a, goal);
            Node node = new Node(a, current, gCost, hCost);
            if(vecInList(closeList, a) && gCost >= current.gCost) continue;
            if(!vecInList(openList, a) || gCost < node.gCost) openList.add(node);
        }
    }
    closeList.clear();
    return null;
}
公共列表findPath(Vector2i开始,Vector2i目标){
List openList=new ArrayList();
List closeList=new ArrayList();
节点当前=新节点(开始,null,0,getDistance(开始,目标));
openList.add(当前);
while(openList.size()>0){
排序(openList、nodeSorter);
当前=openList.get(0);
如果(当前的.tile.equals(目标)){
列表路径=新的ArrayList();
while(current.parent!=null){
添加路径(当前);
current=current.parent;
}
openList.clear();
closeList.clear();
返回路径;
}
openList.remove(当前);
关闭列表。添加(当前);
对于(int i=0;i<9;i++){
如果(i==4)继续;
int x=current.tile.getX();
int y=current.tile.getY();
席X=(I % 3)- 1;
int yi=(i/3)-1;
瓦特=盖特瓦(x+席,y+i);
如果(at==null | | at==Tile.voidTile)继续;
如果(at.isSolid())继续;
向量2a=新向量2I(x+席,y+i);
双gCost=current.gCost+getDistance(current.tile,a);
double hCost=获得距离(a,目标);
节点节点=新节点(a、当前、gCost、hCost);
如果(vecInList(closeList,a)&&gCost>=current.gCost)继续;
如果(!vecInList(openList,a)| gCost
小标题:如果您不想将这些磁贴添加到打开列表中,则不能将它们视为邻居。因此,创建寻路算法时应该回答的基本问题是:在我的图中,哪些节点被视为邻居

在您的情况下,如果它们的两个普通相邻都不可通过,则当前磁贴不是对角磁贴的邻居。例如,对于磁贴{+1,+1},这些邻居将是{0,+1}和{+1,0}。等等因此,让我们检查一下:

// code here...
if(at.isSolid()) continue;

if (Math.abs(xi * yi) == 1) {
  // this is true if we're checking diagonal tile
  if ( !isPassable(getTile(x + xi, y)) && !isPassable(getTile(x, y + yi))  {
    // Both common neigbors are not passable
    // So we won't add this tile as neighbor
    continue;
  }
}
以及可通过的方法:

private boolean isPassable(Tile tile) {
  return tile != Tile.voidTile && !tile.isSolid();
}

如果您处理对角平铺,常见的邻居总是存在的,因此您不应该检查它们是否为空。

您可以发送代码的相关部分吗?谢谢您的帮助,非常感谢