I have a simple WebGL maze game and has problem with enemies overlapping when they are going to target position. Now i'm detecting there collisions, but dont know what to do with them when they are collide, if i choose to stop enemy when it collide with someone other - then when first enemy will reach target other will just stop one after another, so i cant use this solution. The perfect result will be if enemies will bypass other enemies, and i was trying to repel enemy in opposite direction from enemy with which it collides, but then i cant control it, and it could be repelled into the wall, or stuck between wall and other enemy. Could someone share the right solution? I dont need any code examples, i will be happy to hear your thoughts in theory
This is a very common problem in AI pathfinding, and you generally have three possible solutions:
- Accept the overlap. But this is far from ideal.
- Queue your enemies. Detect a collision, and stop the one behind, for up to half a second, then recompute the path and get it moving again. This is a simple, but very naive modification.
- Make your pathfinding aware of the position of your enemies. At the beginning of each AI "frame" update the map with the positions of each enemy. Treat these "maze cells" as blocked, and compute the path based on these additional blockages. Periodically recompute paths, and update the map as enemies are removed/added. If a valid path can't be found, wait a few frames for a path to open, and try again.