• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
dworm

pathfinding behaviour

18 posts in this topic

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.

2

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
2

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
2

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;
            }

        }
2

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
2

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 

0

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
2

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);
0

Share this post


Link to post
Share on other sites

Add a flag for that, it will make your life easier.

 

That seems to be what is wrong in your code, you are mixing the g cost (the ACTUAL cost to get to a node from the start) with the f cost (the estimated cost to get to the end).

 

 

Edit:

I got to go for today, here is my code that evaluates the neighbors:

static int processCell(pathFindingStruct* path, int xAdjust, int yAdjust, edgeRef* edge, edgeRef* end, const unsigned extraCost){

    if(path->mainGrid[edge->i + yAdjust][edge->j + xAdjust].inClosedList == 1){
       return 1;
    }

    unsigned distance = path->mainGrid[edge->i][edge->j].costSoFar + extraCost;
    if (path->mainGrid[edge->i + yAdjust][edge->j + xAdjust].inOpenList == 0 || distance < path->mainGrid[edge->i + yAdjust][edge->j + xAdjust].costSoFar){

        path->mainGrid[edge->i + yAdjust][edge->j + xAdjust].parent.i = edge->i;
        path->mainGrid[edge->i + yAdjust][edge->j + xAdjust].parent.j = edge->j;

        path->mainGrid[edge->i + yAdjust][edge->j + xAdjust].costSoFar = distance;
        path->mainGrid[edge->i + yAdjust][edge->j + xAdjust].totalCost = path->mainGrid[edge->i + yAdjust][edge->j + xAdjust].costSoFar + path->heuristicDistance(edge->i + yAdjust,edge->j + xAdjust, end->i, end->j);

        if (path->mainGrid[edge->i + yAdjust][edge->j + xAdjust].inOpenList == 0){
            edgeRef* newNode = getEdgeRef(path, edge->i + yAdjust, edge->j + xAdjust, &path->mainGrid[edge->i + yAdjust][edge->j + xAdjust]);
            if (newNode == NULL || (modifiedHeapInsert(path->openList, newNode) == 0)){
                free(newNode);
                return 0;
            }
            path->mainGrid[edge->i + yAdjust][edge->j + xAdjust].inOpenList = 1;
            path->mainGrid[edge->i + yAdjust][edge->j + xAdjust].myRef = newNode;
        }
        else{
            modifiedHeapUpdatedValue(path->openList, path->mainGrid[edge->i + yAdjust][edge->j + xAdjust].myRef);
        }

    }

    return 1;
}

It is in C, using pointers to avoid copies. Important notes:

Edge is the current node.

xAdjust and yAdjust is the move, for instance, if their values are (1, 1) I am moving right and down.

The grid is [y][x] aligned.

The return value is just to check memory errors.

"costSoFar" is g and "totalCost" is f.

extraCost is used because diagonal movements are more expensive.

Edited by KnolanCross
2

Share this post


Link to post
Share on other sites

this:: ?

 

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

doesnt change anything ofc..

maybe im not understanding what you mean...

can you post yoru compare code so i see the difference from mine?

 

 

edit: i think my problem is in the heap tbh

for some reason i suspect the new "best" nodes are not going to the top which makes them evaluated after other bad nodes in the open list

 

i mean for an empty map binary heap should just do

-check all near nodes

-add new to open list

- new best nodes (tipically 1 if diagonal is allowed or 2 otherwise) come to the top of the list

 

other open list node should never be evaluated after distance 1 which is more than any other node on diagonal has...

 

 

Edited by dworm
0

Share this post


Link to post
Share on other sites

Hmm, yes, those are the values you should use.

 

About the heap, try with a smaller example and print the whole heap to check if it is always the lowest F on top.

0

Share this post


Link to post
Share on other sites

THNX that was a huge bug, and improved everything a lot

 

still its not perfect, there is a small derive anyway :/

0

Share this post


Link to post
Share on other sites

update it seems to work decently now after all the small fixes, also i add diagonal tiles cost and it seems to work much better

probably another problem was too many tiles with same cost, ill try to improve that too

 

 

dunno how fast it should be thouhg on a 200 x 200 map neraly empty it takes few ms but with many obstacles it take till 30 40 ms (on my core duo)

0

Share this post


Link to post
Share on other sites


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

[rollup='How quoting works']

The multi-quote buttons can be used to quote the full text of multiple replies, and works as a toggle style button.  To use it, toggle the option on by clicking the button on one or more posts; a small popup should appear in the lower right corner listing how many posts are selected, and providing a reply button.  Clicking that reply button should take you to the full post editor with the full text of each selected post provided in the editor.

 

You can also "selective quote" to reply to small portions of one or more posts.  To use this functionality, highlight some text and click the "selective quote" button; the highlighting text will appear in a quote tag in the quick post editor at the bottom of the topic.  You can use this function multiple times to add additional snippets to the post editor.

[/rollup]

 

:)

1

Share this post


Link to post
Share on other sites

update it seems to work decently now after all the small fixes, also i add diagonal tiles cost and it seems to work much better

probably another problem was too many tiles with same cost, ill try to improve that too

 

 

dunno how fast it should be thouhg on a 200 x 200 map neraly empty it takes few ms but with many obstacles it take till 30 40 ms (on my core duo)

 

Performance will depend a lot on the language used. I posted my results the other day, you can use it as basis:

 

http://www.gamedev.net/topic/651968-binary-heap-vs-list-a-my-humble-performance-comparison-updated-with-jps/

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0