探路 2D Java 游戏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/735523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:34:41  来源:igfitidea点击:

Pathfinding 2D Java game?

javapathpath-finding

提问by Relequestual

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

我目前正在基于Theme Hospital的想法编写一个非常基本的 Java 游戏。

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.

我对 Java 很陌生,目前正在大学学习第一年。我已经断断续续地研究 Java 近两年了,但我终于把时间花在了一个不错的项目上。

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.

我正处于需要创建一个人(患者)入院的阶段。他们需要去接待处,然后是 GP 的办公室,然后回到他们的起始位置。

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.

我已经研究过 A* 寻路,但对我来说似乎很复杂。我明白我认为它是如何工作的,但不确定如何将它实施到我的游戏中。

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.

到目前为止,用户可以放置一个接待台,并建立一个GP的办公室。每一个都有一个“使用点”,这将是患者必须到达的地方。格子方格只能填满或不填满,不会有不同的地形。

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.

我还犹豫要不要粘贴任何代码,因为它很混乱,因为我在过去几个月中学习了很多与 GUI 相关的新技术。我的计划是达到里程碑 1,让患者先去办公桌,然后去办公室,然后离开。一旦我有了这个,我会更多地整理代码。

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?

我见过许多 A* 和许多不同类型的实现。有人可以给我一个可以与之合作的起点吗?我应该尝试改编一组已经编写好的类,还是尝试从头开始编写自己的类?

采纳答案by FlySwat

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

您确实需要 A*,它是基于网格的寻路的最佳实现。

This might help you out:

这可能会帮助你:

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

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.

编辑:前面的链接既适合作为一组可实现的类,也适合作为自定义路径查找方法以满足您的满意度的指南。

回答by Ande Turner

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

这是迄今为止我见过的最翔实的寻路帖子:http: //www.ai-blog.net/archives/000152.html

回答by TofuBeer

The book AI for Game Developershas 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.

AI for Game Developers这本书对 A* 有很好的解释。我今天实际上打算写一个实现……如果我这样做了,我会把代码扔在这里。

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

代码搞定了,放在这里太大了,大家可以从:https: //chaos.bcit.ca/svn/public/astar/(自签名证书,但服务器不作恶)。

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

我大部分时间都遵循了书中的伪代码,但我使所有内容都比迄今为止我所看到的 A* 内容更加面向对象。

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

你有一个由瓷砖组成的迷宫。每个图块都有一个位置和一个障碍物(如果没有障碍物则为 null)。

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.

您可以使用路径查找器(如 AStar)来查找给定开始和结束位置之间的最短路径。您会得到一条返回路径,其中包括从头到尾需要经过的瓷砖。

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

您可以通过提供不同的 HeuristicCalculator 来更改启发式计算(当前的只是检查是否有障碍物并计算出最短的瓷砖数量,例如,如果您不这样做,您可以为不同的障碍物添加权重不喜欢默认)。

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

该代码是 LGPL 下的许可证,因此如果您进行更改并分发应用程序,则必须使更改可用。随意将错误报告/修复发送到许可证评论中的电子邮件地址(在每个标题中找到)。

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

我会(从来没有)在考试后评论它,但我认为这是非常直接的。

回答by Tomas Andrle

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 JGraphTlibrary that deals with graphs in general, has a nice API and supports moreshortest path algorithms than just A*.

查看处理一般图形的JGraphT库,它有一个很好的 API 并且支持比 A*更多的最短路径算法。

回答by 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.

也许你找到了你想要的东西,但这里有一个很好的解释 A* 寻路的链接。我必须在C++ 中为我的游戏实现 A* ,这对我理解它的工作原理有很大帮助。

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

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