r/howdidtheycodeit • • Sep 12 '21
Question In grid-based strategy RPGs, how did they code units that take up more than one space?
Imagine a game that takes place on a square grid. Most units take up 1 square. But what about units that are larger and take up 4 squares? How would you calculate pathfinding for them and make sure they fit in narrow spaces?
42
Upvotes
32
u/ctothel Sep 13 '21 edited Sep 13 '21
On a grid, the way I’d handle this is pick a square on the object to denote the “lead” square that you’ll use for the route finding, and store the relative locations of the rest of the squares.
Then for each iteration of my pathfinding algorithm, if the “lead” square is able to access a particular grid point, I’d follow up with a check to see if each of the object’s other squares would be able to access its corresponding location if the unit were to be sitting in that location. If not, you fail that node and try another.
Not a very expensive calculation!
If I can explain this better let me know.
15
u/trotFunky Sep 12 '21
A classic way to do pathfinding in cases like these is to consider the unit is of a regular size (in this case, 1x1), but to widen all obstacles accordingly (here, by one on each axis).
This way you have the exact same pathfinding, but you take into account size differences. You could even handle rectangular units by widening some axes more than others.
Of course this is not perfect : corners for example can be tricky, you need a way to enlarge your obstacles/restrict your pathfinding area on a unit by unit basis, if you handle rotations of rectangles during the path you will need to change obstacles multiple times midway, it can be slow... But it's a useful trick, it can even be used outside of grids !
2
u/thefrenchdev Sep 13 '21
You just have to consider all cases that are blocked on the grid due to the extra size when you calculate the path finding.
1
u/Othinsson Oct 19 '21
Ive read before (and ill never be able to find it again) that some AAA game had different navmeshes baked for different sized enemies and then they ran pathfinding on the appropriate map. You can do a similar thing with a grid where narrow paths become obstacles for large creatures