/// <summary>
/// Adds an entry to the broad phase.
/// </summary>
/// <param name="entry">Entry to add.</param>
public override void Add(BroadPhaseEntry entry)
{
base.Add(entry);
//Entities do not set up their own bounding box before getting stuck in here. If they're all zeroed out, the tree will be horrible.
Vector3 offset;
Vector3.Subtract(ref entry.boundingBox.Max, ref entry.boundingBox.Min, out offset);
if (offset.X * offset.Y * offset.Z == 0)
entry.UpdateBoundingBox();
//binary search for the approximately correct location. This helps prevent large first-frame sort times.
int minIndex = 0; //inclusive
int maxIndex = entries.Count; //exclusive
int index = 0;
while (maxIndex - minIndex > 0)
{
index = (maxIndex + minIndex) / 2;
if (entries.Elements[index].boundingBox.Min.X > entry.boundingBox.Min.X)
maxIndex = index;
else if (entries.Elements[index].boundingBox.Min.X < entry.boundingBox.Min.X)
minIndex = ++index;
else
break; //Found an equal value!
}
entries.Insert(index, entry);
}