PathFinding.js
qiao.github.io
qiao.github.io
http://www.redblobgames.com/pathfinding/a-star/introduction....
You sorta need to use other tools with A* in order to fully utilise it. Steering forces, behaviour trees an etc :)
With a bit of tweaking, I imagine this could be turned into a little game.
Also, a "random maze" button would be nice.
Though you can treat it as effectively O(1) time and O(n) space.
Or rather, yes, sort of, but you only need to do it once (or once per update on a changing grid):
You find the connected components of the grid. This is easy (loop over the entire grid. Any grid cell that has not been assigned a CC, assign the first free CC and floodfill from there.) Then to check if a is reachable from b, just check that a and b are in the same connected component. This is O(1) time (albeit with O(n log n) worst case spacial complexity for the CC structure required).
The problem with the naive version of this is that updates can, in the worst case, require a scan and update of arbitrarily close to the full grid. There are better versions for when updates are frequent, but they take longer to query (although still sublinear time!) and are substantially more complex.
Preprocessing is cheating :). If you preprocess and floodfill the map anyway, you can go one step further, do Dijkstra, save the results using a trick, and you don't need run-time pathfinding at all.
And you can store full paths from each cell to each cell using just O(N * N) memory, no matter the path lengths - that's a neat trick BTW:
you store for each cell pair (P,Q) what is the next cell you should go from P if you want to eventually get to Q. that allows yu to reconstruct full path between each pair of cells quickly.
EDIT to clarify - cheating is good, it's just I don't think O(N log N) is good tradeoff for the ability to return quicker, if you can do away with the whole pathfinding for O(N * N).
EDIT 2: why O(N log N)? It seems you could write the resulting component for each cell, so worst case O(N) space?
In practice you can treat it as O(1) time and O(n) space, though.
Also, that preprocessing with result saving is substantially slower than the connectivity alone. The connectivity alone is O(n) / O(n log n) preprocessing time - you check each cell a maximum of 5 / 9 times (depending on 4 or 8 way connectivity).
Also, that preprocessing with result saving fails miserably when you do updates to the graph.
So it is O(N) in space?
I'm not sure we're talking about the same thing. I mean space requirements for the results of preprocessing.
Preprocessign itself may require computing cluster for all I care.
We are.
> But you can just write for each cell the index of component it belongs to. It can't belong to many, right?
Right. Each cell belongs to 1 and only 1 connected component.
However, there can be O(n) connected components overall. Think of a grid of cells all blocked off from each other, for instance.
As such, storing which connected component a cell belongs to technically requires O(log n) space, worst-case. Which means have overall you have n cells each requiring O(log n) space, or O(n log n) space overall.
In practice you can treat this as effectively O(n), however.
=========== ================
s= =e
=========== ================
I think if you searched from both ends, it would perform much better.Now I'm just waiting for a library/API for hexagon map's ;)
You can use A* and co with hex maps too. There's more info on hex maps and an illustration of pathing here:
That aside, this is really neat, and it's interesting to see a visual representation of the different algorithms.
So...somewhat relevant to what you're asking.
Full USA map graph contains around 24 million nodes, 58 million edges. Your regular A* just doesn't cut it.
As for the scale issue: most of these nodes and edges will never change, so it might be possible to calculate a Dijkstra map.
(I've not implemented any of these myself)
In the implementation, you would just need to modify how the algorithm transverses each valid walkable/unwalkable node (how to go from waypoint to waypoint) and possibly how much extra cost each node has (water/mountainous terrain could have increased cost for example) instead of letting it go the usual up/down/left/right and possibly diagonals for grids.
If the waypoint based approach isn't ideal for you, then you should look up "navigation mesh" [1] pathing which is a bit trickier to implement but works well when you have a bunch of large empty/walkable areas.
And there seems to be "emscripten" version of it (just googled it) - https://github.com/vincent/three-arena/tree/master/recastnav... - as part of the "three-arena" project
demo here - http://three-arena.com/examples/#simplest.js
and the compiled emacsen source here - http://three-arena.com/node_modules/recastjs/lib/recast.js
also this one:
https://github.com/vincent/recast.js found by looking here - https://groups.google.com/forum/#!searchin/recastnavigation/...
Also test from there: