- Home /
Help Calculating Shortest Total Distance Travelled By a Combination of Objects
I'm looking to calculate a total distance between several objects to serve as a sort of really dumb ai. For example, if I have three NPCs walking around available to pick up a weapon, and there are four weapons on the ground, I want to know what is the best set of npc/weapon pairs (if "best" is defined as the shortest total distance travelled by all NPCs in aggregate). Imagine the scene starts with those NPCs and weapons randomly located, then the NPCs need to each grab the closest weapon, but "coordinating" to ensure that two NPCs don't go after the same weapon. And as the game continues, NPCs without weapons need to constantly find idle weapons in this way, and those weapons are not locked to a specific location.
My current solution nests for loops to add together the distances between all possible npc/weapon pairs. So it'd be something like:
distance(npcA, weapA) + distance(npcB, weapB) + distance(npcC, weapC) = t
distance(npcA, weapA) + distance(npcB, weapB) + distance(npcC, weapD) = u
distance(npcA, weapA) + distance(npcB, weapC) + distance(npcC, weapB) = v
distance(npcA, weapA) + distance(npcB, weapC) + distance(npcC, weapD) = w
etc etc etc
then
MIN(t, u, v, w...)
and the combination that gave me the min value is how I assign a weapon to each NPC
There has to be a better way, right? This scales terribly.
Your answer
Follow this Question
Related Questions
focusing on a unit during unit detection in an RTS 1 Answer
swipe distance detection dont work 2 Answers
Game NPC "Guide" Movement (Much like Navi from Zelda) 1 Answer
Help with 2D AI scripting 2 Answers
AI walk through solid walls 1 Answer