Add diagonal and detect objects in AStar2D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By HorseGorilla


After implementing enemy pathfinding with AStar2D, we added diagonal lines for 8-way instead of 4-way.

However, at certain object positions (randomly creating objects), it showed different values than I expected.

expected path


The AStar2D implementation was grid-based using tilemap. The game is top-down, and the implementation of AStar2D has been made using any examples floating around the Internet.

thank you.

( I used the A* simulator from PathFinding.js )

:bust_in_silhouette: Reply From: Whalesstate

in the result Image because it’s 8 ways and it checks for the neighbors [ top-left, top-right , bottom-left, bottom-right] and if they are empty then the object should move to them without checking if the left or up tiles are free, my advice is to use the 4 ways for path finding then simplify the path to have 8 diagonal movement

you just need an algorithm like the pixel perfect algorithm to simplify the path after finding it

Whalesstate | 2021-09-25 13:03

Thank you!

So, I made it in 4 directions first, and when it was diagonal, I put 0 in x and y, respectively, and after checking, if both are empty, I moved it diagonally.

HorseGorilla | 2021-09-27 11:31