I need help with A* pathfinding searching the neighbours for lowest GCost

Started by
2 comments, last by ferrous 9 years, 8 months ago

I need help with A* pathfinding searching the neighbours for lowest GCost

Guys I got the a* pathfinder almost down but my search neighbors for the lowest GCost code isn't working.

That code below should compare the last closeList index closeListNum with the the openList -7 index. It searches 8 moves back for the lowest GCost. I hope someone can help me out with this. :)

// search neighbours
lowestGCost = closeList[i-7].GCost;
for (int i = openListNum-6; i < openListNum; i++)
{
if (openList(i).GCost < lowestGCost)
{
lowestGCost = openList.GCost;
if (lowestGCost < closeList[closeListNum].GCost)
{

closeList[closeListNum].Num = openList.Num;
closeList[closeListNum].GridX = openList.GridX;
closeList[closeListNum].GridY = openList.GridY;
closeList[closeListNum].Heuristic = openList.Heuristic;
closeList[closeListNum].Parent = openList.Parent;
closeList[closeListNum].GCost = openList.GCost;
closeList[closeListNum].FCost = openList.FCost;

}


}
}

Advertisement

oops the code was wrong a little bit here is the new code update.

// search neighbours
lowestGCost = openList[i-7].GCost;
for (int i = openListNum-6; i < openListNum; i++)
{
if (openList(i).GCost < lowestGCost)
{
lowestGCost = openList.GCost;
if (lowestGCost < closeList[closeListNum].GCost)
{

closeList[closeListNum].Num = openList.Num;
closeList[closeListNum].GridX = openList.GridX;
closeList[closeListNum].GridY = openList.GridY;
closeList[closeListNum].Heuristic = openList.Heuristic;
closeList[closeListNum].Parent = openList.Parent;
closeList[closeListNum].GCost = openList.GCost;
closeList[closeListNum].FCost = openList.FCost;

}


}
}

Is openListNum guaranteed to be greater than or equal to 7 every single time you reach this code? Strange things would likely happen if openListNum were 3, for example, since you'd be reading incorrect memory. But that's just a guess; you never mentioned what actually happens, other than that presumably you don't get the path you were hoping for.

Have you tried stepping through the code with a debugger? If not, now is the perfect to learn how. The end-result behavior of a misbehaving pathfinding algorithm can be difficult to interpret. Watching the code execute line by line and observing the values it produces can make the error very obvious in many cases, though.

"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke

Yeah, that -6 and -7 both look really weird to me, but maybe fine depending on implementation? I just am thinking of say, the corner of a 2d grid, it only has 3 neighbors.

This topic is closed to new replies.

Advertisement