我在A*算法实现中遇到了一些问题。有时它会做出奇怪的决定,比如忽略移动成本并穿过高成本区域,或者在回到正确路径之前朝错误方向移动一个格子。
我已经花费了太多时间,不想再继续浪费了,所以我想寻求新的帮助来解决这个问题。
private List<Vector2> PathFromTo(Vector2 startID, Vector2 targetID){
List<Vector2> path = new List<Vector2> ();
List<Node> closedList = new List<Node> ();
List<Node> openList = new List<Node> ();
Node startNode = nodeList.Find (tgt => tgt.nodeID == new Vector2 (startID.x, startID.y));
if (startNode == null)
return path;
Node targetNode = nodeList.Find (tgt => tgt.nodeID == new Vector2 (targetID.x, targetID.y));
if (targetNode == null)
return path;
openList.Add (startNode);
while (openList.Count > 0) {
Node current = openList [0];
for (int i = 1; i < openList.Count; i++)
if (openList [i].GetFCost () <= current.GetFCost () && openList [i].GetHCost () < current.GetHCost ())
current = openList [i];
openList.Remove (current);
closedList.Add (current);
if (current == targetNode) {
RetracePath (startNode, targetNode, ref path);
return path;
}
foreach(Vector2 neighbour in current.neighbors) {
Node neighbourNode = nodeList.Find (tgt => tgt.nodeID == new Vector2 (neighbour.x, neighbour.y));
CheckNeighbor(ref neighbourNode, ref current, ref targetNode, ref closedList, ref openList);
}
}
return path;
}
private void CheckNeighbor(ref Node neighborTile, ref Node currentTile, ref Node targetTile, ref List<Node> closedList, ref List<Node> openList){
if (neighborTile != null) {
if (!neighborTile.passable || closedList.Contains (neighborTile)) {
} else {
int newCostToNeighbor = (int)(currentTile.moveCost + CalculateDistance (currentTile.position, neighborTile.position));
if (newCostToNeighbor < neighborTile.GetGCost() || !openList.Contains (neighborTile)) {
neighborTile.SetGCost (newCostToNeighbor);
neighborTile.SetHCost (CalculateDistance (neighborTile.position, targetTile.position));
neighborTile.SetParent (currentTile);
if (!openList.Contains (neighborTile))
openList.Add (neighborTile);
}
}
}
}
public float CalculateDistance(Vector2 tileA_pos, Vector2 tileB_pos){
float dX = Mathf.Abs (tileB_pos.x - tileA_pos.x);
float dY = Mathf.Abs (tileB_pos.y - tileA_pos.y);
float shift1 = -(tileA_pos.x + tileA_pos.y);
float shift2 = -(tileB_pos.x + tileB_pos.y);
float dZ = Mathf.Abs (shift2 - shift1);
return Mathf.Max (dX, dY, dZ);
}
private void RetracePath(Node start, Node end, ref List<Vector2> pathInfo){
pathInfo = new List<Vector2> ();
Node current = end;
while (current != start) {
pathInfo.Add (current.nodeID);
current = current.GetParent ();
}
pathInfo.Reverse ();
}
CalculateDistance
函数中是如何处理的呢? - GWigWam