nanodeath / CrowLib

Crow: Find your shortest path!
http://www.effectgames.com/effect/article.psp.html/community/Pathfinding_library_Dijkstra_s_Algorithm
MIT License
40 stars 6 forks source link

hex grids #10

Closed cidic closed 12 years ago

cidic commented 13 years ago

would this be a good choice for pathfinding on a hex grid?

nanodeath commented 13 years ago

Hi cidic,

Yeah, this should work fine for hex grids as well -- the main thing that differs between a square grid and a hex grid is the number of neighboring cells each cell has. Right now, there is no built-in implementation of hex grids, but it's trivial to add (designed that way!). If you look at the Quick Start Recipes under I want to find the shortest distance between 10 points on a grid, you'll see mention of creating a class that extends crow.GridNode. This is what you'd do if you had a square grid; however, since you don't, you'll want to instead subclass crow.Node and implement the "abstract" methods in that class -- you can follow GridNode as an example. Do note you'll have to compile CrowLib yourself -- I don't think you'll be able to make use of the prebuilt minified library. The documentation on this particular task is admittedly not great, so I'll give you a couple pointers here:

1) getNeighbors does exactly what it sounds like -- returns an array of adjacent cells. It accepts a graph, and you call graph.getNode on it as much as you need to populate the array. 2) distanceToNeighbor returns the actual distance between two adjacent cells. 3) distanceToGoal returns the estimated distance to the goal; doesn't have to be 100% accurate, but know that the algorithms will often be trying to minimize this value in order to make progress towards the goal.

Let me know if you have other questions.