Hi,
I have a problem with my A-Star implementation. It produces too long path in certain situations (see image)
[29283-a-star.jpg*|29283]
Here´s the code:
public function findPath(start:GameObject, dest:GameObject):ArrayList{
if(this.traversed == null){
return;
}
this.path = new ArrayList();
var openList: ArrayList = new ArrayList();
var closedList: ArrayList = new ArrayList();
openList.Add(start);
while((!closedList.Contains(dest))){
if(openList.Count == 0){
break;
}
var currentCell: GameObject = openList[0] as GameObject;
var currentGridCell: GridCell = currentCell.GetComponent(GridCell);
closedList.Add(currentCell);
openList.Remove(currentCell);
for(var i = 0; i < currentGridCell.neighbors.length; i++){
var currentNeighbor:GameObject = currentGridCell.neighbors*;*
-
if(!closedList.Contains(currentNeighbor) && this.traversed.Contains(currentNeighbor) && currentNeighbor != null){*
-
var currentNeighborGridCell: GridCell = currentNeighbor.GetComponent(GridCell);*
-
currentNeighborGridCell.rating = this.rateGridCell(currentNeighbor, dest) + currentNeighborGridCell.moveCost;*
-
currentNeighborGridCell.predecessor = currentCell;*
-
openList.Add(currentNeighbor);*
-
}*
-
}*
-
openList.Sort(new RatingComparer());*
-
}*
-
if(closedList.Contains(dest)){*
-
path.Add(dest);*
-
dest.renderer.material.color.a = 1.0f;*
-
var gridCell: GridCell = dest.GetComponent(GridCell);*
-
while(gridCell.predecessor != null){*
-
path.Add(gridCell.predecessor);*
-
gridCell.predecessor.renderer.material.color.a = 1.0f;*
-
gridCell = gridCell.predecessor.GetComponent(GridCell);*
-
}*
-
}*
-
print(path.Count - 1);*
-
return path;*
I tried with messing with the heuristic value (now it´s simply the manhattan distance), but it produced even worse paths or the problem occured in other situatons.
What´s wrong with my code?
Thanks in advance.
*