View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Pathing Algorithm Help

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1slapshot6819  Members

Posted 31 January 2012 - 10:55 AM

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?

### #2Cornstalks  Members

Posted 31 January 2012 - 11:13 AM

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.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

### #3slapshot6819  Members

Posted 31 January 2012 - 02:28 PM

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.

### #4Cornstalks  Members

Posted 31 January 2012 - 02:59 PM

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.

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)
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

### #5slapshot6819  Members

Posted 31 January 2012 - 03:46 PM

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!

### #6reisama  Members

Posted 01 February 2012 - 11:26 AM

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.

### #7BeerNutts  Members

Posted 01 February 2012 - 02:04 PM

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.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #8Cornstalks  Members

Posted 01 February 2012 - 02:23 PM

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.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

### #9BeerNutts  Members

Posted 01 February 2012 - 02:53 PM

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.

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?
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #10Cornstalks  Members

Posted 01 February 2012 - 03:01 PM

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.

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?

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).
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

### #11reisama  Members

Posted 01 February 2012 - 03:04 PM

If i remember corectly with Dijkstra's, you let it search all all possible nodes untill it find the node you are looking for, all A* does is adds an estimation to that cost to direct the search in 1 direction.

When running a Dijkstra's algorithm, you expend all nods equaly untill you find desired destination, so as long as you emmit the destination and let it search all nodes with a cost value less then max, it will work without any problems

### #12BeerNutts  Members

Posted 01 February 2012 - 03:09 PM

OK, I see, yes, that sounds like it would work well, using # of steps instead of destination location.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.