• Create Account

## How to avoid cutting corners with A* pathfinding?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1Prot  Members

538
Like
0Likes
Like

Posted 05 July 2013 - 09:44 AM

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.

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.

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> 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)))
{
}

}

}


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.

### #2KnolanCross  Members

1934
Like
2Likes
Like

Posted 05 July 2013 - 02:00 PM

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, 05 July 2013 - 02:04 PM.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

### #3Prot  Members

538
Like
0Likes
Like

Posted 09 July 2013 - 10:36 AM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.