Esempio n. 1
0
        static public bool RectToCircle(float rectX, float rectY, float rectWidth, float rectHeight,
                                        Vector2 circleCenter, float radius)
        {
            //Check if the rectangle contains the circle's center-point
            if (Collisions.RectToPoint(rectX, rectY, rectWidth, rectHeight, circleCenter))
            {
                return(true);
            }

            // Check the circle against the relevant edges
            Vector2 edgeFrom;
            Vector2 edgeTo;
            var     sector = GetSector(rectX, rectY, rectWidth, rectHeight, circleCenter);

            if ((sector & PointSectors.Top) != 0)
            {
                edgeFrom = new Vector2(rectX, rectY);
                edgeTo   = new Vector2(rectX + rectWidth, rectY);
                if (CircleToLine(circleCenter, radius, edgeFrom, edgeTo))
                {
                    return(true);
                }
            }

            if ((sector & PointSectors.Bottom) != 0)
            {
                edgeFrom = new Vector2(rectX, rectY + rectHeight);
                edgeTo   = new Vector2(rectX + rectWidth, rectY + rectHeight);
                if (CircleToLine(circleCenter, radius, edgeFrom, edgeTo))
                {
                    return(true);
                }
            }

            if ((sector & PointSectors.Left) != 0)
            {
                edgeFrom = new Vector2(rectX, rectY);
                edgeTo   = new Vector2(rectX, rectY + rectHeight);
                if (CircleToLine(circleCenter, radius, edgeFrom, edgeTo))
                {
                    return(true);
                }
            }

            if ((sector & PointSectors.Right) != 0)
            {
                edgeFrom = new Vector2(rectX + rectWidth, rectY);
                edgeTo   = new Vector2(rectX + rectWidth, rectY + rectHeight);
                if (CircleToLine(circleCenter, radius, edgeFrom, edgeTo))
                {
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 2
0
        static public bool rectToLine(float rectX, float rectY, float rectWidth, float rectHeight, Vector2 lineFrom, Vector2 lineTo)
        {
            var fromSector = Collisions.getSector(rectX, rectY, rectWidth, rectHeight, lineFrom);
            var toSector   = Collisions.getSector(rectX, rectY, rectWidth, rectHeight, lineTo);

            if (fromSector == PointSectors.Center || toSector == PointSectors.Center)
            {
                return(true);
            }
            else if ((fromSector & toSector) != 0)
            {
                return(false);
            }
            else
            {
                var both = fromSector | toSector;

                // Do line checks against the edges
                Vector2 edgeFrom;
                Vector2 edgeTo;

                if ((both & PointSectors.Top) != 0)
                {
                    edgeFrom = new Vector2(rectX, rectY);
                    edgeTo   = new Vector2(rectX + rectWidth, rectY);
                    if (Collisions.lineToLine(edgeFrom, edgeTo, lineFrom, lineTo))
                    {
                        return(true);
                    }
                }

                if ((both & PointSectors.Bottom) != 0)
                {
                    edgeFrom = new Vector2(rectX, rectY + rectHeight);
                    edgeTo   = new Vector2(rectX + rectWidth, rectY + rectHeight);
                    if (Collisions.lineToLine(edgeFrom, edgeTo, lineFrom, lineTo))
                    {
                        return(true);
                    }
                }

                if ((both & PointSectors.Left) != 0)
                {
                    edgeFrom = new Vector2(rectX, rectY);
                    edgeTo   = new Vector2(rectX, rectY + rectHeight);
                    if (Collisions.lineToLine(edgeFrom, edgeTo, lineFrom, lineTo))
                    {
                        return(true);
                    }
                }

                if ((both & PointSectors.Right) != 0)
                {
                    edgeFrom = new Vector2(rectX + rectWidth, rectY);
                    edgeTo   = new Vector2(rectX + rectWidth, rectY + rectHeight);
                    if (Collisions.lineToLine(edgeFrom, edgeTo, lineFrom, lineTo))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }