/// <summary> /// Adds an entry to the broad phase. /// </summary> /// <param name="entry">Entry to add.</param> public override void Add(BroadPhaseEntry 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); }
[Test] public void Basics() { RawList <int> intList = new RawList <int>(); intList.Add(10); intList.AddRange(new int[] { 17, 42, 94 }); Assert.AreEqual(4, intList.Count); Assert.IsTrue(intList.Contains(42)); Assert.AreEqual(2, intList.IndexOf(42)); CollectionAssert.AreEqual(new int[] { 10, 17, 42, 94 }, intList); CollectionAssert.AreEqual(new int[] { 10, 17, 42, 94 }, intList.Data.Take(4)); intList.ShrinkToFit(); Assert.AreEqual(intList.Count, intList.Capacity); intList.Remove(42); Assert.AreEqual(3, intList.Count); Assert.IsTrue(!intList.Contains(42)); Assert.AreEqual(-1, intList.IndexOf(42)); CollectionAssert.AreEqual(new int[] { 10, 17, 94 }, intList); CollectionAssert.AreEqual(new int[] { 10, 17, 94 }, intList.Data.Take(3)); intList.Insert(1, 100); CollectionAssert.AreEqual(new int[] { 10, 100, 17, 94 }, intList); CollectionAssert.AreEqual(new int[] { 10, 100, 17, 94 }, intList.Data.Take(4)); intList.InsertRange(2, new int[] { 150, 200, 250, 300 }); CollectionAssert.AreEqual(new int[] { 10, 100, 150, 200, 250, 300, 17, 94 }, intList); CollectionAssert.AreEqual(new int[] { 10, 100, 150, 200, 250, 300, 17, 94 }, intList.Data.Take(8)); intList.Clear(); Assert.AreEqual(0, intList.Count); Assert.IsTrue(!intList.Contains(94)); }
[Test] public void Basics() { RawList<int> intList = new RawList<int>(); intList.Add(10); intList.AddRange(new int[] { 17, 42, 94 }); Assert.AreEqual(4, intList.Count); Assert.IsTrue(intList.Contains(42)); Assert.AreEqual(2, intList.IndexOf(42)); CollectionAssert.AreEqual(new int[] { 10, 17, 42, 94 }, intList); CollectionAssert.AreEqual(new int[] { 10, 17, 42, 94 }, intList.Data.Take(4)); intList.ShrinkToFit(); Assert.AreEqual(intList.Count, intList.Capacity); intList.Remove(42); Assert.AreEqual(3, intList.Count); Assert.IsTrue(!intList.Contains(42)); Assert.AreEqual(-1, intList.IndexOf(42)); CollectionAssert.AreEqual(new int[] { 10, 17, 94 }, intList); CollectionAssert.AreEqual(new int[] { 10, 17, 94 }, intList.Data.Take(3)); intList.Insert(1, 100); CollectionAssert.AreEqual(new int[] { 10, 100, 17, 94 }, intList); CollectionAssert.AreEqual(new int[] { 10, 100, 17, 94 }, intList.Data.Take(4)); intList.InsertRange(2, new int[] { 150, 200, 250, 300 }); CollectionAssert.AreEqual(new int[] { 10, 100, 150, 200, 250, 300, 17, 94 }, intList); CollectionAssert.AreEqual(new int[] { 10, 100, 150, 200, 250, 300, 17, 94 }, intList.Data.Take(8)); intList.Clear(); Assert.AreEqual(0, intList.Count); Assert.IsTrue(!intList.Contains(94)); }
internal void Add(ref Int2 index, Grid2DEntry entry) { int cellIndex; int sortingHash; if (TryGetIndex(ref index, out cellIndex, out sortingHash)) { cells.Elements[cellIndex].Add(entry); return; } GridCell2D cell = cellPool.Take(); cell.Initialize(ref index, sortingHash); cell.Add(entry); cells.Insert(cellIndex, cell); count++; ////Take an index. See if it's taken in the set. ////If it's already there, then add the entry to the cell. ////If it's not already there, create a new cell and add the entry to the cell and insert it at the index located. //int sortingHash = index.GetSortingHash(); //int minIndex = 0; //inclusive //int maxIndex = count; //exclusive //int i = 0; //while (maxIndex - minIndex > 0) //If the testing interval has a length of zero, we've done as much as we can. //{ // i = (maxIndex + minIndex) / 2; // if (cells.Elements[i].sortingHash > sortingHash) // maxIndex = i; // else if (cells.Elements[i].sortingHash < sortingHash) // minIndex = ++i; // else // { // //Found an equal sorting hash! // //The hash can collide, and we cannot add an entry to // //an incorrect index. It would break the 'cell responsibility' // //used by the cell update process to avoid duplicate overlaps. // //So, check if the index we found is ACTUALLY correct. // if (cells.Elements[i].cellIndex.Y == index.Y && cells.Elements[i].cellIndex.Z == index.Z) // { // cells.Elements[i].Add(entry); // return; // } // //If it was not the correct index, let it continue searching. // } //} //var cell = cellPool.Take(); //cell.Initialize(ref index, sortingHash); //cell.Add(entry); //cells.Insert(i, cell); //count++; }
internal void Add(ref Int2 index, Grid2DEntry entry) { int cellIndex; int sortingHash; if (TryGetIndex(ref index, out cellIndex, out sortingHash)) { cells.Elements[cellIndex].Add(entry); return; } GridCell2D cell = cellPool.Take(); cell.Initialize(ref index, sortingHash); cell.Add(entry); cells.Insert(cellIndex, cell); count++; }
internal void Add(Grid2DEntry entry) { //binary search for the approximately correct location. This helps prevent large first-frame sort times. entries.Insert(GetIndex(entry.item.boundingBox.Min.X), entry); }
public override void Add(BroadPhaseEntry entry) { //binary search for the approximately correct location. This helps prevent large first-frame sort times. //X Axis: int minIndex = 0; //inclusive int maxIndex = entriesX.count; //exclusive int index = 0; while (maxIndex - minIndex > 0) { index = (maxIndex + minIndex) / 2; if (entriesX.Elements[index].boundingBox.Min.X > entry.boundingBox.Min.X) { maxIndex = index; } else if (entriesX.Elements[index].boundingBox.Min.X < entry.boundingBox.Min.X) { minIndex = ++index; } else { break; //Found an equal value! } } entriesX.Insert(index, entry); //Y Axis: minIndex = 0; //inclusive maxIndex = entriesY.count; //exclusive while (maxIndex - minIndex > 0) { index = (maxIndex + minIndex) / 2; if (entriesY.Elements[index].boundingBox.Min.Y > entry.boundingBox.Min.Y) { maxIndex = index; } else if (entriesY.Elements[index].boundingBox.Min.Y < entry.boundingBox.Min.Y) { minIndex = ++index; } else { break; //Found an equal value! } } entriesY.Insert(index, entry); //Z Axis: minIndex = 0; //inclusive maxIndex = entriesZ.count; //exclusive while (maxIndex - minIndex > 0) { index = (maxIndex + minIndex) / 2; if (entriesZ.Elements[index].boundingBox.Min.Z > entry.boundingBox.Min.Z) { maxIndex = index; } else if (entriesZ.Elements[index].boundingBox.Min.Z < entry.boundingBox.Min.Z) { minIndex = ++index; } else { break; //Found an equal value! } } entriesZ.Insert(index, entry); }