コード例 #1
0
 /// <summary>
 /// Calculates the distance between a given point and a given ray.
 /// </summary>
 /// <param name="point">A <see cref="Vector2F"/> instance.</param>
 /// <param name="ray">A <see cref="Ray"/> instance.</param>
 /// <returns>The distance between the point and the ray.</returns>
 public static float Distance(Vector2F point, Ray ray)
 {
     return (float)System.Math.Sqrt(SquaredDistance(point, ray));
 }
コード例 #2
0
        /// <summary>
        /// Calculates the squared distance between a given point and a given ray.
        /// </summary>
        /// <param name="point">A <see cref="Vector2F"/> instance.</param>
        /// <param name="ray">A <see cref="Ray"/> instance.</param>
        /// <returns>The squared distance between the point and the ray.</returns>
        public static float SquaredDistance(Vector2F point, Ray ray)
        {
            Vector2F diff = point - ray.Origin;
            float t = Vector2F.Dot(diff, ray.Direction);

            if (t <= 0.0f)
            {
                t = 0.0f;
            }
            else
            {
                t	/= ray.Direction.GetLengthSquared();
                diff-= t * ray.Direction;
            }

            return diff.GetLengthSquared();
        }
コード例 #3
0
        /// <summary>
        /// Find the intersection of a ray and a sphere.
        /// Only works with unit rays (normalized direction)!!!
        /// </summary>
        /// <remarks>
        /// This is the optimized Ray-Sphere intersection algorithms described in "Real-Time Rendering".
        /// </remarks>
        /// <param name="ray">The ray to test.</param>
        /// <param name="t">
        /// If intersection accurs, the function outputs the distance from the ray's origin 
        /// to the closest intersection point to this parameter.
        /// </param>
        /// <returns>Returns True if the ray intersects the sphere. otherwise, <see langword="false"/>.</returns>
        public bool FindIntersections(Ray ray, ref float t)
        {
            // Only gives correct result for unit rays.
            //Debug.Assert(MathUtils.ApproxEquals(1.0f, ray.Direction.GetLength()), "Ray direction should be normalized!");

            // Calculates a vector from the ray origin to the sphere center.
            Vector2F diff		= this.center - ray.Origin;
            // Compute the projection of diff onto the ray direction
            float d = Vector2F.Dot(diff, ray.Direction);

            float diffSquared	= diff.GetLengthSquared();
            float radiusSquared = this.radius * this.radius;

            // First rejection test :
            // if d<0 and the ray origin is outside the sphere than the sphere is behind the ray
            if ((d < 0.0f) && (diffSquared > radiusSquared))
            {
                return false;
            }

            // Compute the distance from the sphere center to the projection
            float mSquared = diffSquared - d*d;

            // Second rejection test:
            // if mSquared > radiusSquared than the ray misses the sphere
            if (mSquared > radiusSquared)
            {
                return false;
            }

            float q = (float)System.Math.Sqrt(radiusSquared - mSquared);

            // We are interested only in the first intersection point:
            if (diffSquared > radiusSquared)
            {
                // If the origin is outside the sphere t = d - q is the first intersection point
                t = d - q;
            }
            else
            {
                // If the origin is inside the sphere t = d + q is the first intersection point
                t = d + q;
            }
            return true;
        }
コード例 #4
0
 /// <summary>
 /// Tests if a ray intersects the sphere.
 /// </summary>
 /// <param name="ray">The ray to test.</param>
 /// <returns>Returns True if the ray intersects the sphere. otherwise, <see langword="false"/>.</returns>
 public bool TestIntersection(Ray ray)
 {
     float squaredDistance = DistanceMethods.SquaredDistance(this.center, ray);
     return (squaredDistance <= this.radius*this.radius);
 }
コード例 #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Ray"/> class using given ray.
 /// </summary>
 /// <param name="ray">A <see cref="Ray"/> instance to assign values from.</param>
 public Ray(Ray ray)
 {
     _origin		= ray.Origin;
     _direction	= ray.Direction;
 }