Advertisement Jump to content
Sign in to follow this  
dworm

pathfinding behaviour

This topic is 1822 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement

I think that might depend on which algorithm you're using. Some of the more naive ones can paint the whole darn thing yellow so that there wouldn't be any white left.

 

From the second picture it looks to me like you're using A* because the algorithm will start with checking paths that produce the shortest traveled distance + heuristic distance. It doesn't take into consideration the fact that there might be an obstacle on it's path. You can picture this in your own house for example. If you have two people standing on opposite sides of a wall, the straight line distance (commonly used as heuristic in A*) would direct A* at the wall first, but the actual shortest path might mean that you have to backtrack to get to the door. If there were no obstacles in your path, and all tiles had equal cost, you could probably just expect a single straight path to your goal.

 

The first one looks like it might have goofed up, unless some of the tiles incur significantly different costs.

 

But, all said and done, I've only ever really had to deal with pathfinding once, so my knowledge of it is rather limited.

Share this post


Link to post
Share on other sites

Seems a bit strange...

 

I tried on mine in a total open area. Here is my output:

Total search time 213.
x11.................................................................................................................................
1x11................................................................................................................................
11x11...............................................................................................................................
.11x11..............................................................................................................................
..11x11.............................................................................................................................
...11x11............................................................................................................................
....11x11...........................................................................................................................
.....11x11..........................................................................................................................
......11x11.........................................................................................................................
.......11x11........................................................................................................................
........11x11.......................................................................................................................
.........11x11......................................................................................................................
..........11x11.....................................................................................................................
...........11x11....................................................................................................................
............11x11...................................................................................................................
.............11x11..................................................................................................................
..............11x11.................................................................................................................
...............11x11................................................................................................................
................11x11...............................................................................................................
.................11x11..............................................................................................................
..................11x11.............................................................................................................
...................11x11............................................................................................................
....................11x11...........................................................................................................
.....................11x11..........................................................................................................
......................11x11.........................................................................................................
.......................11x11........................................................................................................
........................11x11.......................................................................................................
.........................11x11......................................................................................................
..........................11x11.....................................................................................................
...........................11x11....................................................................................................
............................11x11...................................................................................................
.............................11x11..................................................................................................
..............................11x11.................................................................................................
...............................11x11................................................................................................
................................11x11...............................................................................................
.................................11x11..............................................................................................
..................................11x11.............................................................................................
...................................11x11............................................................................................
....................................11x11...........................................................................................
.....................................11x11..........................................................................................
......................................11x11.........................................................................................
.......................................11x11........................................................................................
........................................11x11.......................................................................................
.........................................11x11......................................................................................
..........................................11x11.....................................................................................
...........................................11x11....................................................................................
............................................11x11...................................................................................
.............................................11x11..................................................................................
..............................................11x11.................................................................................
...............................................11x11................................................................................
................................................11x11...............................................................................
.................................................11x11..............................................................................
..................................................11x11.............................................................................
...................................................11x11............................................................................
....................................................11x11...........................................................................
.....................................................11x11..........................................................................
......................................................11x11.........................................................................
.......................................................11x11........................................................................
........................................................11x11.......................................................................
.........................................................11x11......................................................................
..........................................................11x11.....................................................................
...........................................................11x11....................................................................
............................................................11x11...................................................................
.............................................................11x11..................................................................
..............................................................11x11.................................................................
...............................................................11x11................................................................
................................................................11x11...............................................................
.................................................................11x11..............................................................
..................................................................11x11.............................................................
...................................................................11x11............................................................
....................................................................11x11...........................................................
.....................................................................11x11..........................................................
......................................................................11x11.........................................................
.......................................................................11x11........................................................
........................................................................11x11.......................................................
.........................................................................11x11......................................................
..........................................................................11x11.....................................................
...........................................................................11x11....................................................
............................................................................11x11...................................................
.............................................................................11x11..................................................
..............................................................................11x11.................................................
...............................................................................11x11................................................
................................................................................11x11...............................................
.................................................................................11x11..............................................
..................................................................................11x11.............................................
...................................................................................11x11............................................
....................................................................................11x11...........................................
.....................................................................................11x11..........................................
......................................................................................11x11.........................................
.......................................................................................11x11........................................
........................................................................................11x11.......................................
.........................................................................................11x11......................................
..........................................................................................11x11.....................................
...........................................................................................11x1111111111111111111...................
............................................................................................11xxxxxxxxxxxxxxxxxxx...................
.............................................................................................11111111111111111111...................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................

Where:

"." is a free area.

"1" is an explored area.

"x" is the path.

 

Post your algorithm so we can check.

 

Edit: I am assuming you are using A*.

Edited by KnolanCross

Share this post


Link to post
Share on other sites

Same here (A* with binary heap), maybe there is something wrong in your open list? Are you sure you are adding all those points? It could be a bug.

Also, what heuristic are you using? Last week there was a guy who was not updating the openList after editing the value of a node, could be a hint...

 

Ops, the time output was not intended (I copied the whole output, my bad), anyways the time is in microsseconds (1s = 10^6).

Edited by KnolanCross

Share this post


