简体   繁体   English

从3D图形获取对象的最有效方法

[英]Most efficient way to get an object from a 3D Graph

So, I am making a game, and I have a problem. 所以,我在做游戏,但遇到了问题。 I need some way to efficiently get the objects that are located in the 3d graph. 我需要一些方法来有效地获取位于3d图形中的对象。

However, the problem is that they all can cover more than one value to get. 但是,问题在于它们全都可以涵盖不止一个价值。 So, for example, the value 'foo' might cover the x range 400-500, y range 10-20 and the z range 30-60. 因此,例如,值'foo'可能涵盖x范围400-500,y范围10-20和z范围30-60。

I am wondering whether there is a more efficient way to store and get these then by just storing them in a Array and searching through them, something I would prefer not to do, because of the large amount of these values. 我想知道是否存在一种更有效的方式来存储和获取它们,然后仅将它们存储在Array中并对其进行搜索,由于这些值很多,我不希望这样做。

This is for selecting biomes for a game. 这是用于为游戏选择生物群系。 However, I chose to put this here versus GameDev because this seemed to be more of a data structures question. 但是,我选择将其与GameDev放在这里,因为这似乎更多是一个数据结构问题。 I might be wrong however, and if I was would gladly close this and open one there. 但是我可能是错的,如果我愿意关闭它并在那里打开它的话。

Is there any other way to do this? 还有其他方法吗?

public static Tile getTile(int height, int moisture, int temperature) {
    for(Tile tile : tileList){

        boolean isTile = true;

        if(!(tile.heightMax > height && tile.heightMin < height)){
            isTile = false;
        }

        if(!(tile.temperatureMax > temperature && tile.temperatureMin < temperature)){
            isTile = false;
        }

        if(!(tile.moistureMax > moisture && tile.moistureMin < moisture)){
            isTile = false;
        }

        if(isTile){
            return tile;
        }
    }
}

This is my searching method. 这是我的搜索方法。 As you can see, I am using a list to store all of the tiles, and searching for the one that fits the criteria. 如您所见,我正在使用一个列表来存储所有图块,并在搜索符合条件的图块。 Is there any other way to do this with many tiles as fast as possible? 还有其他方法可以尽快处理许多图块吗?

The way I've solved this issue in the past is to define regions that represent an area of 3D space and then store a map to each object from all regions an object's space overlaps. 我过去解决此问题的方法是定义代表3D空间区域的区域,然后从对象空间重叠的所有区域存储到每个对象的地图。 Then when you want to find objects at a location you first use the map to find all objects that overlap that location's region before iterating through them looking for objects at that location. 然后,当您要在某个位置查找对象时,请先使用地图查找与该位置的区域重叠的所有对象,然后遍历它们以在该位置查找对象。

Let me give you a code view of that. 让我给您一个代码视图。 To build the region map: 要构建区域地图:

Map<Region, List<Item>> regionMap = new TreeMap<>();

When an item is added: 添加项目时:

item.getOverlappingRegions().forEach(region -> {
    if (!regionMap.containsKey(region))
        regionMap.put(region, new ArrayList<>());
    regionMap.get(region).add(item)
});

Then when you're searching for an item at a location: 然后,当您在某个位置搜索商品时:

if (regionMap.containsKey(location.getRegion()) {
    regionMap.get(location.getRegion()).stream()
        .filter(item -> item.contains(location))
        ....
}

There's definitely overhead in this but it is very fast for searching if your regions are sufficiently small. 这肯定有开销,但是如果您的区域足够小,则搜索非常快。

This is designed for 3D spaces but it can be expanded to as many dimensions as you want: it is all in the implementation of the Region and Location classes. 这是为3D空间设计的,但可以根据需要扩展为任意尺寸:全部在RegionLocation类的实现中。

I am not entirely sure that I have understood what your problem is, but it seems very similar to a collision detection algorithm in 3D space. 我不能完全确定我是否了解您的问题,但它似乎与3D空间中的碰撞检测算法非常相似。 If this is the case, Octrees might be what you are looking for: http://www.codeproject.com/Articles/108761/Octrees 如果是这种情况,八进制可能就是您要查找的内容: http : //www.codeproject.com/Articles/108761/Octrees

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

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