Пример #1
0
        /// <summary>
        /// This method can be used to check if the specified 2D ray intersects the circle.
        /// </summary>
        /// <param name="ray">
        /// The ray involved in the intersection test.
        /// </param>
        /// <param name="t">
        /// If the ray intersects the circle, this will hold the intersection offset between
        /// the ray and the circle. It will always have a value in the interval [0, 1]. If no
        /// intersection happens, it will be set to 0.
        /// </param>
        /// <returns>
        /// True if an intersection happens and false otherwise.
        /// </returns>
        public bool Raycast(Ray2D ray, out float t)
        {
            t = 0.0f;

            // Calculate the equation coefficients
            Vector2 fromCenterToRayOrigin = ray.Origin - _center;
            float   a = Vector3.Dot(ray.Direction, ray.Direction);
            float   b = 2.0f * Vector3.Dot(fromCenterToRayOrigin, ray.Direction);
            float   c = Vector3.Dot(fromCenterToRayOrigin, fromCenterToRayOrigin) - _radius * _radius;

            // If the equation can be solved, it means that ray intersects the circle
            float t1, t2;

            if (Equation.SolveQuadratic(a, b, c, out t1, out t2))
            {
                // Make sure that the ray does not intersect the circle from behind or that the intersection
                // offset doesn't exceed the length of the ray direction.
                if (t1 < 0.0f || t1 > 1.0f)
                {
                    return(false);
                }

                // Store the intersection offset and return true
                t = t1;
                return(true);
            }
            else
            {
                return(false);      // The equation could not be solved, so no intersection occurs
            }
        }
Пример #2
0
        public bool Raycast(Ray ray, out float t)
        {
            t = 0.0f;

            // Calculate the coefficients of the quadratic equation
            Vector3 sphereCenterToRayOrigin = ray.origin - _center;
            float   a = Vector3.SqrMagnitude(ray.direction);
            float   b = 2.0f * Vector3.Dot(ray.direction, sphereCenterToRayOrigin);
            float   c = Vector3.SqrMagnitude(sphereCenterToRayOrigin) - _radius * _radius;

            // If we have a solution to the equation, the ray most likely intersects the sphere.
            float t1, t2;

            if (Equation.SolveQuadratic(a, b, c, out t1, out t2))
            {
                // Make sure the ray doesn't intersect the sphere only from behind
                if (t1 < 0.0f && t2 < 0.0f)
                {
                    return(false);
                }

                // Make sure we are using the smallest positive t value
                if (t1 < 0.0f)
                {
                    float temp = t1;
                    t1 = t2;
                    t2 = temp;
                }
                t = t1;

                return(true);
            }

            // If we reach this point it means the ray does not intersect the sphere in any way
            return(false);
        }