【发布时间】:2012-12-13 13:17:58
【问题描述】:
我正在开发一款游戏,但遇到了问题。基本上这个想法是:
- 玩家在区域使用法术。
- 引擎会选择区域内的所有单位并对每个单位施加效果。
问题在于游戏理论上拥有数千个单元,并且运行一个循环来检查每个单元是否在指定区域内会花费很多处理。
有没有办法索引和加快这个过程?
这是一个例子(它的理论!!):
// Create the unit struct
typedef struct UNIT{
int ID;
int x;
int y;
}unit;
// Initialize the global unit struct
unit* allunits;
// Do some code, expand the array and add more units
void addUnits(){
...
}
// Search for the number of units that are inside a random region using for and the unit array
int unitsInRegion(x1, x2, y1, y2){
// Spend alot of processing because there is like 10.000 units and we need to look at each X and Y
}
我想将这种任务与 DirectX 混合使用,以简化渲染过程,不包括缓冲区中的视图(相机)之外的模型。
对不起,我的英语不好。
【问题讨论】:
-
通常你可以将单位附加到一个地形节点上,然后在节点上搜索任何单位。
-
您可以查看空间索引结构,如网格、四叉树或 BSP(二进制空间分区)树(k-d 树等)
-
您可以构建一些树,但只有数千个点并且每秒搜索次数不多,我认为没有必要。先衡量,再优化。