A* Pathfinding (LibGDX)

Release: March 2013

Genre: Code Example

Made with: LibGDX (Java)

Compatible with: PC

View Source


So the basic story of this one is that a colleague of mine who also studies games programming was trying to work on his own A* Implementation as well, but was struggling on parts of the algorithm. So I figured I’d try myself, since I too haven’t attempted to write my own version for some time. My first attempt at it was back in first year University, and due to the poor resources I ended up giving up. It’s also very worthwhile that I at least get a working implementation done since Secret of Escape also uses pathfinding for some of the enemies.

After about two days of looking through online resources (I highly recommend anyone interesting in building their own to read this article) and help from some very helpful folks on Twitter, I successfully got a working implementation!

The source code is up online (click the view source link), the application was built using LibGDX but with a bit of tweaking the pathfinder and node classes could be used for other Java based engines. The classes are commented with all the steps for clarity.


5 thoughts on “A* Pathfinding (LibGDX)

    • If you go into PathFinder.java and look at the function SetOpenList, it checks all eight possible directions. If you want to ignore diagonal paths, you would instead do four directions (Up, Down, Left, Right) and ignore any that check two boolean if statements (e.g. if (ignoreUp & ignoreLeft)).

    • In GameState.java I set the nodes with the PathFinder’s SetGridNode function:

      pathFinder.SetGridNode(screenX, screenY, GridNode.GridType.START);
      pathFinder.SetGridNode(screenX, screenY, GridNode.GridType.END);

      Where screenX and screenY are X and Y coordinates on the screen, should note that at this point it’s only limited to the size of the screen.

      Hope that helps. 🙂

      • Thank you very much for your help, it’s appreciated. Now to add a background image instead of the white background. 🙂

Leave a Reply to gamepopper Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s