Java:存储无限游戏世界的坐标图是什么好的数据结构? [英] Java: What is a good data structure for storing a coordinate map for an infinite game world?

查看:221
本文介绍了Java:存储无限游戏世界的坐标图是什么好的数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯于PHP编码,但我并不熟练使用Java,现在已经有一段时间了。我希望它是一个相当容易的解决方案,但是我无法找到任何好的示例代码,我搜索它,所以这里:



我正在编程一个游戏,在一个基于瓦片的地图上放置在一个2d随机生成的无限世界(nitpicking:我知道这不会是真正的无限的,我只是期望世界是相当大的)。 map [x] [y]多维数组的通常方法作为一个基本思想开始,但由于Java不提供像PHP这样的非整数(即否定)数组关键字shenanigans的方法,所以我无法正确地拥有( - x,+ x,-y,+ y)与数组键的坐标系。



我需要能够在特定的x,y协调以及查找某个瓦片的相邻瓦片。 (如果我可以getObjectAt(x,y),我可以得到(x + 1,y)等等),这个简单的例子。



我已经阅读了关于四叉树和R-树等。这个概念是令人兴奋的,但是我没有看到任何好的,简单的Java实例。除此之外,我还不确定是否需要这些。



欢迎任何建议



谢谢你

解决方案

我遇到同样的问题来到这个线程,但我的解决方案是使用 Map / HashMaps ,但这些都是一维的。



为了克服这个原因,我使用了一个通用的类(不是你会在股票中找到的东西java库)虽然可以用Position类替换(实际上相同的代码,但不是通用的,而是整数或浮点数)。



所以定义地图时: Map< Pair,Tile> tile = new HashMap< Pair,Tile> ;;



为了将tile对象放在地图上,我使用了 tiles.put (new Pair(x,y),new GrassTile()); 和检索对象 tiles.get(new Pair(x,y));



[x / y将是您要放置的任何坐标(这允许负坐标没有任何混乱!)new GrassTile 只是在创建地图时放置某种类型的图块的示例。显然,如前所述,Pair类是可替换的。]



为什么不能ArrayList?因为数组列表比映射更线性,在我看来更难添加和检索图块,特别是在2维。



更新: strong>



如果有人想知道为什么Java中没有一个Pair()类,这里是一个说明


I am used to coding in PHP but I am not really proficient with Java and this has been a problem for some time now. I expect it to be a fairly easy solution, however I cannot find any good example code any way I search it, so here goes:

I am programming a game that takes place in a 2d random generated infinite world on a tile based map (nitpicking: I know it will not be truly infinite. I just expect the world to be quite large). The usual approach of map[x][y] multidimensional array started out as a basic idea, but since Java does not provide a way for non-integer (i.e. negative) array key shenanigans like PHP does, I cannot properly have a (-x,+x,-y,+y) coordinate system with array keys.

I need to be able to find the objects on a tile at a specific x,y coordinate as well as finding "adjacent tiles" of a certain tile. (Trivial if I can getObjectAt(x,y), I can get(x+1,y) and so on)

I have read about quad trees and R-trees and the like. The concept is exciting, however I haven't seen any good, simple example implementation in Java. And besides I am not really sure if that is what I need exactly.

Any advice is welcome

Thank you

解决方案

I came to this thread with the same problem, but my solution was to use Map/HashMaps, but these are one dimensional.

To overcome this, instead of using a map within a map (which would be messy and very inefficient) I used a generic Pair class (not something that you'll find in the stock java library) although you could replace this with a Position class (virtually the same code, but not generic, instead integers or floats).

So when defining the map: Map<Pair, Tile> tiles = new HashMap<Pair, Tile>;

For placing tile objects onto the map I used tiles.put(new Pair(x, y), new GrassTile()); and for retrieving the object tiles.get(new Pair(x, y));.

[x/y would be any coordinate you wish to place (this allows negative coordinates without any mess!), "new GrassTile()" is just an example of placing a tile of a certain type during map creation. Obviously - as previously stated - the Pair class is replacable.]

Why not ArrayLists you may ask? Because array lists are much more linear than mapping, and in my opinion are more difficult to add and retrieve tiles, especially on 2 Dimensions.

Update:

For anyone wondering why there isn't a Pair() class in Java, here's an explanation.

这篇关于Java:存储无限游戏世界的坐标图是什么好的数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