Pathfinding 2D Java game?

ⅰ亾dé卋堺 提交于 2019-11-28 18:54:11
FlySwat

You do want A*, it is the optimal implementation for grid based pathfinding.

This might help you out:

http://www.cokeandcode.com/main/tutorials/path-finding/

EDIT: The preceeding link is good for both as an implementable set of classes and as a guide for customizing the path finding methods to meet your satisfaction.

Andrew Turner

This is the most informative pathfinding post I've seen to date: http://www.ai-blog.net/archives/000152.html

Naturally you will learn a lot about pathfinding if you write your own implementation. But you will also spend a lot of time doing it.

Check out the JGraphT library that deals with graphs in general, has a nice API and supports more shortest path algorithms than just A*.

The book AI for Game Developers has a very good explanation of A*. I was actually going to write an implementation today... if I do I'll throw the code up here.

The code is done, it is too big to put here, so you can grab it from: https://chaos.bcit.ca/svn/public/astar/ (self signed certificate, but the server doesn't do anything evil).

I followed the pseudo-code in the book for the most part, but I made everything much more object oriented than anything I have seen thus far for A*.

You have a Maze that consists of Tiles. Each Tile has a Location and an Obstacle (null if there is no obstacle).

You can use a PathFinder (like AStar) to find th shortest path between a given start and end location. You get a Path back which includes the Tiles you need to go through to get from the start to the end.

You can change the heuristic calculation by providing a different HeuristicCalculator (the current one just checks to see if there is an Obstacle or not and figures out the shortest number of Tiles to go through, you could add weights to different Obstacles for instance if you don't like the default).

The code is license under the LGPL, so if you make changes and distribute the app you have to make the changes available. Feel free to send bug reports/fixes to the email address in the license comment (found in each header).

I'll (never did) get around to commenting it after exams, but I think it is pretty straight forward.

Anila

Maybe you found what you wanted, but here's a link with a nice explanation of A* pathfinding. I had to implement A* for my game in C++, and it helped me a lot to understand how it works.

http://www.abdn.ac.uk/~csc245/teaching/CS1015/practicals/aStarTutorial.htm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!