Exemple #1
0
 public bool Add(BasicModel obj)
 {
     //If object fall into the area a sub-world, it will be added in
     if (this.nodeBounds.Contains(obj.world.Translation.X, obj.world.Translation.Z))
     {
         return this.Add(obj, new Point((int)obj.world.Translation.X, (int)obj.world.Translation.Z)) != null;
     }
     return false;
 }
Exemple #2
0
        public Quadtree Add(BasicModel obj, Point objCenter)
        {
            //This world has been divided into sub-worlds
            //Find the sub-world it belongs to.

            //--------
            //| 0|1 |
            //-------
            //| 2|3 |
            //-------
            if (this.childNodes != null)
            {
                int index = (objCenter.X < this.nodeCenter.X ? 0 : 1)
                            + (objCenter.Y < this.nodeCenter.Y ? 0 : 2);
                return this.childNodes[index].Add(obj, objCenter);
            }

            //Create more sub-world if there is too much object in the same world
            //and the level of worlds did not reach the maximun

            if (this.currentDepth < maxDepth && this.models.Count + 1 > maxObjectCount)
            {
                Split((int)nodeSize);
                List<BasicModel> tempModel = new List<BasicModel>();
                foreach (BasicModel nodeModel in models)
                {
                    tempModel.Add(nodeModel);

                }

                foreach (BasicModel tempmodels  in tempModel)
                {
                    Add(tempmodels);
                }
                this.models.Clear();
                return Add(obj, objCenter);
            }

            //just add object to this world as other criteria dose not match
            else
            {
                this.models.Add(obj);
            }
            return this;
        }