14

I'm currently writing a 2D game in Javascript using the HTML5 <canvas> element. It's coming along very nicely, but i have run into a problem.

The level design for my game is a grid (so path cost moving from one cell to the north/south/east/west cell is 1) with various obstacles occupying various locations in the grid – a lot like a maze, but with a lot more wiggle room. Each individual level is on the order of 400 × 200 cells.

I'm trying to implement an enemy that will seek out the player no matter where they might be, but i'm having trouble trying to translate one of the various path-finding algorithms to fit my situation. Most of the ones i've come across (like A* and Dijkstra) seem to be best suited to 3D or much more complicated 2D situations. I was wondering if it is possible to dramatically simplify these algorithms to better suit my purposes, or if something like the depth-first search would be a more efficient alternative given the level size.

creXALBO
  • 307
  • 1
  • 3
  • 13
  • You won't do better than A* without giving it some kind of pre-knowledge about the possible paths (like dividing your map into segments with known connectivity). Depth first would be much (much much much) slower than breadth first. – Dave Jun 16 '13 at 00:00
  • This isn't really what Stackoverflow/Stack Exchange is about; ask a question with a definite answer, not a shopping question. By all means, use what people suggest, but come back when you hit a snag with a programming problem. – Amelia Jun 16 '13 at 00:01
  • @Dave: Well, you can easily [find better than plain-ol' A\*](http://programmers.stackexchange.com/questions/197894) when constrained to a grid; but, JPS+A\* is more complicated than just A\*, and usually not necessary. – BlueRaja - Danny Pflughoeft Jun 16 '13 at 04:35

3 Answers3

14

A* is a very common 2D pathfinding algorithm. It might take a little time to wrap your head around what's happening if pathfinding is unfamiliar, but it's not terribly complex. You may just be looking at someone else's example code that's been developed for a more complex application than you intend. There's a good tutorial for understanding the algorithm here.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Skrivener
  • 1,003
  • 6
  • 11
8

Check this out https://github.com/qiao/PathFinding.js It has a lot of demos and path finding algothms.

Rezigned
  • 4,901
  • 1
  • 20
  • 18
3

EasyStar.js is a nice looking library that appears to do what you'd like to. I haven't used it myself, but the documentation on the project's github page looks pretty good, and it's probably what I would choose in your position.

clone45
  • 8,952
  • 6
  • 35
  • 43
TwentyMiles
  • 4,063
  • 3
  • 30
  • 37