Ejemplo n.º 1
0
        /// <summary>
        /// Finds a contaning quad node.
        /// </summary>
        public QuadNode GetNodeContaining(ref CollideElement bounds)
        {
            if (rootNode != null)
            {
                if (rootNode.Contains(ref bounds))
                {
                    return(rootNode.GetNodeContaining(ref bounds));
                }
            }

            return(null);
        }
Ejemplo n.º 2
0
        protected bool TestUsingQuadTree(CollideElement sourceCollide,
                                         QuadNode quadNode,
                                         out Vector3 intersect,
                                         out Vector3 normal,
                                         out float distance)
        {
            bool result = false;

            float   tempDistance  = 0.0f;
            Vector3 tempIntersect = Vector3.Zero;
            Vector3 tempNormal    = Vector3.Zero;

            float   closestDistance     = float.MaxValue;
            Vector3 closestIntersection = Vector3.Zero;
            Vector3 closestNormal       = Vector3.Zero;

            distance  = 0.0f;
            intersect = Vector3.Zero;
            normal    = Vector3.Zero;

            //  checks upper left node.
            if (quadNode.UpperLeftNode != null)
            {
                if (TestUsingQuadTree(sourceCollide, quadNode.UpperLeftNode,
                                      out tempIntersect, out tempNormal,
                                      out tempDistance))
                {
                    result = true;

                    //  checks closest
                    if (closestDistance > tempDistance)
                    {
                        closestDistance     = tempDistance;
                        closestIntersection = tempIntersect;
                        closestNormal       = tempNormal;
                    }
                }
            }

            //  checks upper right node.
            if (quadNode.UpperRightNode != null)
            {
                if (TestUsingQuadTree(sourceCollide, quadNode.UpperRightNode,
                                      out tempIntersect, out tempNormal,
                                      out tempDistance))
                {
                    result = true;

                    //  checks closest
                    if (closestDistance > tempDistance)
                    {
                        closestDistance     = tempDistance;
                        closestIntersection = tempIntersect;
                        closestNormal       = tempNormal;
                    }
                }
            }

            //  checks lower left node.
            if (quadNode.LowerLeftNode != null)
            {
                if (TestUsingQuadTree(sourceCollide, quadNode.LowerLeftNode,
                                      out tempIntersect, out tempNormal,
                                      out tempDistance))
                {
                    result = true;

                    //  checks closest
                    if (closestDistance > tempDistance)
                    {
                        closestDistance     = tempDistance;
                        closestIntersection = tempIntersect;
                        closestNormal       = tempNormal;
                    }
                }
            }

            //  checks lower right node.
            if (quadNode.LowerRightNode != null)
            {
                if (TestUsingQuadTree(sourceCollide, quadNode.LowerRightNode,
                                      out tempIntersect, out tempNormal,
                                      out tempDistance))
                {
                    result = true;

                    //  checks closest
                    if (closestDistance > tempDistance)
                    {
                        closestDistance     = tempDistance;
                        closestIntersection = tempIntersect;
                        closestNormal       = tempNormal;
                    }
                }
            }
            //  checks vertices in quad node.
            if (quadNode.Contains(ref sourceCollide))
            {
                //  checks vertices with bounding sphere.
                if (sourceCollide is CollideSphere)
                {
                    CollideSphere collide = sourceCollide as CollideSphere;

                    // Hit test sphere with the model
                    BoundingSphere sphere = collide.BoundingSphere;

                    if (quadNode.Vertices != null)
                    {
                        if (TestSphereintersectModel(sphere, quadNode.Vertices,
                                                     Matrix.Identity,
                                                     out tempIntersect, out tempNormal,
                                                     out tempDistance))
                        {
                            result = true;

                            //  checks closest
                            if (closestDistance > tempDistance)
                            {
                                closestDistance     = tempDistance;
                                closestIntersection = tempIntersect;
                                closestNormal       = tempNormal;
                            }
                        }
                    }
                }
                //  checks vertices with ray.
                else if (sourceCollide is CollideRay)
                {
                    CollideRay collide = sourceCollide as CollideRay;

                    if (quadNode.Vertices != null)
                    {
                        if (TestRayintersectModel(collide.Ray, quadNode.Vertices,
                                                  Matrix.Identity,
                                                  out tempIntersect, out tempNormal,
                                                  out tempDistance))
                        {
                            result = true;

                            //  checks closest
                            if (closestDistance > tempDistance)
                            {
                                closestDistance     = tempDistance;
                                closestIntersection = tempIntersect;
                                closestNormal       = tempNormal;
                            }
                        }
                    }
                }
            }

            //  resolve final result.
            if (result)
            {
                distance  = closestDistance;
                intersect = closestIntersection;
                normal    = closestNormal;
            }

            return(result);
        }
