Пример #1
0
        public override bool CheckCollision(Collider c)
        {
            BoxCollider a = this;

            if (c is BoxCollider)
            {
                BoxCollider b = (BoxCollider)c;
                //return x1 < x2 + w2 and x2 < x1 + w1 and y1 < y2 + h2 and y2 < y1 + h1
                return((a.parent.x + a.x) < (b.parent.x + b.x) + b.width &&
                       (b.parent.x + b.x) < (a.parent.x + a.x) + a.width &&
                       (a.parent.y + a.y) < (b.parent.y + b.y) + b.height &&
                       (b.parent.y + b.y) < (a.parent.y + a.y) + a.height);
            }
            else if (c is CircleCollider)
            {
                CircleCollider b = (CircleCollider)c;
                //return x1 < x2 + w2 and x2 < x1 + w1 and y1 < y2 + h2 and y2 < y1 + h1
                return((a.parent.x + a.x) < (b.parent.x + b.x) + b.radius &&
                       (b.parent.x + b.x) - b.radius < (a.parent.x + a.x) + a.width &&
                       (a.parent.y + a.y) < (b.parent.y + b.y) + b.radius &&
                       (b.parent.y + b.y) - b.radius < (a.parent.y + a.y) + a.height);
            }

            return(false);
        }
Пример #2
0
        public override bool CheckCollision(Collider c)
        {
            CircleCollider a = this;

            if (c is CircleCollider)
            {
                CircleCollider b = (CircleCollider)c;
                // local dx = bx - ax
                // local dy = by - ay
                // local dist = math.sqrt(dx * dx + dy * dy)
                // return dist < ar + br

                int    dx   = (b.parent.x + b.x) - (a.parent.x + a.x);
                int    dy   = (b.parent.y + b.y) - (a.parent.y + a.y);
                double dist = Math.Sqrt(dx * dx + dy * dy);

                return(dist < a.radius + b.radius);
            }
            else if (c is BoxCollider)
            {
                BoxCollider b = (BoxCollider)c;
                //return x1 < x2 + w2 and x2 < x1 + w1 and y1 < y2 + h2 and y2 < y1 + h1
                return((a.parent.x + a.x) - a.radius < (b.parent.x + b.x) + b.width &&
                       (b.parent.x + b.x) < (a.parent.x + a.x) + a.radius &&
                       (a.parent.y + a.y) - a.radius < (b.parent.y + b.y) + b.height &&
                       (b.parent.y + b.y) < (a.parent.y + a.y) + a.radius);
            }

            return(false);
        }