Unity A*寻径崩溃
本文关键字:崩溃 Unity | 更新日期: 2023-09-27 17:54:31
所以我试图实现A*算法。基本逻辑基本完成了,但有一个问题我就是无法解决。
当请求路径时,Unity停止响应,我的PC一直变慢,直到它挂起,我不得不强制关闭。
下面是简化后的代码:public static List<Node> ReturnPath(Vector3 pointA, Vector3 pointB)
{
/* A Bunch of initializations */
while (!pathFound)
{
//add walkable neighbours to openlist
foreach (Node n in GetNeighbours(currentNode))
{
if (!n.walkable)
continue;
n.gCost = currentNode.gCost + GetManDist (currentNode, n);
n.hCost = GetManDist (n, B);
openList.Add (n);
n.parent = currentNode;
}
//check openList for lowest fcost or lower hCost
for (int i = 0; i < openList.Count; i++)
{
if ((openList [i].fCost < currentNode.fCost) || (openList [i].fCost == currentNode.fCost && openList [i].hCost < currentNode.hCost))
{
//make the currentNode = node with lowest fcost
currentNode = openList [i];
}
openList.Remove (currentNode);
if(!closedList.Contains(currentNode))
closedList.Add (currentNode);
}
//Check if the currentNode it destination
if (currentNode == B)
{
Debug.Log ("Path Detected");
path = RetracePath (A, B);
pathFound = true;
}
}
return path;
}
只要目的地在两个节点之外,就可以正常工作。如果超过这个数,就会出现上述问题。为什么呢?我的第一个猜测是我在openList中投入了太多。
注意:我将一个30 × 30的单位平台(层)分成1x1的正方形,称为"节点"。GetManDist()获取两个节点之间的曼哈顿距离。
UPDATE:这是工作代码。注释太长
public List<Node> ReturnPath(Vector3 pointA, Vector3 pointB)
{
List<Node> openList = new List<Node>();
List<Node> closedList = new List<Node>();
List<Node> path = new List<Node> ();
Node A = ToNode (pointA);
Node B = ToNode (pointB);
Node currentNode;
bool pathFound = false;
A.hCost = GetManDist(A, B);
A.gCost = 0;
openList.Add (A);
while (!pathFound) // while(openList.Count > 0) might be better because it handles the possibility of a non existant path
{
//Set to arbitrary element
currentNode = openList [0];
//Check openList for lowest fcost or lower hCost
for (int i = 0; i < openList.Count; i++)
{
if ((openList [i].fCost < currentNode.fCost) || ((openList [i].fCost == currentNode.fCost && openList [i].hCost < currentNode.hCost)))
{
//Make the currentNode = node with lowest fcost
currentNode = openList [i];
}
}
//Check if the currentNode is destination
if (currentNode.hCost == 0) //Better than if(currentNode == B)
{
path = RetracePath (A, B);
pathFound = true;
}
//Add walkable neighbours to openlist
foreach (Node n in GetNeighbours(currentNode))
{
//Avoid wasting time checking unwalkable and those already checked
if (!n.walkable || closedList.Contains(n))
continue;
//Movement Cost to neighbour
int newGCost = currentNode.gCost + GetManDist (currentNode, n);
//Calculate g_Cost, Update if new g_cost to neighbour is less than an already calculated one
if (n.gCost > newGCost || !openList.Contains (n))
{
n.gCost = newGCost;
n.hCost = GetManDist (n, B);
n.parent = currentNode; //So we can retrace
openList.Add (n);
}
}
//We don't need you no more...
openList.Remove (currentNode);
//Avoid redundancy of nodes in closedList
if(!closedList.Contains(currentNode))
closedList.Add (currentNode);
}
return path;
}
currentNode的值有问题。由于我们在openlist中检查具有最低f_Cost或较低h_Cost的节点,而不是currentNode,因此在某些情况下,当寻路遇到墙时,它必须返回或转向导致增加f_Cost和h_Cost(两者都大于currentNode)。因此,openlist中不再有f_Cost/h_Cost较低的节点导致无限循环。简单的解决方案是每次将currentNode设置为openList中的任意元素。
添加currentNode = openlist[0];