寻路 2D Java 游戏?
我目前正在编写一个非常基本的 Java 游戏,基于主题医院< /em>.
我对 Java 还很陌生,目前正在大学学习第一年。 我已经断断续续地使用 Java 近两年了,但我终于把时间投入到了一个像样的项目上。
我正处于需要创建一个人(患者)入院的阶段。 他们需要去接待台,然后去全科医生办公室,然后回到他们的起始位置。
我研究过 A* 寻路,但对我来说似乎真的很复杂。 我认为我理解它是如何工作的,但不确定如何将其应用到我的游戏中。
至此,用户可以放置一个接待台,并建立一个全科医生的办公室。 每一个都有一个“使用点”,这将是患者必须到达的地方。 网格方格只能是满的或不满的,不会有不同的地形。
我还在犹豫要不要粘贴任何代码,因为它很混乱,因为在过去的几个月里我学到了很多与 GUI 相关的新技术。 我的计划是达到里程碑 1,让患者先去办公桌,然后去办公室,然后离开。 一旦有了这个,我就会进一步整理代码。
我见过许多 A* 的实现和许多不同的类型。 有人可以给我一个可以合作的起点吗? 我应该尝试改编一组已经写好的类,还是尝试从头开始编写自己的类?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您确实需要 A*,它是基于网格的寻路的最佳实现。
这可能会帮助您:
http://www.cokeandcode.com/main/tutorials/ path-finding/
编辑: 前面的链接既可以作为可实现的类集,也可以作为自定义路径查找方法以满足您的满意的指南。
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.
如果您编写自己的实现,您自然会学到很多有关寻路的知识。 但你也会花很多时间来做这件事。
查看 JGraphT 库,它通常处理图形,有一个很好的 API 并支持 更多最短路径算法,而不仅仅是 A*。
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*.
这是迄今为止我见过的信息最丰富的寻路帖子:http://www.ai-blog.net/archives/000152.html ai-blog.net/archives/000152.html
This is the most informative pathfinding post I've seen to date: http://www.ai-blog.net/archives/000152.html
AI for Game Developers 这本书对 A* 有很好的解释。 我今天实际上打算编写一个实现......如果我这样做,我会将代码放在这里。
代码完成了,它太大了,放在这里,所以你可以从以下位置获取它: https ://chaos.bcit.ca/svn/public/astar/(自签名证书,但服务器不会做任何邪恶的事情)。
我大部分时间都遵循书中的伪代码,但我使所有内容都比迄今为止我在 A* 中看到的任何内容都更加面向对象。
您有一个由瓷砖组成的迷宫。 每个图块都有一个位置和一个障碍物(如果没有障碍物则为空)。
您可以使用 PathFinder(如 AStar)来查找给定起始位置和结束位置之间的最短路径。 您将获得一条返回路径,其中包括从起点到终点所需经过的图块。
您可以通过提供不同的启发式计算器来更改启发式计算(当前的启发式计算器只是检查是否存在障碍物,并计算出要经过的最短数量的图块,例如,如果您不这样做,您可以为不同的障碍物添加权重)不喜欢默认的)。
该代码是 LGPL 下的许可证,因此如果您进行更改并分发应用程序,则必须使更改可用。 请随意将错误报告/修复发送到许可证注释中的电子邮件地址(在每个标题中找到)。
我会(从来没有)在考试后抽出时间来评论它,但我认为这是非常简单的。
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.
也许您找到了想要的东西,但这里有一个链接,其中包含 A* 寻路的详细解释。 我必须在 C++ 中为我的游戏实现 A*,它对我有帮助很多了解它是如何工作的。
http://www.abdn.ac.uk/~ csc245/教学/CS1015/practicals/aStarTutorial.htm
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