【问题标题】:Game development logic (get units in region)游戏开发逻辑(获取区域内的单位)
【发布时间】: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 树等)
  • 您可以构建一些树,但只有数千个点并且每秒搜索次数不多,我认为没有必要。先衡量,再优化。

标签: c++ directx logic


【解决方案1】:

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

正如 cmets 中提到的,执行此操作的常用方法是像 Quadtrees、Octrees 或 BSP-Trees 这样的树。

我想将这种任务与 DirectX 混合使用,以简化渲染过程,将缓冲区中不在视野(相机)内的模型排除在外。

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

【讨论】:

    猜你喜欢
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 2011-12-13
    相关资源
    最近更新 更多