简体   繁体   English

寻路2D Java游戏的其他问题

[英]Pathfinding 2d java game further issues

I asked a question some time ago on java 2d pathfinding... Pathfinding 2D Java game? 我前段时间在java 2d pathfinding上问了一个问题

The game im developing is based on the idea of theme hospital. 我正在开发的游戏基于主题医院的理念。 The chosen answer from my question, A* pathfinding, the link was awesome, and very helpful. 从我的问题中选择的答案是A *寻路,链接很棒,而且非常有帮助。 I'm eventually getting to implement this into my game, however I have some further questions/issues regarding it. 我最终要在游戏中实现这一点,但是我对此还有其他疑问/问题。

In my game, the map will change. 在我的游戏中,地图将会改变。 The tutorial assumes that the map is static (I think). 本教程假定地图是静态的(我认为)。 I have been looking at the code, and as far as I can work out, I just need to create a method to call to update the game map in the pathfinding code. 我一直在看代码,据我所知,我只需要创建一种方法来调用即可在寻路代码中更新游戏地图。

Second, I see the class GameMap. 其次,我看到了GameMap类。 I have my own class called Board which houses all the tiles. 我有自己的班级,叫做Board,可以容纳所有瓷砖。 I believe I could integrate the methods on GameMap into my Board class. 我相信我可以将GameMap上的方法集成到我的Board类中。 Right? 对?

Third, I have been working on the reasoning that any rooms would be counted as blocked. 第三,我一直在进行推理,将所有房间都视为不通房间。 I mean as in, any squares the room is covering are counted as blocked. 我的意思是,房间覆盖的任何正方形都算作遮挡。 I was thinking ahead to where the people will enter the rooms. 我在想人们进入房间的方向。 They will then have to move around these rooms to get to various places. 然后,他们将不得不在这些房间中移动以到达各个地方。 I was thinking I would just invert the Blocked boolean for each square, but this would not work for 2 reasons. 我以为我只是将每个正方形的Blocked布尔值取反,但这有两个原因。 1, rooms could have ajoining walls, and potentially muck up pathfinding. 1,房间可能有相连的墙,并可能破坏寻路。 2, if the blocked status is simply inverted, then any solid items in the room would be seen as not solid when inverted back, which could cause problems when they are touching a wall. 如图2所示,如果将阻塞状态简单地倒转,则房间中的所有固体物品在倒转时都将被视为不牢固,这可能会在它们碰到墙壁时引起问题。

Thinking about it, it would be better if you could make sides of a square blocked rather than actual whole squares. 考虑一下,如果您可以将正方形的边遮住而不是实际的整个正方形会更好。 This must be possible, but I am just getting by using the tutorial in the previous question, and not sure if I should try and change A* to do this, or work on workarounds for the room items issue. 这必须是可能的,但是我只是通过使用上一个问题中的教程来获得,并且不确定是否应该尝试更改A *来执行此操作,或者解决客房项目问题的解决方法。

Any thoughts or suggestions on any of these issues? 对这些问题有任何想法或建议吗? I'm implementing the simple path finding today, but just thinking ahead of myself. 今天,我正在执行简单的路径查找,但是要先思考一下。

From a quick look, it looks like the isValidLocation(mover,sx,sy,xp,yp) method defines if moving from point(sx,sy) to point(xp,yp) is a valid move. 快速浏览一下,看起来isValidLocation(mover,sx,sy,xp,yp)方法定义了从point(sx,sy)移至point(xp,yp)是否有效。

If this method took into consideration the direction of the move, you could block specific directions out of / into a block without making the block entirely impenetrable. 如果此方法考虑了移动的方向,则可以将特定的方向从一个块中划入一个块,而不必使该块完全不可穿透。 This way, you could have 2 accessible blocks next to each other with a solid boundary between them. 这样,您可以将2个可访问块彼此相邻,并在它们之间建立一个牢固的边界。

