Ejemplo n.º 1
0
        /// <summary>
        /// Adds the given body to one of the child trees of this instance.
        /// </summary>
        /// <param name="newBody">The body to add.</param>
        private void AddToChildTree(Body newBody)
        {
            // don't create subtrees if it violates the minimum width limit, to prevent infinitely deep trees and thus
            // preventing excessive resource allocation
            if (octant.Length / 2 < Constants.MinimumTreeWidth)
            {
                return;
            }

            for (int childTreeIndex = 0; childTreeIndex < 8; childTreeIndex++)
            {
                OctantTree childTree = SubTree(childTreeIndex);

                // if the child tree under consideration contains the given body, then add it to that tree
                if (childTree.octant.ContainsPoint(newBody.Position))
                {
                    childTree.AddBody(newBody);
                    return;
                }
            }
        }