Game Development Reference
In-Depth Information
}
openList.remove(current);
current.setClosed(true);
if (goalNode.isClosed()) {
break;
}
// sort list
Collections.sort(openList, waypointComparator);
}
backtrack();
}
4. It should begin by adding the
startNode
to
openList
.
5. Next, we define a while loop that always picks the first node in
openList
.
6. Inside this loop, we create another
for
loop that iterates through all the currently
selected connected nodes, called neighbors.
7. If the neighboring node is not in
openList
, it should be added there. It should
also set the current node to
parentNode
of the
neighbor
node, as shown in
the following code:
openList.add(neighbor);
neighbor.setOpen(true);
neighbor.setParent(current);
8. While doing this,
g
of the neighbor should be set to current node's
G
plus the dis-
tance between the two nodes, as shown in the following code:
neighbor.setG(current.getG() + (int)
(current.getPosition().distance(neighbor.getPosition())
* multiple));
9. Also, if
H
has not already been calculated for
neighbor
, it should, by measur-
ing the distance between
neighbor
and
goalNode
.
F
should be updated by
adding
G
and
H
together, as shown in the following code:
if(neighbor.getH() == 0){
neighbor.setH((int)
(neighbor.getPosition().distance(goalNode.getPosition())
* multiple));