Example #1
0
		/**
		 * Create a root element
		 * 
		 * Given rect of boundary must have topleft corner as origin
		 **/
		public static QuadtreeNode createRootQuadtree(ConvexRect nodeBoundry){

			QuadtreeNode rootQuadtree = new QuadtreeNode (null);

			rootQuadtree.SetBoundary (nodeBoundry);

			return rootQuadtree;
		}
Example #2
0
		/**
		 * Find root quadtree 
		 **/
		public QuadtreeNode rootQuadtree(){

			QuadtreeNode q = this;

			while (q.parentNode != null)
				q = q.parentNode;

			#if DEBUG
			if(q!=null)
				q.debugDrawColor = Color.green;
			#endif

			return q;
		}
Example #3
0
		/**
		 * Initilize quadtree node
		 **/
		void InitNode(QuadtreeNode parent){

			elements = new List<IQuadtreeAgent> (elementCapacity);
			overlapElements = new List<IQuadtreeAgent> ();
			elementsNextFrame = new List<IQuadtreeAgent> ();

			parentNode = parent;

			//set depth to 0 if this is root
			if (parent == null)
				depthIndex = 0;
			else
				depthIndex = parent.depthIndex + 1;
		}
        void Awake()
        {
            quadtreeSize = new Vector2(Camera.main.orthographicSize - 1.0f, Camera.main.orthographicSize - 1.0f);


            boundary = new ConvexRect(new Vector2(transform.position.x, transform.position.y),
                                      new Vector2(quadtreeSize.x, quadtreeSize.y)
                                      );

            QuadtreeNode.elementCapacity = capacity;
            quadtree = QuadtreeNode.createRootQuadtree(boundary);

            if (objToAdd > 0)
            {
                StartCoroutine(AutoAddObject());
            }
        }
Example #5
0
		/**
		 * Split quadtree node into 4
		 **/
		private void Split(){

			/*
			#if DEBUG
			Debug.Log("Split quadtree");
			#endif
			*/
			#if DEBUG
			if(nodes != null){
				Debug.LogWarning("Child node already exist before split");
				return;
			}
			#endif

			ConvexRect newBoundary;
			float width = boundary.Width / 2.0f;
			float height = boundary.Height / 2.0f;
			Vector2 topLeftCorner = boundary.AllCorners[0];

			nodes = new QuadtreeNode[4];

			//TopLeft(NorthWest)
			newBoundary = new ConvexRect(topLeftCorner.x, topLeftCorner.y, width, height);
			nodes[0] = new QuadtreeNode (this,newBoundary);


			//TopRight(NorthEast)
			newBoundary = new ConvexRect(topLeftCorner.x+width, topLeftCorner.y, width, height);
			nodes[1] = new QuadtreeNode (this, newBoundary);


			//BottomRight(SouthEast)
			newBoundary = new ConvexRect(topLeftCorner.x+width, topLeftCorner.y - height, width, height);
			nodes[2] = new QuadtreeNode (this, newBoundary);


			//BottomLeft(SouthWest)
			newBoundary = new ConvexRect(topLeftCorner.x, topLeftCorner.y - height, width, height);
			nodes[3] = new QuadtreeNode (this, newBoundary);
		}
Example #6
0
 public override void AddToQuadtreeNode(QuadtreeNode node)
 {
     base.AddToQuadtreeNode(node);
 }
Example #7
0
		QuadtreeNode(QuadtreeNode parent, float x, float y , float width, float height){

			InitNode (parent);
			ConvexRect nodeBoundary = new ConvexRect (x, y, width, height);
			SetBoundary (nodeBoundary);
		}
Example #8
0
		QuadtreeNode(QuadtreeNode parent, ConvexRect nodeBoundary){

			InitNode (parent);
			SetBoundary (nodeBoundary);
		}
Example #9
0
		/**
		 * Constructor
		 **/
		QuadtreeNode(QuadtreeNode parent){

			InitNode (parent);
		}
Example #10
0
 public override void AddToQuadtreeNode(QuadtreeNode node)
 {
     //update node
     currentNode = node;
 }
Example #11
0
 public abstract void AddToQuadtreeNode(QuadtreeNode node);
Example #12
0
        /**
         * Method to update agent in quadtree especially
         * when agent moving around
         **/
        void UpdateAgentInQuadtree()
        {
            if (lastPosition != newPosition)
            {
                if (currentNode != null)
                {
                    //check collision between agent and node boundary
                    CollisionResult result = this.GetShape().IntersectWithShape(currentNode.Boundary);

                    switch (result)
                    {
                    case CollisionResult.Fit:
                        if (!currentNode.IsLeaf)                          //has child node

                        //for all child nodes
                        {
                            IEnumerator er = currentNode.AllNodes.GetEnumerator();
                            while (er.MoveNext())
                            {
                                //if agent fit in this child
                                if (this.GetShape().IntersectWithShape((er.Current as QuadtreeNode).Boundary) == CollisionResult.Fit)
                                {
                                    //move to child node
                                    currentNode.Remove(this);
                                    (er.Current as QuadtreeNode).Add(this);
                                }
                            }
                        }
                        break;

                    case CollisionResult.Overlap:
                        //find parent until agent complete fit in
                        QuadtreeNode pNode = currentNode.Parent;
                        while (pNode != null)
                        {
                            if (this.GetShape().IntersectWithShape(pNode.Boundary) == CollisionResult.Fit)
                            {
                                break;
                            }

                            pNode = pNode.Parent;
                        }

                        currentNode.Remove(this);

                        if (pNode == null)                        //root node
                        {
                            currentNode.rootQuadtree().Add(this);
                        }
                        else
                        {
                            pNode.Add(this);
                        }

                        break;

                    case CollisionResult.None:
                        currentNode.Remove(this);
                        currentNode.rootQuadtree().Add(this);                          //add from root quadtree
                        break;
                    }
                }

                lastPosition = newPosition;
            }
        }