示例#1
0
 public List<Actor> Intersect(Collidable col)
 {
     UpdateDirty();
     return Intersect(col.Box);
 }
示例#2
0
 public void Remove(Collidable col)
 {
     if (col.Node != null)
         col.Remove();
 }
示例#3
0
 public void RegisterDirty(Collidable col)
 {
     DirtyObjects.Add(col);
 }
示例#4
0
 public void Insert(Collidable col)
 {
     Root.Insert(col);
 }
示例#5
0
        internal bool Insert(Collidable obj)
        {
            ++Count;

            if (Children == null)
            {
                if (Objects.Count < Tree.MaxObjectsPerNode || Level > 12)
                {
                    obj.Node = this;
                    Objects.Add(obj);
                    return true;
                }
                else
                {
                    Divide();

                    var oldObjectsCount = Objects.Count;
                    var oldObjects = Objects.ToArray();

                    Count = 0;
                    Objects.Clear();

                    for (var i = 0; i < oldObjectsCount; ++i)
                    {
                        Insert(oldObjects[i]);
                    }
                }
            }

            var center = obj.Center;
            var extents = obj.Extents;

            var right = center.X + extents.X;
            var left = center.X - extents.X;
            var top = center.Y + extents.Y;
            var bottom = center.Y - extents.Y;
            var front = center.Z + extents.Z;
            var back = center.Z - extents.Z;

            // in right 
            if (left > Center.X)
            {
                // in right_top
                if (bottom > Center.Y)
                {
                    // in right_top_back
                    if (front < Center.Z)
                    {
                        Children[RIGHT_TOP_BACK].Insert(obj);
                    }

                    // in right_top_front
                    else if (back > Center.Z)
                    {
                        Children[RIGHT_TOP_FRONT].Insert(obj);
                    }

                    // in both Z quadrants
                    else
                    {
                        obj.Node = this;
                        Objects.Add(obj);
                    }
                }

                // in the right_bottom
                else if (top < Center.Y)
                {

                    // in right_bottom_back
                    if (front < Center.Z)
                    {
                        Children[RIGHT_BOTTOM_BACK].Insert(obj);
                    }

                    // in right_bottom_front
                    else if (back > Center.Z)
                    {
                        Children[RIGHT_BOTTOM_FRONT].Insert(obj);
                    }

                    // in both Z quadrants
                    else
                    {
                        obj.Node = this;
                        Objects.Add(obj);
                    }
                }

                // in both right Y quadrants
                else
                {
                    obj.Node = this;
                    Objects.Add(obj);
                }
            }

            // in the left X quadrant
            else if (right < Center.X)
            {

                // in left_top
                if (bottom > Center.Y)
                {
                    // in left_top_back
                    if (front < Center.Z)
                    {
                        Children[LEFT_TOP_BACK].Insert(obj);
                    }

                    // in left_top_front
                    else if (back > Center.Z)
                    {
                        Children[LEFT_TOP_FRONT].Insert(obj);
                    }

                    // in both Z quadrants
                    else
                    {
                        obj.Node = this;
                        Objects.Add(obj);
                    }
                }

                // in the left_bottom
                else if (top < Center.Y)
                {

                    // in left_bottom_back
                    if (front < Center.Z)
                    {
                        Children[LEFT_BOTTOM_BACK].Insert(obj);
                    }

                    // in left_bottom_front
                    else if (back > Center.Z)
                    {
                        Children[LEFT_BOTTOM_FRONT].Insert(obj);
                    }

                    // in both Z quadrants
                    else
                    {
                        obj.Node = this;
                        Objects.Add(obj);
                    }
                }

                // in both left Y quadrants
                else
                {
                    obj.Node = this;
                    Objects.Add(obj);
                }

            }

            // in both X quadrants
            else
            {
                obj.Node = this;
                Objects.Add(obj);
            }

            return false;
        }