So I’ve been reading this article on binary heaps and trying to incorporate it in my pathfinding code but have no luck. I was hoping to get some feedback on my attempt to see what went wrong:
void SortedAdd (Node nextNode) {
if(open.Count < 1){
open.Add(new Node());
return;
}
open.Add(nextNode);
int index = open.IndexOf(nextNode);
int parentIndex = Mathf.RoundToInt(index/2);
float fCost = nextNode.f;
while(fCost > open[parentIndex].f){
if(fCost > open[parentIndex].f){
Swap(nextNode, open[parentIndex]);
int opIndex = parentIndex;
parentIndex = Mathf.RoundToInt(opIndex/2);
}else{
break;
}
}
}
void Swap (Node n1, Node n2) {
int n1ind = open.IndexOf(n1);
int n2ind = open.IndexOf(n2);
open[n1ind] = n2;
open[n2ind] = n1;
}
When I checked the open list, the nodes were not in order therefore the path was all over the place. Any idea where I went wrong?