Hi,
I am currently working on improving my pathfinding skills… I just started with a non optimized A* algorithm (working with this tutorial: www.policyalmanac.org/games/aStarTutorial.htm) like I did many times before. Normally it worked, now it does in most cases: My rabbit I am testing with does always reach the target but sometimes it takes a strange way I can’t explain why.
As you can see on the screenshots:
This is the path from 0/0 to 40/40
And this is the path from 0/0 to 39/40
I guess the issue is somewhere here:
@Override
public void run() {
AStarQuad startQuad = map.getAStarQuad(start);
openList.add(startQuad);
while(!searchFinished()){
AStarQuad current = this.getLowestF(openList);
openList.remove(current);
closedList.add(current);
this.checkQuad(map.getNextQuad(current, 1, 0), current);
this.checkQuad(map.getNextQuad(current, 0, 1), current);
this.checkQuad(map.getNextQuad(current, -1, 0), current);
this.checkQuad(map.getNextQuad(current, 0, -1), current);
this.checkQuad(map.getNextQuad(current, 1, 1), current);
this.checkQuad(map.getNextQuad(current, 1, -1), current);
this.checkQuad(map.getNextQuad(current, -1, 1), current);
this.checkQuad(map.getNextQuad(current, -1, -1), current);
}
if(targetFound()){
ArrayList<Vector2f> pathList = new ArrayList<>();
pathList.add(target);
AStarQuad curr = this.getTarget();
while(curr != null){
pathList.add(new Vector2f(curr.getX(), curr.getY()));
curr = curr.getPrev();
}
Collections.reverse(pathList);
path = new Path(pathList);
}
else
path = new Path(null);
}
private void checkQuad(AStarQuad quad, AStarQuad current){
if(quad.isWalkable() && !closedList.contains(quad)){
quad.H = (int)FastMath.abs(target.x - quad.getX()) + (int)FastMath.abs(target.y - quad.getY());
if(!isInList(quad, closedList)){
if(!isInList(quad, openList))
openList.add(quad);
else{
if(this.getFromList(quad, openList).G > current.G + 14){
openList.remove(this.getFromList(quad, openList));
quad = new AStarQuad(quad.getX(), quad.getY(), quad.isWalkable(), current);
openList.add(quad);
}
}
}
}
}
Thank you in advance for any ideas