Thursday, December 24, 2015

c# - Pathfinding for units of variable size



I'm using a grid map for a top-down RTS, and using a* for pathfinding. Specifically, I've found one implementation which is heavily optimised, and I've been using it for a while, but now I've realised I only checked it with infantry and not with vehicles, which take more map space, and so I can't rely on the algorithm - what would be "hugging a cliff" for infantry would be passing through it for a vehicle.


So, I'm trying to decide what to do. I see three options. a. The obvious thing is write my own A* implementation, but this is both very time consuming and I presume I won't reach near the optimised speed of the algorithm I found. (rewriting the algorithm is out of the question - I just don't have the time to try to read it and understand it).


b. corrections - read the path generated by the algorithm, and either correct it at every step or when receiving the path recalculate it for width.


c. Hold variable sized maps, that are different representations of the free space on the map, on account of size.


Option C sounds to me the simpolest, but is likely to be too memory heavy, even if there're only three-four size categories. Between options a & b, I'm not sure what is likely to take more calculation time - a non-optimised A*, or running over & correcting a path.



Answer



A* is not a hard algo to understand and it is one you should get intimately familiar with if you plan on using it in games.


What you really need from your description is an annotated pathfinder. (clearance based) There is a great description of one here.


Clearance Based Path Finding


This article describes a hierarchical annotated a* path finder however once you can write your own A* from scratch adding features you need for each project is very simple.



Heavy use of a profiler will help you optimize your path finding in short order. It really is a simple piece of code once you get the hang of it.


Edit: I am sorry I forgot to add. If you already have a fast a* the modification to add the clearance based checks is pretty simple. Obviously your base object that anything that is going to use the path finder is derived from needs a "clearance" variable of some kind.


Inside the actually path finder just find the spot where it is checking against if things are walkable or not and that is where you add in the clearance checks. Also when you first generate your map you need to add a var to the node or however your graphing your map to pre-calculate the clearance values.(that will make it faster) Really that is basically all there is to it.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...