Beispiel #1
0
        /// <summary>
        /// Gets all objects close to the collidable object passed in parameter
        /// </summary>
        /// <returns>True if objects are available to test otherwise return false</returns>
        /// <param name='colliderToTest'>The object to test with the collection</param>
        /// <param name='testFunction'>A callback function</param>
        public bool TestCandidates(ICollidable2 colliderToTest, TestOneCallback testFunction)
        {
            bool hasCandidate = false;

            List <ICollidable2> candidates = GetCandidates(colliderToTest);

            foreach (ICollidable2 candidate in candidates)
            {
                testFunction(candidate);
                hasCandidate = true;
            }

            return(hasCandidate);
        }
Beispiel #2
0
        /// <summary>
        /// Get all that can potentially collide with this entity
        /// </summary>
        /// <param name="entity">A collidable entity to test</param>
        /// <returns>An array of collidable elements</returns>
        public List <ICollidable2> GetCandidates(ICollidable2 entity)
        {
            List <ICollidable2> candidates = new List <ICollidable2>();

            int index = GetNodeIndex(entity.Rectangle);

            // If the space is already splited we get node objects that can potentially collide with this entity
            if (index > -1 && _nodes[0] != null)
            {
                candidates.AddRange(_nodes[index].GetCandidates(entity));
            }

            // All remaining objects can potentially collide with this entity
            candidates.AddRange(_objects);

            return(candidates);
        }
Beispiel #3
0
        /// <summary>
        /// Add a rectangle of an entity.
        /// </summary>
        /// <param name="entity">Rectangle of an entity</param>
        public void Add(ICollidable2 entity)
        {
            // If the Quadtree is already splited
            if (_nodes[0] != null)
            {
                int index = GetNodeIndex(entity.Rectangle);

                if (index > -1)
                {
                    _nodes[index].Add(entity);
                    return;
                }
            }

            _objects.Add(entity);

            // Split the space if we have too many objects. The limit is MaxLevels
            if (_objects.Count > _maxObjectsPerNode && _level < _maxSubLevels)
            {
                if (_nodes[0] == null)
                {
                    Split();
                }

                int i = 0;
                while (i < _objects.Count)
                {
                    int index = GetNodeIndex(_objects[i].Rectangle);
                    if (index > -1)
                    {
                        // Add the object to the correct node en remove it from the its parent
                        _nodes[index].Add(_objects[i]);
                        _objects.RemoveAt(i);
                    }
                    else
                    {
                        i++;
                    }
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Gets all objects close to the collidable objects passed in parameter
        /// </summary>
        /// <param name="collidables"></param>
        /// <param name="functionTest"></param>
        /// <returns></returns>
        public bool TestCandidates(ICollidable2[] collidables, TestManyCallback testFunction)
        {
            bool hasCandidate = false;

            List<ICollidable2> candidates;
            foreach (ICollidable2 collidable in collidables)
            {
                candidates = GetCandidates(collidable);
                foreach (ICollidable2 candidate in candidates)
                {
                    testFunction(collidable, candidate);
                    hasCandidate = true;
                }
            }

            return hasCandidate;
        }
Beispiel #5
0
 /// <summary>
 /// Clear the quadtree and add collidables objects on it
 /// </summary>
 /// <param name="collidables">An array of collidable objects</param>
 public void Begin(ICollidable2[] collidables)
 {
     Clear();
     foreach (ICollidable2 collidable in collidables)
         Add(collidable);
 }
Beispiel #6
0
        /// <summary>
        /// Add a rectangle of an entity.
        /// </summary>
        /// <param name="entity">Rectangle of an entity</param>
        public void Add(ICollidable2 entity)
        {
            // If the Quadtree is already splited
            if (_nodes[0] != null)
            {
                int index = GetNodeIndex(entity.Rectangle);

                if (index > -1)
                {
                    _nodes[index].Add(entity);
                    return;
                }
            }

            _objects.Add(entity);

            // Split the space if we have too many objects. The limit is MaxLevels
            if (_objects.Count > _maxObjectsPerNode && _level < _maxSubLevels)
            {
                if (_nodes[0] == null)
                    Split();

                int i = 0;
                while (i < _objects.Count)
                {
                    int index = GetNodeIndex(_objects[i].Rectangle);
                    if (index > -1)
                    {
                        // Add the object to the correct node en remove it from the its parent
                        _nodes[index].Add(_objects[i]);
                        _objects.RemoveAt(i);
                    }
                    else
                        i++;
                }
            }
        }
Beispiel #7
0
        /// <summary>
        /// Get all that can potentially collide with this entity
        /// </summary>
        /// <param name="entity">A collidable entity to test</param>
        /// <returns>An array of collidable elements</returns>
        public List<ICollidable2> GetCandidates(ICollidable2 entity)
        {
            List<ICollidable2> candidates = new List<ICollidable2>();

            int index = GetNodeIndex(entity.Rectangle);

            // If the space is already splited we get node objects that can potentially collide with this entity
            if (index > -1 && _nodes[0] != null)
                candidates.AddRange(_nodes[index].GetCandidates(entity));

            // All remaining objects can potentially collide with this entity
            candidates.AddRange(_objects);

            return candidates;
        }
Beispiel #8
0
        /// <summary>
        /// Gets all objects close to the collidable object passed in parameter
        /// </summary>
        /// <returns>True if objects are available to test otherwise return false</returns>
        /// <param name='colliderToTest'>The object to test with the collection</param>
        /// <param name='testFunction'>A callback function</param>
        public bool TestCandidates(ICollidable2 colliderToTest, TestOneCallback testFunction)
        {
            bool hasCandidate = false;

            List<ICollidable2> candidates = GetCandidates(colliderToTest);

            foreach (ICollidable2 candidate in candidates)
            {
                testFunction(candidate);
                hasCandidate = true;
            }

            return hasCandidate;
        }
Beispiel #9
0
 public bool TestCandidates(ICollidable2 collidable, TestManyCallback functionTest)
 {
     return TestCandidates(new ICollidable2[] { collidable }, functionTest);
 }
Beispiel #10
0
 public bool TestCandidates(ICollidable2 collidable, TestManyCallback functionTest)
 {
     return(TestCandidates(new ICollidable2[] { collidable }, functionTest));
 }