Link to post
Share on other sites

yes there surely is something wrong, here is the code

 

 

 
 public void Pathfind(int startX, int startY, int targetX, int targetY)
        {




            Point start = new Point (startX,startY);
            Point target = new Point (targetX,targetY);


            if (start.X == -1 || start.Y == -1)
            {
                return;
            }




            int actualX = start.X;
            int actualY = start.Y;
            squareMap[actualX, actualY].D = 0;
            squareMap[actualX, actualY].F = Math.Abs(actualX - target.X) + Math.Abs(actualY - target.Y);


            openListNumber = 1;
            Openlist[1] = new Point(actualX, actualY);
            bool found = false;
            int step = 0;
            while (!found)
            {


                int D;
                step++;
                D = squareMap[actualX, actualY].D;
                int S;


                foreach (Point dir in ValidMoves(actualX, actualY)) // find lowest  F score
                {
                    int k = D + 1 + Math.Abs(dir.X - target.X) + Math.Abs(dir.Y - target.Y);
                    S = squareMap[dir.X, dir.Y].F;
                    if (S > k)
                    {
                        squareMap[dir.X, dir.Y].F = k;
                        squareMap[dir.X, dir.Y].D = D + 1;
                        squareMap[dir.X, dir.Y].DirX = actualX - dir.X;
                        squareMap[dir.X, dir.Y].DirY = actualY - dir.Y;




                        if (S == 5000)
                        {
                            addToOpenList(dir.X, dir.Y);
                        }
                        else
                        {
                            correctOpenList(dir.X, dir.Y);
                        }
                    }


                }
                squareMap[actualX, actualY].Checked = true;


                if (target.X == actualX && target.Y == actualY)
                {
                    found = true;
                }


                //pick lowest open list
                actualX = Openlist[1].X;
                actualY = Openlist[1].Y;


                RemoveFromList();
                if (openListNumber < 0) { found = true; }


            }


            //backtrack path         
            int pointX = target.X;
            int pointY = target.Y;
            int dirX = 1;
            int dirY = 1;
            if (pointX == -1 && pointY == -1)
            {
                return;
            }


            while (pointX != start.X || pointY != start.Y)
            {
                dirX = squareMap[pointX, pointY].DirX;
                dirY = squareMap[pointX, pointY].DirY;
                pointX += dirX;
                pointY += dirY;
                squareMap[pointX, pointY].IsPath = true;
            }
            squareMap[pointX, pointY].IsPath = false;
        }

this to add to open list:

void addToOpenList(int x, int y)
        {
            openListNumber++;
            Openlist[openListNumber] = new Point(x, y);
            int nTemp = openListNumber;
            int nTemp2 = nTemp;
            int F = squareMap[x, y].F;
            Point temp;
            while (nTemp2 > 1)
            {
                nTemp2 = nTemp / 2;
                if (squareMap[Openlist[nTemp].X, Openlist[nTemp].Y].F > F)
                {
                    temp = Openlist[nTemp2];
                    Openlist[nTemp2] = Openlist[nTemp];
                    Openlist[nTemp] = temp;
                }
                nTemp = nTemp2;
            }


        }

