这些将是两个或多个八叉树边界之间的对象。目前,我正在执行的操作是复制实体的主要列表,并将其作为对子八叉树的引用,如果子对象完全适合其范围,子八叉树将从列表中获取对象。比较该列表中剩余的内容是否与主列表发生冲突。但是我不确定这是否是最好的方法,因为我不得不重复一个可能包含数千个项目的列表(我也可能做得不好)。
using System.Collections.Generic;
public class Octree
{
BoundingCube boundingCube;
List<Entity> containedEntities;
List<Entity> unreservedEntities;
Octree[] children;
static readonly int capacity = 10;
static readonly int smallestSize = 8;
public Octree(BoundingCube givenBoundingCube, ref List<Entity> givenEntities)
{
boundingCube = givenBoundingCube;
containedEntities = new List<Entity>();
for (int i = givenEntities.Count - 1; i > -1; i--)
{
if (boundingCube.InBounds(givenEntities[i].posdim))
{
containedEntities.Add(givenEntities[i]);
givenEntities.Remove(givenEntities[i]);
}
}
if (containedEntities.Count > capacity && boundingCube.size > smallestSize)
{
Divide();
}
}
public void Divide()
{
unreservedEntities = Methods.CloneList(containedEntities);
children = new Octree[8];
children[0] = new Octree(boundingCube.LeftBottomFront, ref unreservedEntities);
children[1] = new Octree(boundingCube.RightBottomFront, ref unreservedEntities);
children[2] = new Octree(boundingCube.LeftTopFront, ref unreservedEntities);
children[3] = new Octree(boundingCube.RightTopFront, ref unreservedEntities);
children[4] = new Octree(boundingCube.LeftBottomBack, ref unreservedEntities);
children[5] = new Octree(boundingCube.RightBottomBack, ref unreservedEntities);
children[6] = new Octree(boundingCube.LeftTopBack, ref unreservedEntities);
children[7] = new Octree(boundingCube.RightTopBack, ref unreservedEntities);
}
public void CheckForCollisions()
{
for (int i = unreservedEntities.Count; i > 0; i++)
{
for (int j = containedEntities.Count; j > 0; j++)
{
if (unreservedEntities[j].posdim.IntersectsWith(containedEntities[i].posdim))
{
unreservedEntities[j].RunCollision(containedEntities[i]);
break;
}
}
}
}
}
public static List<T> CloneList<T>(List<T> originalList)
{
List<T> newList = new List<T>();
for (int i = 0; i < originalList.Count; i++)
{
newList.Add(originalList[i]);
}
return newList;
}