I've played around with this a bit in the past, and found the following to work fairly well:
1) Make a 2D map of the 'walkable' parts of the map, drawn on top of the real background picture 2) Scale down the map to 1/4 or 1/8 of the actual resolution (or however little accuracy you can put up with) -- this means a significantly smaller map to parse, and therefore much less overhead finding an optimal path 3) Store the 2D map in an array 4) Run the a* against the array
something else to think about, is what to do when you click on a part of the map that is unreachable. Here's what I did:
If you determine the destination to be unreachable, calculate a direct line between the source and destination point. Starting at the destination point, do an A* back to the starting point. If it fails, move 32 pixels (or something) closer to the start on the line and calculate again. Repeat until you get a solution. When you have a solution, let your character walk this path in reverse -- essentially this will make him walk towards your desired destination point, but stop at the edge of the walkable area, where he'll be blocked. Looks much better than refusing to walk at all if there is no full path available.
You can blend walking maps with waypoints as well, to easily add/remove barriers programmatically. For example, you have to cross a specific waypoint to walk across a drawbridge. You can disable the waypoint when the bridge is open, and re-enable it again when closed.
|