Skip to content

Commit

Permalink
Made the heuristic consistent and added fcost comparison for a more a…
Browse files Browse the repository at this point in the history
…ccurate path
  • Loading branch information
Metaphoriker committed Jul 2, 2024
1 parent 9de0849 commit d32ce21
Showing 1 changed file with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,19 @@ private double heuristic() {
double manhattanDistance = this.position.manhattanDistance(target);
double octileDistance = this.position.octileDistance(target);
double perpendicularDistance = calculatePerpendicularDistance();
double heightFactor =
Math.abs(this.position.getBlockY() - target.getBlockY()); // Consider height differences
double heightDifference = Math.abs(this.position.getBlockY() - target.getBlockY());

return (manhattanDistance * heuristicWeights.getManhattanWeight())
+ (octileDistance * heuristicWeights.getOctileWeight())
+ (perpendicularDistance * heuristicWeights.getPerpendicularWeight())
+ (heightFactor * heuristicWeights.getHeightWeight());
double manhattanWeight = heuristicWeights.getManhattanWeight();
double octileWeight = heuristicWeights.getOctileWeight();
double perpendicularWeight = heuristicWeights.getPerpendicularWeight();
double heightWeight = heuristicWeights.getHeightWeight();

// Ensure the combined heuristic is consistent
return Math.max(
manhattanDistance * manhattanWeight,
octileDistance * octileWeight
+ perpendicularDistance * perpendicularWeight
+ heightDifference * heightWeight);
}

private double calculatePerpendicularDistance() {
Expand All @@ -85,6 +91,10 @@ private double calculatePerpendicularDistance() {

@Override
public int compareTo(@NonNull Node o) {
int fCostComparison = Double.compare(this.getFCost(), o.getFCost());
if (fCostComparison != 0) {
return fCostComparison;
}
int heuristicComparison = Double.compare(this.heuristic.get(), o.heuristic.get());
if (heuristicComparison != 0) {
return heuristicComparison;
Expand Down

0 comments on commit d32ce21

Please sign in to comment.