I have implemented the A* search algorithm in a game for pathfinding AI. For some reason when I am in the positive X, Y range in relation to the entity tracking me, the Entity jitters back and forth. I printed the size of the node path returned and it would seem that it is generating two paths and getting stuck between them on each pass. This happens on a flat surface with no obstacles and within the level I built for testing with walls as well. Does anyone know any common things to check for in my algorithm? or if it may be outside the actual search method?
public List<Node> findPath(Vector2i start, Vector2i goal, boolean boatOrFloat) { List<Node> openList = new ArrayList<>(); List<Node> closedList = new ArrayList<>(); Node current = new Node(start, null, 0d, start.getDistance(goal)); openList.add(current); while(!openList.isEmpty()) { Collections.sort(openList, nodeSorter); current = openList.get(0); if (current.tile.equals(goal)) { List<Node> path = new ArrayList<>(); while (current.parent != null) { path.add(current); current = current.parent; } openList.clear(); closedList.clear(); System.out.println("" + path.size()); return path; } openList.remove(current); closedList.add(current); for(int i = 0; i < 9; i++) { if (i == 4) continue; int x = current.tile.getX(); int y = current.tile.getY(); int xi = (i % 3) - 1; int yi = (i / 3) - 1; Tile at = getTile(new Coord(x + xi, y + yi)); if (at == null) continue; if (at.solid(boatOrFloat)) continue; Vector2i a = new Vector2i(x + xi, y + yi); double gCost = current.gCost + current.tile.getDistance(a) == 1 ? 1 : 0.95; double hCost = a.getDistance(goal); Node node = new Node(a, current, gCost, hCost); if (vecInList(closedList, a) && gCost >= node.gCost) continue; if(!vecInList(openList, a) || gCost < node.gCost) openList.add(node); } } closedList.clear(); return null; }