Ejemplo n.º 1
0
        /// <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();
            }
            var newEntry = entryPool.Take();

            newEntry.Initialize(entry);
            entries.Add(newEntry);
            //Add the object to the grid.
            for (int i = newEntry.previousMin.Y; i <= newEntry.previousMax.Y; i++)
            {
                for (int j = newEntry.previousMin.Z; j <= newEntry.previousMax.Z; j++)
                {
                    var index = new Int2 {
                        Y = i, Z = j
                    };
                    cellSet.Add(ref index, newEntry);
                }
            }
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Adds an entry to the hierarchy.
        /// </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();
            }

            //Could buffer additions to get a better construction in the tree.
            LeafNode node = leafNodes.Take();

            node.Initialize(entry);
            if (root == null)
            {
                //Empty tree.  This is the first and only node.
                root = node;
            }
            else
            {
                if (root.IsLeaf) //Root is alone.
                {
                    root.TryToInsert(node, out root);
                }
                else
                {
                    BoundingBox.CreateMerged(ref node.BoundingBox, ref root.BoundingBox, out root.BoundingBox);
                    InternalNode internalNode = (InternalNode)root;
                    Vector3.Subtract(ref root.BoundingBox.Max, ref root.BoundingBox.Min, out offset);
                    internalNode.currentVolume = offset.X * offset.Y * offset.Z;
                    //internalNode.maximumVolume = internalNode.currentVolume * InternalNode.MaximumVolumeScale;
                    //The caller is responsible for the merge.
                    Node treeNode = root;
                    while (!treeNode.TryToInsert(node, out treeNode))
                    {
                        ; //TryToInsert returns the next node, if any, and updates node bounding box.
                    }
                }
            }
        }
Ejemplo n.º 4
0
        /// <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);
        }
Ejemplo n.º 5
0
 /// <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();
     var newEntry = entryPool.Take();
     newEntry.Initialize(entry);
     entries.Add(newEntry);
     //Add the object to the grid.
     for (int i = newEntry.previousMin.Y; i <= newEntry.previousMax.Y; i++)
     {
         for (int j = newEntry.previousMin.Z; j <= newEntry.previousMax.Z; j++)
         {
             var index = new Int2();
             index.Y = i;
             index.Z = j;
             cellSet.Add(ref index, newEntry);
         }
     }
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Adds an entry to the hierarchy.
 /// </summary>
 /// <param name="entry">Entry to remove.</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();
     //Could buffer additions to get a better construction in the tree.
     var node = leafNodes.Take();
     node.Initialize(entry);
     if (root == null)
     {
         //Empty tree.  This is the first and only node.
         root = node;
     }
     else
     {
         if (root.IsLeaf) //Root is alone.
             root.TryToInsert(node, out root);
         else
         {
             BoundingBox.CreateMerged(ref node.BoundingBox, ref root.BoundingBox, out root.BoundingBox);
             var internalNode = (InternalNode)root;
             Vector3.Subtract(ref root.BoundingBox.Max, ref root.BoundingBox.Min, out offset);
             internalNode.currentVolume = offset.X * offset.Y * offset.Z;
             //internalNode.maximumVolume = internalNode.currentVolume * InternalNode.MaximumVolumeScale;
             //The caller is responsible for the merge.
             var treeNode = root;
             while (!treeNode.TryToInsert(node, out treeNode)) ;//TryToInsert returns the next node, if any, and updates node bounding box.
         }
     }
 }