我试着把一个四叉树变成一个小项目,只是为了练习。我有几个粒子在半径内弹跳,一个四叉树被绑定到它所形成的圆上。
一切正常,除了我认为我的插入或清除有某种漏洞。在运行了大约10秒与1000个粒子,它开始严重滞后。
以下是功能
_subDivisions是数组,_drawableGameObjects是列表
public void Clear()
{
_drawableGameObjects.Clear();
if (_subDivisions != null)
foreach (QuadTree quad in _subDivisions)
quad.Clear();
_subDivisions = null;
}
public void Insert(DrawableGameObject drawableGameObject)
{
if (_subDivisions != null)
{
int index = GetIndex(drawableGameObject);
if (index > -1)
_subDivisions[index].Insert(drawableGameObject);
return;
}
_drawableGameObjects.Add(drawableGameObject);
if (_drawableGameObjects.Count > _maxObjects && _level < _maximumLevel)
{
Subdivide();
int i = 0;
while (i < _drawableGameObjects.Count)
{
int index = GetIndex(_drawableGameObjects[i]);
if (index > -1)
{
DrawableGameObject currentObject = _drawableGameObjects[i];
_subDivisions[index].Insert(currentObject);
_drawableGameObjects.Remove(currentObject);
}
else
{
i++;
}
}
}
}发布于 2013-10-10 15:07:49
我发现了问题,但我忘了把这段代码排除在问题之外。
除了四叉树功能之外,我还在游戏层中跟踪每个四叉树。所以四人不仅是他们的父母,也是游戏层的孩子。当我删除四边形时,我是从他们的父方清除他们,而不是从他们的父游戏层中清除他们。
https://stackoverflow.com/questions/18923784
复制相似问题