Sign in to follow this  
Followers 0
Prot

How to avoid cutting corners with A* pathfinding?

2 posts in this topic

On my second try with A* I managed to calculate all values needed for traceback. Also marked the starting cell with S, blocked with B and the goal cell with F.

 

 

http://s14.directupload.net/file/d/3307/gsiapr4r_png.htm

 

 

Now for the backtracking I would simply follow the cells with lowest G value, starting from goal cell. Here I would follow G=24 => G=10 => S.

As you can see this solution would create a path which is not valid because it leads throught a wall in this case.

 

This hangs together with corner cutting when calculating the values for a grid. As you can see here. One would backtrace: G=50 => G= 40 and here one would take G=20 next. This leads to the corner cutting.

 

http://s7.directupload.net/file/d/3307/9ffdp83z_png.htm

 

I think this issue occurs when calculating the values for each adjacent cell. Maybe I could avoid this if I set some restrictions when adding adjacent cells to the current cell?

public List<Cell> GetAdjacent(Cell _currentCell, List<Cell> _closedList, List<Cell> _gridList) 
    {
        List<Cell> adjacentList = new List<Cell>();
        List<Cell> gridList = _gridList;
        List<Cell> closedList = _closedList;
        Cell currentCell = _currentCell;

        foreach (Cell cell in gridList) 
        {
            bool containedInClosedList = closedList.Any(c => c.id == cell.id);

            if (!cell.blocked && !containedInClosedList && 
                ((cell.positionCR.X == currentCell.positionCR.X - 1 && cell.positionCR.Y == currentCell.positionCR.Y) ||
                (cell.positionCR.X == currentCell.positionCR.X + 1 && cell.positionCR.Y == currentCell.positionCR.Y) ||
                (cell.positionCR.X == currentCell.positionCR.X && cell.positionCR.Y == currentCell.positionCR.Y - 1) ||
                (cell.positionCR.X == currentCell.positionCR.X && cell.positionCR.Y == currentCell.positionCR.Y + 1) ||
                (cell.positionCR.X == currentCell.positionCR.X - 1 && cell.positionCR.Y == currentCell.positionCR.Y - 1) ||
                (cell.positionCR.X == currentCell.positionCR.X - 1 && cell.positionCR.Y == currentCell.positionCR.Y + 1) ||
                (cell.positionCR.X == currentCell.positionCR.X + 1 && cell.positionCR.Y == currentCell.positionCR.Y - 1) ||
                (cell.positionCR.X == currentCell.positionCR.X + 1 && cell.positionCR.Y == currentCell.positionCR.Y + 1)))
            {
                adjacentList.Add(cell);
            }

        }



        return adjacentList;
    }

Could it also be a problem with the custom costs I defined? I took a G=10 for straight and G=14 for diagonal cells.

 

I think this is the last thing which stops me from finishing the algorithm, so I am looking forward for any help or constructive input.

Thanks in advance!

0

Share this post


Link to post
Share on other sites

Remember that you must only look at the nodes at your open list, and you must not add a node at the open list unless it is a valid move from the node you are currently checking. In my blog there is an implementation if you want to take a look (but it is written on C).

 

It seems to me that you are adding every neighbor to the open list, and this is not correct.

 

In my implementation I use eight booleans (one for each possible neighbor) and I have two functions, one that checks straight cells and the other check diagonal cells. I first call the straight cell check, where I test if the cell is blocked, if it is the two diagonals that would pass by it must also be blocked. For instance, when If test the left cell and it is blocked, I already know that the upper left and lower left cells are also blocked and must not be included in the open list yet.

 

Hope this helps.

Edited by KnolanCross
2

Share this post


Link to post
Share on other sites

 

In my implementation I use eight booleans (one for each possible neighbor) and I have two functions, one that checks straight cells and the other check diagonal cells. I first call the straight cell check, where I test if the cell is blocked, if it is the two diagonals that would pass by it must also be blocked. For instance, when If test the left cell and it is blocked, I already know that the upper left and lower left cells are also blocked and must not be included in the open list yet.

 

Did it according to your description. Now it works. Thanks for help

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