# Pathing Algorithm Help

This topic is 2814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm developing a game that requires some advanced(?) path detection. It is a 2D, top down, turn based game and when the user activates a character, all eligible move positions should be highlighted. So, if they activate their character and no obstacles are around, it should display a circle. If there is an object at the edge of the circle, it should look like the middle picture.

However, if there is an object in the middle, pathing should sort of "blob" around that object. The character can always move 'x' distance, but that distance does not have to be in a straight line, so they can move to the corner of the object then use their remaining distance to move around it, which is why it should show a blobbing effect.

Anyway, if that wasn't clear enough, this is what the effect should look like: (yes, I drew this in paint lol)

Red: Character
Green: Movement Area
Black: Obstacle
Left shows the obstacle completely out of the way, middle shows the obstacle partially in the way, and right shows the obstacle completely in the way with the "blob" effect.

Assuming I have a 2D array of pixel perfect collision information, how would I generate this effect? Or maybe I should abandon this method and go about it some completely different way?

##### Share on other sites
You could use something similar to Dijkstra's algorithm. Except instead of looking for a goal, you're just marking pixels as valid move points as long as the movement cost hasn't become too high. Once there are no more pixels left with a low-enough movement cost, you can stop the pathfinding, and all valid pixels should've been found.

##### Share on other sites

You could use something similar to Dijkstra's algorithm. Except instead of looking for a goal, you're just marking pixels as valid move points as long as the movement cost hasn't become too high. Once there are no more pixels left with a low-enough movement cost, you can stop the pathfinding, and all valid pixels should've been found.

Do you think Dijkstra's could work at a reasonable speed? The game is designed for the iPad and whatever method I use for this needs to be fairly efficient.

Edit: I've also heard A* might potentially be useful for this, but just wanted some other opinions.

##### Share on other sites

[quote name='Cornstalks' timestamp='1328030007' post='4908073']
You could use something similar to Dijkstra's algorithm. Except instead of looking for a goal, you're just marking pixels as valid move points as long as the movement cost hasn't become too high. Once there are no more pixels left with a low-enough movement cost, you can stop the pathfinding, and all valid pixels should've been found.

Do you think Dijkstra's could work at a reasonable speed? The game is designed for the iPad and whatever method I use for this needs to be fairly efficient.
[/quote]
Dijkstra's algorithm should be fast enough. You aren't searching the whole map (you're only searching a small circular area within the map). It may, however, require some clever optimizations (not low-level optimizations; I mean high-level optimizations). I don't know how much processing your game already does, so I can't say for sure.

Edit: I've also heard A* might potentially be useful for this, but just wanted some other opinions.

A* is very similar to Dijkstra's algorithm, except A* introduces a heuristic to help it guess which nodes to search and which ones to ignore. There are two problems with A* in this case: 1) I can't imagine how you'd come up with a heuristic, because the heuristic depends on you having a specific destination (whereas you don't have any specific destination), and 2) A* just tries to weed out unnecessary checks with the heuristic, but you aren't trying to weed any out (and if it did weed any out you'd end up with gaps in your movable region)

##### Share on other sites

A* is very similar to Dijkstra's algorithm, except A* introduces a heuristic to help it guess which nodes to search and which ones to ignore. There are two problems with A* in this case: 1) I can't imagine how you'd come up with a heuristic, because the heuristic depends on you having a specific destination (whereas you don't have any specific destination), and 2) A* just tries to weed out unnecessary checks with the heuristic, but you aren't trying to weed any out (and if it did weed any out you'd end up with gaps in your movable region)

Hmm okay, this makes sense. Alright, maybe I'll go with Dijkstra's and see where that gets me. Thanks for the help!

##### Share on other sites
I am unable to see the pictures, but here is the solution i used for my game, its something in between the A* and Dijkstra's. I might still work on it a bit more, but so far it works like a charm.

