This site requires JavaScript, please enable it in your browser!
Greenfoot back
Super_Hippo
Super_Hippo wrote ...

2017/2/3

Pathfinding in a dynamic world

Super_Hippo Super_Hippo

2017/2/3

#
Hello, in my game (a bit like Age of Empires some day hopefully), I want units to travel around the world. By doing that, they have to avoid obstacles like buildings. Right now I am struggling at how to implement he pathfinding. If possible, I do not want the unit to just move straight forward until it finds an obstacles and move around it because this could lead could still lead into a dead end behind the obstacle for example. I think letting each cell in the world know the route to each other can't be the most efficient way since the world is 2000x2000 right now and will probably be made bigger if everything works. My first idea was this: Each building creates four objects a bit off its corners. (I am not sure if I could remove them in the end, but I did it at least for testing and keeping things clear.) Let's call them "Corners". An actor is added between unit and target and checks if the way is blocked by a building. If there is one, the length is reduced until the first obstacle is found. Then it can find its corners, save every corner which is not on the other side of the building (= way of sight not blocked to this corner) in an array and also save the distance between the current position of the unit and this corner. After this, from the nearest corner, it checks if the way of sight is blocked to the target etc... I am stuck at making a real 'while-loop' out of that to get the shortest route to the target though... The next idea was to set the unit in the middle of its current position and the target and find all of those corner objects in a circle around it (maybe twice the distance between unit and target) with the 'getObjectsInRange' method and then calculate the distances between all of them to find the shortest route... I also read about Dijkstra, Anytime D* (this looked quite amazing) and some other algorithms, but I am not really sure if they can really be used here (or how). Is there a good way to deal with this problem (hopefully without creating huge lag)? Note that the obstacles change every now and then because units can build new buildings or destroy others.
Super_Hippo Super_Hippo

2017/2/7

#
For anyone who is curious, this idea is working now:
The next idea was to set the unit in the middle of its current position and the target and find all of those corner objects in a circle around it (maybe twice the distance between unit and target) with the 'getObjectsInRange' method and then calculate the distances between all of them to find the shortest route...
I only have to hope that it will still work if there are plenty units trying to find their way simultaneously.
You need to login to post a reply.