this to remove

 void RemoveFromList()
        {
            Openlist[1] = Openlist[openListNumber];
            openListNumber--;
            int nTemp = 1;
            int nTemp2 = 1;
            bool jump = true;
            Point temp;
            while (jump)
            {
                nTemp2 = nTemp;
                if (nTemp2 * 2 < openListNumber)
                {
                    if (squareMap[Openlist[nTemp2].X, Openlist[nTemp2].Y].F > squareMap[Openlist[nTemp2 * 2].X, Openlist[nTemp2 * 2].Y].F)
                    {
                        nTemp = nTemp2 * 2;
                    }
                    if (squareMap[Openlist[nTemp].X, Openlist[nTemp].Y].F > squareMap[Openlist[nTemp2 * 2 + 1].X, Openlist[nTemp2 * 2 + 1].Y].F)
                    {
                        nTemp = nTemp2 * 2 + 1;
                    }
                }
                else if (nTemp2 * 2 <= openListNumber)
                {
                    if (squareMap[Openlist[nTemp2].X, Openlist[nTemp2].Y].F > squareMap[Openlist[nTemp2 * 2].X, Openlist[nTemp2 * 2].Y].F)
                    {
                        nTemp = nTemp2 * 2;
                    }
                }

                if (nTemp != nTemp2)
                {
                    temp = Openlist[nTemp2];
                    Openlist[nTemp2] = Openlist[nTemp];
                    Openlist[nTemp] = temp;
                }
                else
                {
                    jump = false;
                }
            }

this to correct when a tile is found better than the previous value

      void correctOpenList(int x, int y)
        {
            int nTemp = 1;
            while (Openlist[nTemp].X != x || Openlist[nTemp].Y != y)
            {
                nTemp += 1;
            }
            int nTemp2 = nTemp;
            int F = squareMap[x, y].F;
            Point temp;
            while (nTemp2 > 1)
            {
                nTemp2 = nTemp / 2;
                if (squareMap[Openlist[nTemp].X, Openlist[nTemp].Y].F > F)
                {
                    temp = Openlist[nTemp2];
                    Openlist[nTemp2] = Openlist[nTemp];
                    Openlist[nTemp] = temp;
                }
                nTemp = nTemp2;
            }

        }

Share this post


Link to post
Share on other sites

In a first look to your code (and assuming the add/remove is working correctly):

 

This

int k = D + 1 + Math.Abs(dir.X - target.X) + Math.Abs(dir.Y - target.Y);

Seems wrong, in the code posted you are not checking if the neighbor is in the closed list (it is not a valid neighbor if it is).

Also, k should be the actual distance from the current node to the neighbor, not the heuristic distance to the target!

You should use the heristic distance to the target only when you are calculating the f value.

 

Here is the wikipedia algorithm marked where I think it is your mistake:

function A*(start,goal)
    closedset := the empty set    // The set of nodes already evaluated.
    openset := {start}    // The set of tentative nodes to be evaluated, initially containing the start node
    came_from := the empty map    // The map of navigated nodes.
 
    g_score[start] := 0    // Cost from start along best known path.
    // Estimated total cost from start to goal through y.
    f_score[start] := g_score[start] + heuristic_cost_estimate(start, goal)
 
    while openset is not empty
        current := the node in openset having the lowest f_score[] value
        if current = goal
            return reconstruct_path(came_from, goal)
 
        remove current from openset
        add current to closedset
        for each neighbor in neighbor_nodes(current)
            if neighbor in closedset
                continue
            >>>>>>>>>>> Take a look here! <<<<<<<<<<<<<<<<<<
            tentative_g_score := g_score[current] + dist_between(current,neighbor)
 
            if neighbor not in openset or tentative_g_score < g_score[neighbor] 
                came_from[neighbor] := current
                g_score[neighbor] := tentative_g_score
                f_score[neighbor] := g_score[neighbor] + heuristic_cost_estimate(neighbor, goal)
                if neighbor not in openset
                    add neighbor to openset
 
            >>>>>>>>>>> Up to here! <<<<<<<<<<<<<<<<<<

    return failure

I didn't really understand your list removal algorithm to say if it is correct =x

Also, didn't understand how your algorithm detects that a path is impossible.

 

Edit:

Now I understood the code, it is a heap update... seems ok, but just for the notice, once this condition:

if (squareMap[Openlist[nTemp].X, Openlist[nTemp].Y].F > F)

Is false, you can stop the loop, the heap property is restored by then.

Edited by KnolanCross

Share this post


Link to post
Share on other sites
Seems wrong, in the code posted you are not checking if the neighbor is in the closed list (it is not a valid neighbor if it is).

this is checked into:

 private IEnumerable<Point> ValidMoves(int x, int y)
    {
        foreach (Point movePoint in movements)
        {
            int newX = x + movePoint.X;
            int newY = y + movePoint.Y;


            if (ValidateCoor(newX, newY))
            {
                if ((squareMap[newX, newY].ContentCode == 0 || squareMap[newX, newY].ContentCode == 3) && !squareMap[newX, newY].Checked)
                {
                    yield return new Point(newX, newY);
                }
            }
        }
    }

but im not totally sure what you mean

 

 

 

 

You should use the heristic distance to the target only when you are calculating the f value.

 

i probably renamed F with k dunno why, but isnt it correct the check?

 

i thought its the same since the funcion distance from target (x,y) is a constant for (x,y)  while D (x,y) depends on the path to reach (x,y)

so if i check the D (distance from start) or the total should be the same, no?

 

 


Is false, you can stop the loop, the heap property is restored by then.

true, ill update that, but it should only save a few time, not really change the path 

Share this post


Link to post
Share on other sites

Nevermind about the closed list, it wasn't in the code you posted the first time, but it is in the ValidMoves.

 

On the "k" value, what you are doing is:

int k = D + 1 + Math.Abs(dir.X - target.X) + Math.Abs(dir.Y - target.Y);

What you should be doing is:

int k = D + 1

Which is the cost of moving current node to the neighbor.

Then you should test if:

dir is not on open list (in other words, dir has not been checked yet) OR dir current score is bigger than k (in other words, you found a better path to dir).

 

If the problem is still not clear, take a look at this link:

http://www.policyalmanac.org/games/aStarTutorial.htm

 

And pay attention to what happens bellow the Figure 4.

Edited by KnolanCross

Share this post


Link to post
Share on other sites

(i dont get how to multiquote using this forum :/ )

 

yes i do check , basically my closed list is

!squareMap[newX, newY].Checked

after i check all close square i add actual to 

squareMap[actualX, actualY].Checked = true;

making it skipped next time

 

 

dir is already on open list, dir is picked already from the intersection of not checked nodes via that code above

 

the check is basically below when i check if dir value was 5000 which is only possible (until i test on 3000x3000 map :D ) if it was a unchecked node

 

so if total value of dir node is higher either its 5000 and so its a unchecked node and it goes into 

addToOpenList(dir.X, dir.Y);

if it wasnt, it means it was checked already and in the open list so i just run the update mechanic to move him up the list depending on its new score

correctOpenList(dir.X, dir.Y);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!