예제 #1
0
        /// <summary>
        ///		Returns whether or not this box intersects another.
        /// </summary>
        /// <param name="box2"></param>
        /// <returns>True if the 2 boxes intersect, false otherwise.</returns>
        public bool Intersects(AxisAlignedBox box2)
        {
            // Early-fail for nulls
            if (this.IsNull || box2.IsNull)
            {
                return(false);
            }

            // Use up to 6 separating planes
            if (this.maxVector.X < box2.minVector.X)
            {
                return(false);
            }
            if (this.maxVector.Y < box2.minVector.Y)
            {
                return(false);
            }
            if (this.maxVector.Z < box2.minVector.Z)
            {
                return(false);
            }

            if (this.minVector.X > box2.maxVector.X)
            {
                return(false);
            }
            if (this.minVector.Y > box2.maxVector.Y)
            {
                return(false);
            }
            if (this.minVector.Z > box2.maxVector.Z)
            {
                return(false);
            }

            // otherwise, must be intersecting
            return(true);
        }
예제 #2
0
        /// <summary>
        ///		Intersection test with an <see cref="AxisAlignedBox"/>.
        /// </summary>
        /// <remarks>
        ///		May return false positives but will never miss an intersection.
        /// </remarks>
        /// <param name="box">Box to test.</param>
        /// <returns>True if interesecting, false otherwise.</returns>
        public bool Intersects(AxisAlignedBox box)
        {
            if (box.IsNull)
            {
                return(false);
            }

            // If all points are on outside of any plane, we fail
            Vector3[] points = box.Corners;

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

                // Test which side of the plane the corners are
                // Intersection fails when at all corners are on the
                // outside of one plane
                bool splittingPlane = true;
                for (int corner = 0; corner < 8; corner++)
                {
                    if (plane.GetSide(points[corner]) != outside)
                    {
                        // this point is on the wrong side
                        splittingPlane = false;
                        break;
                    }
                }
                if (splittingPlane)
                {
                    // Found a splitting plane therefore return not intersecting
                    return(false);
                }
            }

            // couldn't find a splitting plane, assume intersecting
            return(true);
        }
예제 #3
0
        /// <summary>
        ///    Tests an intersection between a ray and a box.
        /// </summary>
        /// <param name="ray"></param>
        /// <param name="box"></param>
        /// <returns>A Pair object containing whether the intersection occurred, and the distance between the 2 objects.</returns>
        public static IntersectResult Intersects(Ray ray, AxisAlignedBox box)
        {
            if (box.IsNull)
            {
                return(new IntersectResult(false, 0));
            }

            float   lowt = 0.0f;
            float   t;
            bool    hit = false;
            Vector3 hitPoint;
            Vector3 min = box.Minimum;
            Vector3 max = box.Maximum;

            // check origin inside first
            if (ray.origin > min && ray.origin < max)
            {
                return(new IntersectResult(true, 0.0f));
            }

            // check each face in turn, only check closest 3

            // Min X
            if (ray.origin.X < min.X && ray.direction.X > 0)
            {
                t = (min.X - ray.origin.X) / ray.direction.X;

                if (t > 0)
                {
                    // substitue t back into ray and check bounds and distance
                    hitPoint = ray.origin + ray.direction * t;

                    if (hitPoint.Y >= min.Y && hitPoint.Y <= max.Y &&
                        hitPoint.Z >= min.Z && hitPoint.Z <= max.Z &&
                        (!hit || t < lowt))
                    {
                        hit  = true;
                        lowt = t;
                    }
                }
            }

            // Max X
            if (ray.origin.X > max.X && ray.direction.X < 0)
            {
                t = (max.X - ray.origin.X) / ray.direction.X;

                if (t > 0)
                {
                    // substitue t back into ray and check bounds and distance
                    hitPoint = ray.origin + ray.direction * t;

                    if (hitPoint.Y >= min.Y && hitPoint.Y <= max.Y &&
                        hitPoint.Z >= min.Z && hitPoint.Z <= max.Z &&
                        (!hit || t < lowt))
                    {
                        hit  = true;
                        lowt = t;
                    }
                }
            }

            // Min Y
            if (ray.origin.Y < min.Y && ray.direction.Y > 0)
            {
                t = (min.Y - ray.origin.Y) / ray.direction.Y;

                if (t > 0)
                {
                    // substitue t back into ray and check bounds and distance
                    hitPoint = ray.origin + ray.direction * t;

                    if (hitPoint.X >= min.X && hitPoint.X <= max.X &&
                        hitPoint.Z >= min.Z && hitPoint.Z <= max.Z &&
                        (!hit || t < lowt))
                    {
                        hit  = true;
                        lowt = t;
                    }
                }
            }

            // Max Y
            if (ray.origin.Y > max.Y && ray.direction.Y < 0)
            {
                t = (max.Y - ray.origin.Y) / ray.direction.Y;

                if (t > 0)
                {
                    // substitue t back into ray and check bounds and distance
                    hitPoint = ray.origin + ray.direction * t;

                    if (hitPoint.X >= min.X && hitPoint.X <= max.X &&
                        hitPoint.Z >= min.Z && hitPoint.Z <= max.Z &&
                        (!hit || t < lowt))
                    {
                        hit  = true;
                        lowt = t;
                    }
                }
            }

            // Min Z
            if (ray.origin.Z < min.Z && ray.direction.Z > 0)
            {
                t = (min.Z - ray.origin.Z) / ray.direction.Z;

                if (t > 0)
                {
                    // substitue t back into ray and check bounds and distance
                    hitPoint = ray.origin + ray.direction * t;

                    if (hitPoint.X >= min.X && hitPoint.X <= max.X &&
                        hitPoint.Y >= min.Y && hitPoint.Y <= max.Y &&
                        (!hit || t < lowt))
                    {
                        hit  = true;
                        lowt = t;
                    }
                }
            }

            // Max Z
            if (ray.origin.Z > max.Z && ray.direction.Z < 0)
            {
                t = (max.Z - ray.origin.Z) / ray.direction.Z;

                if (t > 0)
                {
                    // substitue t back into ray and check bounds and distance
                    hitPoint = ray.origin + ray.direction * t;

                    if (hitPoint.X >= min.X && hitPoint.X <= max.X &&
                        hitPoint.Y >= min.Y && hitPoint.Y <= max.Y &&
                        (!hit || t < lowt))
                    {
                        hit  = true;
                        lowt = t;
                    }
                }
            }

            return(new IntersectResult(hit, lowt));
        }
예제 #4
0
파일: Ray.cs 프로젝트: mesh42/vpnet
 /// <summary>
 ///    Tests whether this ray intersects the given box.
 /// </summary>
 /// <param name="box"></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(AxisAlignedBox box)
 {
     return(MathUtil.Intersects(this, box));
 }
예제 #5
0
 public override object Parse(string text, CultureInfo culture)
 {
     return(AxisAlignedBox.Parse(text));
 }
예제 #6
0
        public override object[] GetConstructorArguments(object value)
        {
            AxisAlignedBox box = (AxisAlignedBox)value;

            return(new object[] { box.Minimum, box.Maximum });
        }
예제 #7
0
파일: Sphere.cs 프로젝트: mesh42/vpnet
 /// <summary>
 ///		Returns whether or not this sphere interects a box.
 /// </summary>
 /// <param name="box"></param>
 /// <returns>True if the box intersects, false otherwise.</returns>
 public bool Intersects(AxisAlignedBox box)
 {
     return(MathUtil.Intersects(this, box));
 }