She ain’t much to look at, but she sure do work. Honestly, I’m not exactly sure how it works. A recursive function is a fierce and fickle mistress.
Actually, I do know how it works; I was just startled at how fast it came together. There are a lot of great resources on the subject, as well. Here’s the one I used:
And if you’d like to play around with it, just click the image.
The real problem I have with it is that it’s pretty short-sighted. Units don’t see a wall until they’re right on top of it. Obviously this makes sense for a lot of units- sometimes you don’t know where you’re going until you get there. But I think I’m going to try some smoothing algorithms to remove some waypoints and get those paths a little more natural-looking.
That’s a pretty cool little algorithm. It seems to find a fairly efficient algorithm. It takes awhile, though. Won’t that really bog down a computer when you’ve got 50 guys trying to find a path at once? Perhaps you should analyze if there are some wasted operations. Even if there’s just an addition or a small loop–inside a recursive algorithm, it adds execution time quick.
Unfortunately, smoothing it out will take even longer. I’m thinking about spreading execution out across multiple frames. That way it shouldn’t freak if fifty guys do try to pathfind at the same time.
If I can ever incorporate your raycast thing, the smoothing would be a walk in the park.
That would be pretty cool. You could also have them follow one another–though a single file line of soldiers isn’t much use when you’re trying to surround an enemy… I can do a quick raycasting write-up if you’d like.
They probably will end up following each other to some extent, just because they’ll all find similar paths from nearby locations. I’m thinking of introducing “Formations”, though. You’ll select several units and click a destination, and they’ll just find their own way there (single-file, perhaps) and assemble back into their formation when they get there.