Okay I posted not long ago about path finding which i have finally had a look at, I understand how it works but cannot implement it into code
Can someone advice me on what i need to do next please
private ArrayList<Node> openList;
private ArrayList<Node> closedList;
public APathfind() {
openList = new ArrayList<Node>();
closedList = new ArrayList<Node>();
}
public void calculatePath(Vector2 start,Vector2 target,MasterTile[][] map){
openList.clear();
closedList.clear();
Node startNode = new Node();
openList.add(startNode);
}
Node
public float h; // Distance from a node to the target node (Manhatten)
public float g; // Movement cost from node to another node
public float f; // h+g
public Node parent;
public Node(float h,float g,float f) {
this.h = h;
this.g = g;
this.f = f;
}
public Node() {
this(0,0,0);
}
Putting forth your code and asking us to try to complete most of it doesn’t help you learn. Have you tried to implement it at all?
This article is really helpful. It has the source code and a very thorough breakdown of how everything works. Try using that article first and see if you can try to implement some more things yourself. I’m not asking you to solve it yourself, if you’re really stuck then ask us and we’ll deliver.
Thanks or replying, I have looked at the link and have attempted a bit more
I am mainly following this tut http://www.policyalmanac.org/games/aStarTutorial.htm which makes a lot of sense but i am having trouble with implementing it
private ArrayList<Node> openList;
private ArrayList<Node> closedList;
private TileMap map;
private Node[][] nodes;
public APathfind(TileMap map) {
openList = new ArrayList<Node>();
closedList = new ArrayList<Node>();
this.map = map;
nodes = new Node[map.getWidth()][map.getHeight()];
}
public void calculatePath(int startTileX,int startTileY,int targetTileX,int targetTileY){
openList.clear();
closedList.clear();
Node startNode = new Node();
nodes[startTileX][startTileY] = startNode;
int currentX = startTileX;
int currentY = startTileY;
openList.add(startNode);
while ((currentX != targetTileX && currentY != targetTileY) || closedList.contains(nodes[targetTileX][targetTileY])){
}
}
I understand the concept… I get to the point where i am trying to calculate the f,g,h and i am not completely sure how i am meant to do this… or if i am even doing it correctly
while ((currentX != targetTileX && currentY != targetTileY) || closedList.contains(nodes[targetTileX][targetTileY])){
// Top
Node top = new Node();
}
I don’t know what specifically what will do it for you, but I can link my favorite A* walkthrough: http://www.redblobgames.com/pathfinding/a-star/introduction.html
It starts with BFS and goes through the evolution into A*, including where f g and h come from.
I found that one really confusing but i really liked the interactions
Am i doing this right??
while ((currentX != targetTileX && currentY != targetTileY) || closedList.contains(nodes[targetTileX][targetTileY])){
// Top
// H (From current to target)
float hx = Math.abs(targetTileX - currentX);
float hy = Math.abs(targetTileY - (currentY+1));
float h = hx + hy;
// G (Movement Cost)
float g = parent.g + 10;
// F (G + H)
float f = g+h;
Node top = new Node(h,g,f);
}
It can be kind of hard to spot errors in this kind of code just from reading through it online. As has probably been mentioned elsewhere, you’d probably have better luck digging into it with debugging tools to figure out what’s going on. Also, I don’t see an indication in your most recent post of where the path is supposed to begin and end, and in absence of that it’s difficult to diagnose the problem based on the screenshot. Maybe the path start and end are correct though and it’s only the ‘zigzag’ behavior at the bottom of the screenshot that’s wrong.
In any case, I skimmed the code and am wondering about your ‘calculate node’ function. Here:
float hx = Math.abs(targetTileX - currentX);
float hy = Math.abs(targetTileY - (currentY + moveCost));
float h = hx + hy;
Is there a particular reason you’re adding the movement cost to the current y value? Maybe I’m missing something (and apologies if so), but that doesn’t seem to be correct. I’d expect the computation of ‘h’ to look more like this:
float hx = Math.abs(targetTileX - currentX);
float hy = Math.abs(targetTileY - currentY);
float h = (hx + hy) * 10;
I think the tutorial suggests this as well.
You might also double-check that your ‘g’ values are being computed correctly - I’m not seeing right off how the ‘g’ values are being tracked. (Stepping through in the debugger with a simple straight-line path would be a good idea here - if it’s working correctly, the ‘g’ values for the nodes in the path should be successive nonnegative integer multiples of 10.)
Again, I only skimmed the code, and I haven’t implemented this algorithm in a while, so it’s possible that everything in this post is wrong (well, the advice about using your debugging tools is probably ok :)).
Based on the tutorial you’re following? It still doesn’t look right to me. For one thing, it seems the ‘g’ cost would get smaller with each successive node. Also, the distance from the origin to the current position isn’t really relevant here. And, I don’t see the move cost (10 or 14) incorporated anywhere.
Anyway, maybe the code could use another look. The best thing to do would probably be to step through it in the debugger, but at the very least, you could print out the ‘g’ cost at each step, just to make sure you’re getting sensible values (e.g. no negative numbers, etc.).
‘g’ still looks wrong. It still seems to have the same problems it did before (move cost not incorporated, and uses distance from origin to current position, which is irrelevant), and now it doesn’t appear to factor in the parent ‘g’ at all.
Does the tutorial explain how to compute ‘g’? Can you tell us where you’re getting your current code? Is that from the tutorial? Or maybe you could try explaining in words what you think ‘g’ is supposed to represent - that might help illuminate the problem.
A* (and graph algorithms in general) can be tricky to implement. Speaking for myself, I’ve implemented A* more than once, but if I had to implement it right now I’d still have to do some review and do everything very carefully in order to get it right. Unfortunately, getting close to ‘right’ generally won’t work - if even one little thing is wrong (such as how ‘g’ is computed), it’s unlikely to work. Also, because there are so many more ways to get it wrong than to get it right, trial and error probably isn’t a particularly effective way to proceed.
Also, if I may ask, what development tools are you using? Have you tried setting up a simple test case (e.g. a short, unobstructed straight-line path) and stepping through the code in the debugger?
Thanks Jesse for replying, I am using Eclipse, I am not sure what you mean?
I have just fixed the issue I think??
private Node calculateNode(int currentX, int currentY, int targetTileX, int targetTileY, Node parent, int moveCost) {
// H (From current to target)
float hx = Math.abs(targetTileX - currentX);
float hy = Math.abs(targetTileY - currentY);
float h = (hx + hy);
// G (Movement Cost)
float g = moveCost;
// F (G + H)
float f = g + h;
Node node = new Node(h,g,f, parent, currentX, currentY);
return node;
}
For tiles which are top, bottom,left,right of the parent is this has a movement cost of 10
and the diagonal tiles have a movement cost of 14
No result
I am not sure what i am doing wrong… I will rewrite it tomorrow after work…
Unless it’s factored in somewhere else, I think ‘g’ is still wrong because it doesn’t incorporate the parent cost. (Either that or I’m misremembering how to compute ‘g’.)
Do you know how to use the debugger in Eclipse? (It’s ok if the answer is no by the way I’m just trying to get a better picture of the situation.)
Well yes i am working the G wrong just looked it up "G = the movement cost to move from the starting point A to a given square on the grid, following the path generated to get there. "
Also no i do not know how to use the debugger
[quote]Since we are calculating the G cost along a specific path to a given square, the way to figure out the G cost of that square is to take the G cost of its parent, and then add 10 or 14 depending on whether it is diagonal or orthogonal (non-diagonal) from that parent square.
[/quote]
float g = parent.g + movementCost(parent, currentX, currentY);
Where movementCost() is the cost to get from parent to (currentX, currentY). (the 10-or-14)
This definition of G says: “the cost to get from the starting point to where we are now is the cost of all previous steps in the path (the parent.g) plus the cost of this one last step.”
Each node builds onto the path, so G is a running tally of how costly the path has gotten.