In my case:
each character has a max travel cost
each map point is aware of its neighbors and cost of travel between it and it`s neighbor
i already have A* implemented to find a path, used mainly by NPCs.
Each map point stores the point that was used to access it.

When a move command is selected for a player, i get all neighbors from point, and check to insure that the move cost from current point, to next point
does not exceed characters max travel cost.
I more or less repeat the process untill I have checked all available points.

Now when a player clicks one of the available map points, I already have the best path to that point stored in memory that can be used to get the character to the desired point.

Sorry if my explenation is not clear, but i hope it helps.

##### Share on other sites
What you want is to try and see if the player can move to each pixel in a radius around his current location (ie, each pixel on the outside of your green circle) within a certain number of "steps" (steps being the radius length in pixels, for example). Depending on the size of the circle, if you run Dijkstra's, with the end vertex's being each pixel, it could become costly. But, if your circle is small, then maybe it will be OK.

I wonder, if you did use A*, and set the goal to be each pixel, it could work around the obstacle fine, and then you could use the resulting path, only up to X amount of steps. I don't know how much that would cost either, but it's something to consider.

Or, you could just set the goal to be every 5 Degrees around the circle, that would probably be a good approximation, so you don't check every pixel. I'm just brain-storming here, I've never done this before.

##### Share on other sites

What you want is to try and see if the player can move to each pixel in a radius around his current location (ie, each pixel on the outside of your green circle) within a certain number of "steps" (steps being the radius length in pixels, for example). Depending on the size of the circle, if you run Dijkstra's, with the end vertex's being each pixel, it could become costly. But, if your circle is small, then maybe it will be OK.

I wonder, if you did use A*, and set the goal to be each pixel, it could work around the obstacle fine, and then you could use the resulting path, only up to X amount of steps. I don't know how much that would cost either, but it's something to consider.

Or, you could just set the goal to be every 5 Degrees around the circle, that would probably be a good approximation, so you don't check every pixel. I'm just brain-storming here, I've never done this before.

That's too costly. Running Dijkstra's algorithm as I mentioned would be more efficient (running until a certain move-cost had been reached, rather than a specific end goal). Searching hundreds of times for the perimeter of a circle is expensive.

##### Share on other sites

[quote name='BeerNutts' timestamp='1328126682' post='4908445']
What you want is to try and see if the player can move to each pixel in a radius around his current location (ie, each pixel on the outside of your green circle) within a certain number of "steps" (steps being the radius length in pixels, for example). Depending on the size of the circle, if you run Dijkstra's, with the end vertex's being each pixel, it could become costly. But, if your circle is small, then maybe it will be OK.

I wonder, if you did use A*, and set the goal to be each pixel, it could work around the obstacle fine, and then you could use the resulting path, only up to X amount of steps. I don't know how much that would cost either, but it's something to consider.

Or, you could just set the goal to be every 5 Degrees around the circle, that would probably be a good approximation, so you don't check every pixel. I'm just brain-storming here, I've never done this before.

That's too costly. Running Dijkstra's algorithm as I mentioned would be more efficient (running until a certain move-cost had been reached, rather than a specific end goal). Searching hundreds of times for the perimeter of a circle is expensive.
[/quote]

I don't understand how you would run Dijkstra's without having every pixel on the outer edge being a possible vertex destination. Admittedly, I've only ever used A*, but my understanding of Dijkstra is it still has multiple vertex nodes and can find shortest path to all of them. How do you do use dijkstra's without these nodes?

##### Share on other sites

[quote name='Cornstalks' timestamp='1328127784' post='4908453']
[quote name='BeerNutts' timestamp='1328126682' post='4908445']
What you want is to try and see if the player can move to each pixel in a radius around his current location (ie, each pixel on the outside of your green circle) within a certain number of "steps" (steps being the radius length in pixels, for example). Depending on the size of the circle, if you run Dijkstra's, with the end vertex's being each pixel, it could become costly. But, if your circle is small, then maybe it will be OK.

I wonder, if you did use A*, and set the goal to be each pixel, it could work around the obstacle fine, and then you could use the resulting path, only up to X amount of steps. I don't know how much that would cost either, but it's something to consider.

Or, you could just set the goal to be every 5 Degrees around the circle, that would probably be a good approximation, so you don't check every pixel. I'm just brain-storming here, I've never done this before.

That's too costly. Running Dijkstra's algorithm as I mentioned would be more efficient (running until a certain move-cost had been reached, rather than a specific end goal). Searching hundreds of times for the perimeter of a circle is expensive.
[/quote]

I don't understand how you would run Dijkstra's without having every pixel on the outer edge being a possible vertex destination. Admittedly, I've only ever used A*, but my understanding of Dijkstra is it still has multiple vertex nodes and can find shortest path to all of them. How do you do use dijkstra's without these nodes?
[/quote]

Dijkstra's algorithm just floods out from a starting point until it reaches the end destination. At least that's how it's normally done. You just keep flooding out until you reach the end (and once you reach the end you stop the algorithm, as you've found the shortest path). However, you can slightly modify the algorithm so that instead of stopping the search when a particular point is reached, you can stop the search once all the nodes left to search exceed the maximum movement cost. You don't need to really need to have an "end node" when you run it like this.

The only reason you have a destination node in Dijkstra's algorithm is so you know when you've found the goal and you should stop. You don't need a destination node to run the movement-cost calculation part of the algorithm (unlike A*, which needs the destination node for its heuristic).

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 11
• 23
• 42
• 75