コード例 #1
0
ファイル: AxisAlignedBox.cs プロジェクト: mesh42/awmanaged
        /// <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
ファイル: MathUtil.cs プロジェクト: mesh42/awmanaged
        /// <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
ファイル: Converters.cs プロジェクト: mesh42/awmanaged
 public override object Parse(string text, CultureInfo culture)
 {
     return(AxisAlignedBox.Parse(text));
 }
コード例 #5
0
ファイル: Converters.cs プロジェクト: mesh42/awmanaged
        public override object[] GetConstructorArguments(object value)
        {
            AxisAlignedBox box = (AxisAlignedBox)value;

            return(new object[] { box.Minimum, box.Maximum });
        }
コード例 #6
0
ファイル: Ray.cs プロジェクト: mesh42/awmanaged
 /// <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));
 }
コード例 #7
0
 /// <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));
 }