Tile Map how to get the adjacent tiles. (Regarding obstacle avoidance, pathfinding)

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

Hi so I am a little bit new to tile maps. So I am using ASTAR algortihm to identify a section of the map as wall. So that the npc moves but this is happening
enter image description here

The obstacle avoidance is working by detecting the which tiles have collision shape. But I need to make the two tiles adjacent to the collision tile also be avoided. Because otherwise the character is getting stuck on the edge of the wall.

Any advice will be greatly appreciated.

Currently I used the world to map them map to world to disable the adjacent tiles. But now when the player stands in those adjacent tiles the navigation just stops.

Skydome | 2022-06-05 20:11

:bust_in_silhouette: Reply From: Inces
func get_neighbours(starttile: vector2, tilerange: int)
var adjacent = []
for x in [-tilerange,tilerange]:
     for y in [-tilerange, tilerange]:
              var targettile = starttile + Vector2(x,y)
              if targettile != starttile :
return adjacent

this will return all tiles in 8 directions from original tile, in given range. You can check if any of them is a wall and disable astar of original tile if it is so.

Hi, thank you. While I did figure out a way already. This looks a lot cleaner than my approach.

Skydome | 2022-06-06 20:07

I actually made mistake :
it is supposed to be

for x in range(-tilerange,tilerange)

in both of these lines :slight_smile:

Inces | 2022-06-06 21:01