Example #1
0
        public static bool Intersect(Bounding.Cylinder a, Vector3 b, out object intersection) // CylHeight implemented
        {
            if (!Intersect(a, Math.ToVector2(b), out intersection))
            {
                return(false);
            }

            intersection = null;
            float z1 = a.Position.Z; float z2 = b.Z;

            return(z2 >= z1 && z2 <= (z1 + a.Height));
        }
        public static RSpatialRelation Relation(BoundingBox a, Bounding.Cylinder b)
        {
            // NOTE: Not tested thoroughly!

            /*
             * This all is based on finding the intersections between the axises of the box
             * */

            float m, n;

            float boxMinZ = a.Minimum.Z; float boxMaxZ = a.Maximum.Z;
            float cylMinZ = b.Position.Z; float cylMaxZ = cylMinZ + b.Height;

            RSpatialRelation verticalRelation = Relation1DLines(boxMinZ, boxMaxZ, cylMinZ, cylMaxZ);

            if (verticalRelation == RSpatialRelation.Outside)
            {
                return(RSpatialRelation.Outside);
            }

            // from this point on we know that the cylinder at least intersects the box on the vertical axis

            //X Axis, first find intersections
            CylP x1 = CylP.NotIntersecting;

            if (Math.CircleXAxisIntersection(b.Position, b.Radius, a.Minimum.Y, out m, out n))
            {
                if (m > a.Maximum.X || n < a.Minimum.X)
                {
                    x1 = CylP.Outside;
                }
                else if (m < a.Minimum.X && n > a.Maximum.X)
                {
                    x1 = CylP.AInsideB;
                }
                else
                {
                    return(RSpatialRelation.Intersect);
                }
            }

            CylP x2 = CylP.NotIntersecting;

            if (Math.CircleXAxisIntersection(b.Position, b.Radius, a.Maximum.Y, out m, out n))
            {
                if (m > a.Maximum.X || n < a.Minimum.X)
                {
                    x2 = CylP.Outside;
                }
                else if (m < a.Minimum.X && n > a.Maximum.X)
                {
                    x2 = CylP.AInsideB;
                }
                else
                {
                    return(RSpatialRelation.Intersect);
                }
            }

            //Y Axis, first find intersections
            CylP y1 = CylP.NotIntersecting;

            if (Math.CircleYAxisIntersection(b.Position, b.Radius, a.Minimum.X, out m, out n))
            {
                if (m > a.Maximum.Y || n < a.Minimum.Y)
                {
                    y1 = CylP.Outside;
                }
                else if (m < a.Minimum.Y && n > a.Maximum.Y)
                {
                    y1 = CylP.AInsideB;
                }
                else
                {
                    return(RSpatialRelation.Intersect);
                }
            }

            CylP y2 = CylP.NotIntersecting;

            if (Math.CircleYAxisIntersection(b.Position, b.Radius, a.Maximum.X, out m, out n))
            {
                if (m > a.Maximum.Y || n < a.Minimum.Y)
                {
                    y2 = CylP.Outside;
                }
                else if (m < a.Minimum.Y && n > a.Maximum.Y)
                {
                    y2 = CylP.AInsideB;
                }
                else
                {
                    return(RSpatialRelation.Intersect);
                }
            }

            if (x1 == CylP.AInsideB && x2 == CylP.AInsideB && y1 == CylP.AInsideB && y2 == CylP.AInsideB)
            {
                if (verticalRelation == RSpatialRelation.AInsideB)
                {
                    return(RSpatialRelation.AInsideB);
                }
                return(RSpatialRelation.Intersect);
            }

            //var horizontalRelation = RectCircleRelation(new RectangleF(new PointF(a.Minimum.X, a.Minimum.Y), new SizeF(a.Maximum.X - a.Minimum.X, a.Maximum.Y - a.Minimum.Y)), b);

            //if (horizontalRelation == RSpatialRelation.Outside)
            //    return RSpatialRelation.Outside;
            //else if (horizontalRelation == verticalRelation)
            //    return horizontalRelation;
            //else
            //    return RSpatialRelation.Intersect;

            if (x1 == CylP.NotIntersecting && x2 == CylP.NotIntersecting &&
                y1 == CylP.NotIntersecting && y2 == CylP.NotIntersecting &&
                Intersection.Intersect(a, Math.ToVector2(b.Position)))
            {
                if (verticalRelation == RSpatialRelation.BInsideA)
                {
                    return(RSpatialRelation.BInsideA);
                }
                return(RSpatialRelation.Intersect);
            }

            else
            {
                return(RSpatialRelation.Outside);
            }
#if false
            //We start by checking if the circle is so large it covers the whole rectangle
            bool intersects = false, ainsideb = true;

            if (p.CullPoint(position.X, position.Y) == ECull.Intersect)
            {
                intersects = true;
            }
            else
            {
                ainsideb = false;
            }

            if (p.CullPoint(position.X + size.X, position.Y) == ECull.Intersect)
            {
                intersects = true;
            }
            else
            {
                ainsideb = false;
            }

            if (p.CullPoint(position.X, position.Y + size.Y) == ECull.Intersect)
            {
                intersects = true;
            }
            else
            {
                ainsideb = false;
            }

            if (p.CullPoint(position.X + size.X, position.Y + size.Y) == ECull.Intersect)
            {
                intersects = true;
            }
            else
            {
                ainsideb = false;
            }

            if (ainsideb)
            {
                return(RSpatialRelation.AInsideB);
            }
            if (intersects)
            {
                return(RSpatialRelation.Intersect);
            }

            //The find the point on the circle which is closest to the center of the rectangle
            Vector3 k = center - p.Position;
            if (k.LengthSquared() == 0)
            {
                k.X = 1;
            }
            Vector3 d = Vector3.Normalize(k) * p.Radius;

            Vector3 a = p.Position + d;

            //And check if it is outside the rectangle
            if (CullPoint(a.X, a.Y) == ECull.Outside)
            {
                return(RSpatialRelation.Outside);
            }

            //The we take the point furthest away from the center
            Vector3 b = p.Position - d;
            if (CullPoint(b.X, b.Y) == ECull.Outside)
            {
                return(RSpatialRelation.Intersect);
            }

            return(RSpatialRelation.BInsideA);
#endif
        }
        private static RSpatialRelation RectCircleRelation(RectangleF a, Bounding.Cylinder b)
        {
            float m, n;

            //X Axis, first find intersections
            CylP x1 = CylP.NotIntersecting;

            if (Math.CircleXAxisIntersection(b.Position, b.Radius, a.Y, out m, out n))
            {
                if (m > a.X + a.Width || n < a.X)
                {
                    x1 = CylP.Outside;
                }
                else if (m < a.X && n > a.X + a.Width)
                {
                    x1 = CylP.AInsideB;
                }
                else
                {
                    return(RSpatialRelation.Intersect);
                }
            }

            CylP x2 = CylP.NotIntersecting;

            if (Math.CircleXAxisIntersection(b.Position, b.Radius, a.Y + a.Height, out m, out n))
            {
                if (m > a.X + a.Height || n < a.X)
                {
                    x2 = CylP.Outside;
                }
                else if (m < a.X && n > a.X + a.Height)
                {
                    x2 = CylP.AInsideB;
                }
                else
                {
                    return(RSpatialRelation.Intersect);
                }
            }

            //Y Axis, first find intersections
            CylP y1 = CylP.NotIntersecting;

            if (Math.CircleYAxisIntersection(b.Position, b.Radius, a.X, out m, out n))
            {
                if (m > a.Y + a.Height || n < a.Y)
                {
                    y1 = CylP.Outside;
                }
                else if (m < a.Y && n > a.Y + a.Height)
                {
                    y1 = CylP.AInsideB;
                }
                else
                {
                    return(RSpatialRelation.Intersect);
                }
            }

            CylP y2 = CylP.NotIntersecting;

            if (Math.CircleYAxisIntersection(b.Position, b.Radius, a.X + a.Width, out m, out n))
            {
                if (m > a.Y + a.Height || n < a.Y)
                {
                    y2 = CylP.Outside;
                }
                else if (m < a.Y && n > a.Y + a.Height)
                {
                    y2 = CylP.AInsideB;
                }
                else
                {
                    return(RSpatialRelation.Intersect);
                }
            }

            if (x1 == CylP.AInsideB && x2 == CylP.AInsideB && y1 == CylP.AInsideB && y2 == CylP.AInsideB)
            {
                return(RSpatialRelation.AInsideB);
            }

            if (x1 == CylP.NotIntersecting && x2 == CylP.NotIntersecting &&
                y1 == CylP.NotIntersecting && y2 == CylP.NotIntersecting &&
                Intersection.Intersect(a, Math.ToVector2(b.Position)))
            {
                return(RSpatialRelation.BInsideA);
            }

            else
            {
                return(RSpatialRelation.Outside);
            }
        }