Пример #1
0
        /// <summary>
        /// Checks whether the current BoundingFrustum intersects the specified Ray.
        /// </summary>
        /// <param name="ray">The Ray to check for intersection with.</param>
        /// <param name="inDistance">The distance at which the ray enters the frustum if there is an intersection and the ray starts outside the frustum.</param>
        /// <param name="outDistance">The distance at which the ray exits the frustum if there is an intersection.</param>
        /// <returns><c>true</c> if the current BoundingFrustum intersects the specified Ray.</returns>
        public bool Intersects(ref MyRay ray, out float?inDistance, out float?outDistance)
        {
            if (Contains(ray.Position) != MyContainmentType.Disjoint)
            {
                float nearstPlaneDistance = float.MaxValue;
                for (int i = 0; i < 6; i++)
                {
                    var   plane = GetPlane(i);
                    float distance;
                    if (MyCollision.RayIntersectsPlane(ref ray, ref plane, out distance) && distance < nearstPlaneDistance)
                    {
                        nearstPlaneDistance = distance;
                    }
                }

                inDistance  = nearstPlaneDistance;
                outDistance = null;
                return(true);
            }
            else
            {
                //We will find the two points at which the ray enters and exists the frustum
                //These two points make a line which center inside the frustum if the ray intersects it
                //Or outside the frustum if the ray intersects frustum planes outside it.
                float minDist = float.MaxValue;
                float maxDist = float.MinValue;
                for (int i = 0; i < 6; i++)
                {
                    var   plane = GetPlane(i);
                    float distance;
                    if (MyCollision.RayIntersectsPlane(ref ray, ref plane, out distance))
                    {
                        minDist = Math.Min(minDist, distance);
                        maxDist = Math.Max(maxDist, distance);
                    }
                }

                MyVector3 minPoint = ray.Position + ray.Direction * minDist;
                MyVector3 maxPoint = ray.Position + ray.Direction * maxDist;
                MyVector3 center   = (minPoint + maxPoint) / 2f;
                if (Contains(ref center) != MyContainmentType.Disjoint)
                {
                    inDistance  = minDist;
                    outDistance = maxDist;
                    return(true);
                }
                else
                {
                    inDistance  = null;
                    outDistance = null;
                    return(false);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Determines whether there is an intersection between a <see cref="MyRay"/> and a <see cref="MyOrientedBoundingBox"/>.
        /// </summary>
        /// <param name="ray">The ray to test.</param>
        /// <param name="point">When the method completes, contains the point of intersection,
        /// or <see cref="MyVector3.Zero"/> if there was no intersection.</param>
        /// <returns>Whether the two objects intersected.</returns>
        public bool Intersects(ref MyRay ray, out MyVector3 point)
        {
            // Put ray in box space
            MyMatrix invTrans;
            MyMatrix.Invert(ref Transformation, out invTrans);

            MyRay bRay;
            MyVector3.TransformNormal(ref ray.Direction, ref invTrans, out bRay.Direction);
            MyVector3.TransformCoordinate(ref ray.Position, ref invTrans, out bRay.Position);

            //Perform a regular ray to BoundingBox check
            var bb = new MyBoundingBox(-Extents, Extents);
            var intersects = MyCollision.RayIntersectsBox(ref bRay, ref bb, out point);

            //Put the result intersection back to world
            if (intersects)
                MyVector3.TransformCoordinate(ref point, ref Transformation, out point);

            return intersects;
        }
Пример #3
0
        /// <summary>
        /// Determines if there is an intersection between the current object and a <see cref="MyRay"/>.
        /// </summary>
        /// <param name="ray">The ray to test.</param>
        /// <returns>Whether the two objects intersected.</returns>
        public bool Intersects(ref MyRay ray)
        {
            float distance;

            return(MyCollision.RayIntersectsSphere(ref ray, ref this, out distance));
        }
Пример #4
0
 /// <summary>
 /// Determines if there is an intersection between the current object and a <see cref="MyRay"/>.
 /// </summary>
 /// <param name="ray">The ray to test.</param>
 /// <param name="point">When the method completes, contains the point of intersection,
 /// or <see cref="MyVector3.Zero"/> if there was no intersection.</param>
 /// <returns>Whether the two objects intersected.</returns>
 public bool Intersects(ref MyRay ray, out MyVector3 point)
 {
     return(MyCollision.RayIntersectsSphere(ref ray, ref this, out point));
 }
Пример #5
0
 /// <summary>
 /// Determines if there is an intersection between the current object and a <see cref="MyRay"/>.
 /// </summary>
 /// <param name="ray">The ray to test.</param>
 /// <param name="distance">When the method completes, contains the distance of the intersection,
 /// or 0 if there was no intersection.</param>
 /// <returns>Whether the two objects intersected.</returns>
 public bool Intersects(ref MyRay ray, out float distance)
 {
     return(MyCollision.RayIntersectsBox(ref ray, ref this, out distance));
 }
Пример #6
0
        /// <summary>
        /// Checks whether the current BoundingFrustum intersects the specified Ray.
        /// </summary>
        /// <param name="ray">The ray.</param>
        /// <returns><c>true</c> if the current BoundingFrustum intersects the specified Ray.</returns>
        public bool Intersects(ref MyRay ray)
        {
            float?inDist, outDist;

            return(Intersects(ref ray, out inDist, out outDist));
        }
Пример #7
0
 /// <summary>
 /// Determines if there is an intersection between the current object and a <see cref="MyPlane"/>.
 /// </summary>
 /// <param name="plane">The plane to test.</param>
 /// <param name="line">When the method completes, contains the line of intersection
 /// as a <see cref="MyRay"/>, or a zero ray if there was no intersection.</param>
 /// <returns>Whether the two objects intersected.</returns>
 public bool Intersects(ref MyPlane plane, out MyRay line)
 {
     return(MyCollision.PlaneIntersectsPlane(ref this, ref plane, out line));
 }
Пример #8
0
 /// <summary>
 /// Determines whether there is an intersection between a <see cref="MyRay"/> and a <see cref="MyOrientedBoundingBox"/>.
 /// </summary>
 /// <param name="ray">The ray to test.</param>
 /// <returns>Whether the two objects intersected.</returns>
 public bool Intersects(ref MyRay ray)
 {
     MyVector3 point;
     return Intersects(ref ray, out point);
 }