Example #1
0
 // dirty as it only checks whether one contains a point of the other (analog to polygon case)
 internal static bool CollideDiamondBoundingBox(ICollidible collidibleDiamond, ICollidible collidibleBox)
 {
     if (CollideBoundingBoxBoundingBox(collidibleDiamond, collidibleBox))
     {
         var diamondBox = ((CCNode)collidibleDiamond).BoundingBoxTransformedToWorld;
         var diamond    = BoxToDiamond(diamondBox);
         var box        = ((CCNode)collidibleBox).BoundingBoxTransformedToWorld;
         var boxPoints  = Constants.CCRectPoints(box);
         foreach (var point in diamond)
         {
             if (box.ContainsPoint(point))
             {
                 return(true);
             }
         }
         foreach (var point in boxPoints)
         {
             if (CollideDiamondPosition(diamondBox, diamondBox.Center, point))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Example #2
0
 // dirty, because only the center of the bounding box + all points of the box are used to check the angle;
 // this function is using CCDegrees
 internal static bool CollideArcBoundingBox(CCPoint posCircle, float radius, float angleArcCenter, float angleHalfArcWidth, CCRect box)
 {
     if (CollideBoundingBoxCircle(box, posCircle, radius))
     {
         // check whether the center of the box is inside the arc
         CCPoint vectorCirclePoint = box.Center - posCircle;
         float   anglePoint        = Constants.DxDyToCCDegrees(vectorCirclePoint.X, vectorCirclePoint.Y);
         float   angleDifference   = Constants.AbsAngleDifferenceDeg(angleArcCenter, anglePoint);
         if (angleDifference <= angleHalfArcWidth)
         {
             return(true);
         }
         // check whether a point of the box is inside the arc
         foreach (CCPoint point in Constants.CCRectPoints(box))
         {
             vectorCirclePoint = point - posCircle;
             anglePoint        = Constants.DxDyToCCDegrees(vectorCirclePoint.X, vectorCirclePoint.Y);
             angleDifference   = Constants.AbsAngleDifferenceDeg(angleArcCenter, anglePoint);
             if (angleDifference <= angleHalfArcWidth)
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Example #3
0
        // dirty as it only checks whether one contains a point of the other
        internal static bool CollideBoundingBoxPolygon(ICollidible boxCollidible, ICollidible polyCollidible, CollisionTypePolygon cTypePoly)
        {
            // first check the bounding box of the polygon (for performance)
            CCRect box = ((CCNode)boxCollidible).BoundingBoxTransformedToWorld;

            if (box.IntersectsRect(((CCNode)polyCollidible).BoundingBoxTransformedToWorld))
            {
                CCPoint[] boxPoints = Constants.CCRectPoints(box);
                // transform the polygon to match the positioning, rotation and scale of the node
                Polygon transformedPolygon = ((Polygon)cTypePoly.collisionPolygon.Clone());
                transformedPolygon.TransformAccordingToGameObject(polyCollidible);
                foreach (var point in boxPoints)
                {
                    if (transformedPolygon.ContainsPoint(point))
                    {
                        return(true);
                    }
                }
                foreach (var point in transformedPolygon.Points)
                {
                    if (box.ContainsPoint(point))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Example #4
0
        internal static bool CollideBoundingBoxLine(ICollidible boxCollidible, CollisionTypeLine cTypeLine)
        {
            CCRect box = ((CCNode)boxCollidible).BoundingBoxTransformedToWorld;
            float  sx  = cTypeLine.StartPoint.X;
            float  sy  = cTypeLine.StartPoint.Y;
            float  ex  = cTypeLine.EndPoint.X;
            float  ey  = cTypeLine.EndPoint.Y;

            // for performance first check whether both points of the line are outside and on one side of the box
            if ((sx < box.MinX && ex < box.MinX) ||
                (sx > box.MaxX && ex > box.MaxX) ||
                (sy < box.MinY && ey < box.MinY) ||
                (sy > box.MaxY && ey > box.MaxY))
            {
                return(false);
            }
            // check whether the start or end point is contained in the box
            if (box.ContainsPoint(cTypeLine.StartPoint) || box.ContainsPoint(cTypeLine.EndPoint))
            {
                return(true);
            }
            // check for intersections of the line and the box boundaries
            CCPoint[] boxPoints = Constants.CCRectPoints(box);
            for (int i = 0; i < 3; i++)
            {
                if (CCPoint.SegmentIntersect(cTypeLine.StartPoint, cTypeLine.EndPoint, boxPoints[i], boxPoints[i + 1]))
                {
                    return(true);
                }
            }
            return(false);
        }
Example #5
0
        internal static bool CollideBoundingBoxCircle(CCRect box, CCPoint circlePos, float radius)
        {
            // for peformance first approximate the box with a circle and check whether these two collide
            // if they don't then the circle can't collide with the box either
            float boxRadius = box.Size.Width > box.Size.Height ? box.Size.Width : box.Size.Height;

            if (!CollideCircleCircle(circlePos, radius, box.Center, boxRadius))
            {
                return(false);
            }
            // check whether the circle center is inside the bounding box
            if (box.ContainsPoint(circlePos))
            {
                return(true);
            }
            // check if the circle collides with the lines of the box
            var boxPoints = Constants.CCRectPoints(box);
            int i, j;

            for (i = 0, j = 3; i < 4; j = i++)
            {
                if (CollideCircleLine(circlePos, radius, boxPoints[i], boxPoints[j]))
                {
                    return(true);
                }
            }
            return(false);
        }