coderbanna
coderbanna

Reputation: 123

path finding in a simple grid, no obstacles

I want a simple algorithm in As3 that finds the shortest path in a simple grid, i need complete path not the only both points or the length.

check image here: http://screencast.com/t/GmR5YaO4L

so i need all the square numbers of the grid that comes in the path from A to B.

Upvotes: 1

Views: 2019

Answers (1)

jhocking
jhocking

Reputation: 5577

Although A* is kind of overkill when there are no obstacles, you might as well use it since I wrote a library for it in AS3: http://www.newarteest.com/flash/astar.html


EDIT: If you are anything less than 100% sure the pathfinding will never have obstacles then I still recommend A* because that gives you the most flexibility, but in your specific case it's pretty easy to find the path without obstacles because you only move once horizontally then vertically (or the other way around) as opposed to diagonally.

First subtract the x and y components of the positions to determine which way is longer and start moving in that direction. Then when the objects are in the same position on that axis, switch to moving in the other direction.

Depending on your needs you can easily build a loop or recursive function that finds the entire path before you traverse it, but it may be simpler to move the object in the correct direction without knowing the entire path ahead of time. While finding the path you need to loop through all the nodes anyway, so you could avoid having to loop through the nodes twice.

Upvotes: 3

Related Questions