Pathfinding 2D Java game?

戏子无情 提交于 2019-11-27 20:29:28

问题


I'm currently writing a very basic Java game based on the idea of Theme Hospital.

I'm quite new to Java and am currently studying at the university my first year. I have done Java for nearly two years now on and off, but I'm finally devoting my time to a decent project.

I'm at the stage where I need to create a person (patient) to be admitted to the hospital. They need to go to the reception desk, then GP's office, and then back to their starting position.

I have looked into A* path finding, but it seems really complicated to me. I understand how it works I think, but am unsure how to implement it into my game.

So far, the user can place a reception desk, and build a GP's office. Each of these has a "point of usage" which will be the place the patient has to get to. The grid squares can only be full or not, there will be no different terrain.

I'm hesitant to paste any code yet, as it's messy as I've learn a lot of new techniques to do with GUI in the past few months. My plan is to get to the milestone 1, making the patient go to the desk then the office and then leave. Once I have this, I will tidy up the code more.

I've seen many implementations of A* and many different types. Can someone give me a starting point I can work with? Should I try and adapt an already written set of classes, or try to write my own from the scratch?


回答1:


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.




回答2:


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




回答3:


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*.




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/735523/pathfinding-2d-java-game

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