Ejemplo n.º 3
0
        protected bool TestUsingQuadTree(CollideElement sourceCollide,
                                        QuadNode quadNode,
                                        out Vector3 intersect,
                                        out Vector3 normal,
                                        out float distance)
        {
            bool result = false;

            float tempDistance = 0.0f;
            Vector3 tempIntersect = Vector3.Zero;
            Vector3 tempNormal = Vector3.Zero;

            float closestDistance = float.MaxValue;
            Vector3 closestIntersection = Vector3.Zero;
            Vector3 closestNormal = Vector3.Zero;

            distance = 0.0f;
            intersect = Vector3.Zero;
            normal = Vector3.Zero;

            //  checks upper left node.
            if (quadNode.UpperLeftNode != null)
            {
                if (TestUsingQuadTree(sourceCollide, quadNode.UpperLeftNode,
                                        out tempIntersect, out tempNormal, 
                                        out tempDistance))
                {
                    result = true;

                    //  checks closest
                    if (closestDistance > tempDistance)
                    {
                        closestDistance = tempDistance;
                        closestIntersection = tempIntersect;
                        closestNormal = tempNormal;
                    }
                }
            }

            //  checks upper right node.
            if (quadNode.UpperRightNode != null)
            {
                if (TestUsingQuadTree(sourceCollide, quadNode.UpperRightNode,
                                        out tempIntersect, out tempNormal, 
                                        out tempDistance))
                {
                    result = true;

                    //  checks closest
                    if (closestDistance > tempDistance)
                    {
                        closestDistance = tempDistance;
                        closestIntersection = tempIntersect;
                        closestNormal = tempNormal;
                    }
                }
            }

            //  checks lower left node.
            if (quadNode.LowerLeftNode != null)
            {
                if (TestUsingQuadTree(sourceCollide, quadNode.LowerLeftNode,
                                        out tempIntersect, out tempNormal, 
                                        out tempDistance))
                {
                    result = true;

                    //  checks closest
                    if (closestDistance > tempDistance)
                    {
                        closestDistance = tempDistance;
                        closestIntersection = tempIntersect;
                        closestNormal = tempNormal;
                    }
                }
            }

            //  checks lower right node.
            if (quadNode.LowerRightNode != null)
            {
                if (TestUsingQuadTree(sourceCollide, quadNode.LowerRightNode,
                                        out tempIntersect, out tempNormal, 
                                        out tempDistance))
                {
                    result = true;

                    //  checks closest
                    if (closestDistance > tempDistance)
                    {
                        closestDistance = tempDistance;
                        closestIntersection = tempIntersect;
                        closestNormal = tempNormal;
                    }
                }
            }
            //  checks vertices in quad node.
            if (quadNode.Contains(ref sourceCollide))
            {
                //  checks vertices with bounding sphere.
                if (sourceCollide is CollideSphere)
                {
                    CollideSphere collide = sourceCollide as CollideSphere;

                    // Hit test sphere with the model
                    BoundingSphere sphere = collide.BoundingSphere;

                    if (quadNode.Vertices != null)
                    {
                        if (TestSphereintersectModel(sphere, quadNode.Vertices,
                                            Matrix.Identity,
                                            out tempIntersect, out tempNormal,
                                            out tempDistance))
                        {
                            result = true;

                            //  checks closest
                            if (closestDistance > tempDistance)
                            {
                                closestDistance = tempDistance;
                                closestIntersection = tempIntersect;
                                closestNormal = tempNormal;
                            }
                        }
                    }
                }
                //  checks vertices with ray.
                else if (sourceCollide is CollideRay)
                {
                    CollideRay collide = sourceCollide as CollideRay;

                    if (quadNode.Vertices != null)
                    {
                        if (TestRayintersectModel(collide.Ray, quadNode.Vertices,
                                            Matrix.Identity,
                                            out tempIntersect, out tempNormal,
                                            out tempDistance))
                        {
                            result = true;

                            //  checks closest
                            if (closestDistance > tempDistance)
                            {
                                closestDistance = tempDistance;
                                closestIntersection = tempIntersect;
                                closestNormal = tempNormal;
                            }
                        }
                    }
                }
            }

            //  resolve final result.
            if (result)
            {
                distance = closestDistance;
                intersect = closestIntersection;
                normal = closestNormal;
            }

            return result;
        }