Skip to content

Commit

Permalink
fix AStar heuristic cost
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzeroglu committed Oct 17, 2021
1 parent e8712e7 commit dbf7336
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions js/core/AStar.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ AStar.prototype.isNodeBelongToVector = function(node, vector){
return node === this.getHeapNode(vector.x, vector.y, vector.z);
}

AStar.prototype.getTotalCost = function(heapNode){
var cost = 0;
while(heapNode){
var parentTag = heapNode.parentTag;
cost += heapNode.priority;
heapNode = heapNode.parent;
if (heapNode && parentTag != this.searchID){
heapNode = null;
}
}
return cost;
}

AStar.prototype.generatePath = function(endVector){

var path = this.path;
Expand Down Expand Up @@ -123,10 +136,15 @@ AStar.prototype.findShortestPath = function(fromVector, toVector){
var markNodeAsClosed = this.markNodeAsClosed;

var heapNode = this.getHeapNode(fromVector.x, fromVector.y, fromVector.z);
heapNode.priority = 0;
heapNode.priorityTag = searchID;
heapNode.parent = null;
heapNode.parentTag = null;
heapNode.jumpDescriptor = null;

var vec = vectorPool.get();

heap.insert(heapNode);

while (heapNode){
if (this.isNodeBelongToVector(heapNode, toVector)){
return this.generatePath(toVector);
Expand All @@ -135,11 +153,12 @@ AStar.prototype.findShortestPath = function(fromVector, toVector){
markNodeAsClosed(heapNode, searchID);

vec.set(heapNode.x, heapNode.y, heapNode.z);
var totalCost = this.getTotalCost(heapNode);
graph.forEachNeighbor(vec, function(neighborVec, cost, jumpDescriptor){

var neighborHeapNode = getHeapNode(neighborVec.x, neighborVec.y, neighborVec.z, heapNodes);

var heuristicCost = neighborVec.getDistanceSq(toVector);
var heuristicCost = totalCost;

if (!isNodeClosed(neighborHeapNode, searchID)){
neighborHeapNode.priority = cost + heuristicCost;
Expand Down

0 comments on commit dbf7336

Please sign in to comment.