Tilemap pathfinding handling collisions with other objects?

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

Hello!

I want to make my NPCs move randomly but smartly on the map, avoiding collisions when they can.

For now, I have a TileMap for the ground tiles, and an other one for objects that can be placed on top of that (fences, rocks, …).
I’m good for the part of setting a random destination point, I implemented a flood fill algorithm from the NPC and saved every tiles when it can move to (so I just have to pick a random one in that array).
I tried to use Navigation2D to make NPC move randomly to a position, which works great except it does not handle collisions with my object TileMap.

What would be the simplest way to make NPC “smartly” move to the destination while handling collisions? I have a lot of fences that will block NPCs (animals) from moving, so I can’t just make them stupidly move and collide until they find a way.

I’ve seen some leads like subtracting collision shapes from navigation polygons.
Also, knowing that I already have every tiles coordinates the NPC can move to, I suppose I could build a moving algorithm from that, but I wonder if there is a simpler way of doing what I want…

Thanks!

This is a really bad solution, so this isn’t an answer, but I just draw manually draw the navigation polygon with a margin around collision shapes so that the NPCs will never collide with anything in the first place.

exuin | 2021-06-22 00:27

Actually never though of that, I’ll give it a try, thanks!
But would like to hear about a sort of “official” solution that should be used for that, if anyone knows any.

Sosthen | 2021-06-22 08:01