예제 #1
0
        /// <summary>
        ///    Ray/PlaneBoundedVolume intersection test.
        /// </summary>
        /// <param name="ray"></param>
        /// <param name="volume"></param>
        /// <returns>Struct that contains a bool (hit?) and distance.</returns>
        public static IntersectResult Intersects(Ray ray, PlaneBoundedVolume volume)
        {
            List <Plane> planes = volume.planes;

            float maxExtDist = 0.0f;
            float minIntDist = float.PositiveInfinity;

            float dist, denom, nom;

            for (int i = 0; i < planes.Count; i++)
            {
                Plane plane = planes[i];

                denom = plane.Normal.Dot(ray.Direction);
                if (MathUtil.Abs(denom) < float.Epsilon)
                {
                    // Parallel
                    if (plane.GetSide(ray.Origin) == volume.outside)
                    {
                        return(new IntersectResult(false, 0));
                    }

                    continue;
                }

                nom  = plane.Normal.Dot(ray.Origin) + plane.D;
                dist = -(nom / denom);

                if (volume.outside == AwManaged.Math.PlaneSide.Negative)
                {
                    nom = -nom;
                }

                if (dist > 0.0f)
                {
                    if (nom > 0.0f)
                    {
                        if (maxExtDist < dist)
                        {
                            maxExtDist = dist;
                        }
                    }
                    else
                    {
                        if (minIntDist > dist)
                        {
                            minIntDist = dist;
                        }
                    }
                }
                else
                {
                    //Ray points away from plane
                    if (volume.outside == AwManaged.Math.PlaneSide.Negative)
                    {
                        denom = -denom;
                    }

                    if (denom > 0.0f)
                    {
                        return(new IntersectResult(false, 0));
                    }
                }
            }

            if (maxExtDist > minIntDist)
            {
                return(new IntersectResult(false, 0));
            }

            return(new IntersectResult(true, maxExtDist));
        }
예제 #2
0
파일: Ray.cs 프로젝트: mesh42/awmanaged
 /// <summary>
 ///		Tests whether this ray intersects the given PlaneBoundedVolume.
 /// </summary>
 /// <param name="volume"></param>
 /// <returns>
 ///		Struct containing info on whether there was a hit, and the distance from the
 ///		origin of this ray where the intersect happened.
 ///	</returns>
 public IntersectResult Intersects(PlaneBoundedVolume volume)
 {
     return(MathUtil.Intersects(this, volume));
 }