IEnumerator AutoAddObject()
        {
            Debug.Log("Adding... " + quadtree.TotalElementCount);

            for (int i = 0; i < objToAdd; i++)
            {
                Vector3       pos;
                GameObject    go            = Instantiate(obj);
                QtCircleAgent agnet         = go.GetComponent <QtCircleAgent> ();
                Vector2       topLeftCorner = boundary.AllCorners [0];
                pos = new Vector3(Random.Range(topLeftCorner.x + 3.0f + agnet.Radius(), topLeftCorner.x + boundary.Width - 3.0f - agnet.Radius()),
                                  Random.Range(topLeftCorner.y - 3.0f - agnet.Radius(), topLeftCorner.y - boundary.Height + 3.0f + agnet.Radius()),
                                  transform.position.z);

                go.transform.position = pos;
                go.name = "Agent " + i;

                quadtree.Add(go.GetComponent <QtAgent> ());

                cacheAgents.Add(go.GetComponent <QtAgent> ());

                yield return(new WaitForSeconds(addInterval));
            }

            Debug.Log("Finish add " + quadtree.TotalElementCount);


            quadtree.LevelDesc();
        }
Exemple #2
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;
            }
        }