游戏开发逻辑(获取区域内的单位)

时间:2012-12-13 13:17:58

标签: c++ directx logic

我正在开发一款游戏而且我遇到了问题。基本上这个想法是:

  • 玩家在区域使用咒语。
  • 引擎选择区域中的所有单位并对每个单位施加效果。

问题在于游戏理论上拥有数千个单位,并运行一个循环来检查每个单元是否在指定区域内花费大量处理。

有没有办法索引和加快这个过程?

这是一个例子(它的THEORETICAL !!):

// 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混合,以简化渲染过程,从缓冲区中排除视图(相机)不可见的模型。

抱歉我的英语不好。

1 个答案:

答案 0 :(得分:2)

  

有没有办法索引和加快这个过程?

正如评论中所提到的那样,通常的方法是QuadtreesOctreesBSP-Trees等树。

  

我希望将此类任务与DirectX混合,以简化渲染过程,从缓冲区中排除视图(相机)不可见的模型。

我认为你正在寻找View Frustum Culling(Tutorial)。您可以将此方法与提到的树中的一个树混合,以便有效地剔除场景中的所有对象。