This approach has some interesting side-effects such as the ability to create one-way boundaries (block A has access to block B, but not vice versa.) Could be a useful game mechanic in letting the A* take one way doors (fire escapes?) into account. 这种方法具有一些有趣的副作用,例如能够创建单向边界(A块可以访问B块,反之亦然。)可能是使A *采取单向门(射击)的有用的游戏机制。转义?)。

There is an algorithm called Adaptive A* which can recalculate a portion of a path say, if someone stands in front of you. 如果有人站在您面前,则有一种称为Adaptive A *的算法可以重新计算路径的一部分。 I would concentrate on the vanilla A* first, you could always calculate a new path from that point on if you found half way through that a previously valid path was blocked. 我将首先关注原始A *,如果您发现之前有效的路径被阻塞了一半,那么您始终可以从该点开始计算一条新路径。

This looks like interesting reading: Real-Time Adaptive A* [PDF] 看起来很有趣: 实时自适应A * [PDF]

If the game map changes you do need to recalculate paths, however you don't necessarily need to recalculate all paths depending on what changed. 如果游戏地图发生变化,则确实需要重新计算路径,但是不一定需要根据更改的内容重新计算所有路径。

You should integrate the methods of GameMap into your Board class (with modifications to the GameMap class). 您应该将GameMap的方法集成到Board类中(对GameMap类进行修改)。

To block sides of a square you could think of each tile as nine separate ones instead (3X3). 要遮盖正方形的边,您可以将每个图块视为9个单独的图块(3X3)。 For example for a tile with blocked horizontal walls, instead of a single square you can represent the tile (to your a* algorithm) as: 例如,对于水平墙被遮挡的瓷砖,您可以将瓷砖(对于a *算法)表示为单个瓷砖,而不是单个正方形:

[X| |X]
[X| |X]
[X| |X]

A tile with a vertical and horizontal tile blocked: 一块具有垂直和水平砖块的砖块:

[ | |X]
[ | |X]
[X|X|X]

You would have to store the additional edge information with your game map. 您将必须在游戏地图中存储其他边缘信息。 Hope this helps. 希望这可以帮助。

For the path question : 对于路径问题:

A simple solution is to recalculate the path if and only if the next move in the current path is considered as invalid (a new element has been put on the map, a new room has been added, a door has been moved...). 一个简单的解决方案是,当且仅当当前路径中的下一个移动被视为无效时才重新计算路径(在地图上放置了新元素,添加了新房间,并移动了门...) 。 Of course, you recalculate from the current position. 当然,您需要从当前位置重新计算。 The problem is more complex if the blocking element is another moving one. 如果阻塞元件是另一个移动的元件,则问题会更加复杂。 In this case, one of the two objet has to wait a few cycle, and the other has to re-path, depending on a priority. 在这种情况下,根据优先级,两个对象之一必须等待几个周期,而另一个必须重新设置路径。 however, this may lead to problems with multiple path collision (two high priority objec each one one side of the door, and a low priority object in the door : it can not move, and the high priority will wait a long time) 但是,这可能会导致多路径冲突的问题(两个高优先级对象分别位于门的一侧,而低优先级对象在门中:它不能移动,并且高优先级将等待很长时间)

For the room problem : 对于房间问题:

It is common to define a room as a set of tiles instead of one tile. 通常将房间定义为一组图块而不是一个图块。 Thus, you can define for one room, the subtiles are passable, and those that are not. 因此,您可以为一个房间定义子瓷砖是可通过的,而那些不是。 if your model allows it, you can even describe the objets that are present one the different tiles and set them as impassable : the waiting room ( a 6 tiles x 4 tiles room) if fully passable, but contains a set of chairs and a small water fountain that render some subtiles impassables. 如果模型允许,您甚至可以描述存在于不同瓷砖上的对象,并将其设置为不可通过:候诊室(6瓷砖x 4瓷砖的房间),如果完全可以通过,但包含一组椅子和一小块使得一些实体无法通行的喷泉。

Hope this helps 希望这可以帮助

Guillaume 纪尧姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM