public static Vector2 PushSpeedVector(CircleCollider main, CircleCollider second)
        {
            Vector2 pushV = second.GetCenter() - main.GetCenter();

            pushV.Normalize();
            return(pushV * pushOutSpeed);
        }
        public static bool DoCollide(CircleCollider c1, CircleCollider c2)
        {
            float dist   = MathUtils.GetDistance(c1.GetCenter(), c2.GetCenter());
            float radsum = c1.GetRadius() + c2.GetRadius();

            return(dist <= radsum);
        }
Exemple #3
0
    /*
     *  Calculate Collision Data
     */

    // Circle & Circle
    private bool CalculateCollisionData(CircleCollider a, CircleCollider b)
    {
        Vector2 centerA        = a.GetCenter();
        Vector2 centerB        = b.GetCenter();
        Vector2 centerDelta    = centerB - centerA;
        float   combinedRadius = a.radius + b.radius;

        if (centerDelta.sqrMagnitude <= combinedRadius * combinedRadius)
        {
            contactNormal    = centerDelta.normalized;
            penetrationDepth = combinedRadius - centerDelta.magnitude;
            contactPoint     = centerA + contactNormal * (a.radius - penetrationDepth * 0.5f);
            return(true);
        }

        return(false);
    }
Exemple #4
0
        private static bool CollideRectCirc(RectangleCollider a, CircleCollider b)
        {
            if (a.IntersectsPoint(b.GetCenter()))
                return true;

            //Check the circle against the four edges of the rectangle
            Vector2 pA = a.GetTopLeft();
            Vector2 pB = a.GetTopRight();
            Vector2 pC = a.GetBottomRight();
            Vector2 pD = a.GetBottomLeft();
            if (b.IntersectsLine(pA, pB, 0) || b.IntersectsLine(pB, pC, 0) || b.IntersectsLine(pC, pD, 0) || b.IntersectsLine(pD, pA, 0))
                return true;

            return false;
        }
Exemple #5
0
 private static bool CollideCircCirc(CircleCollider a, CircleCollider b)
 {
     Vector2 diff = b.GetCenter() - a.GetCenter();
     return (diff.X * diff.X + diff.Y * diff.Y < Math.Pow(XP.Distance(b.GetCenter(), a.GetCenter()),2));
 }
Exemple #6
0
    // Circle & Rectangle
    private bool CalculateCollisionData(CircleCollider a, RectangleCollider b)
    {
        Vector2 centerA = Util.RotateVector2(a.GetCenter() - b.GetCenter(), -b.attachedPhysicsBody.rotation);

        Vector2 inflatedExtents = b.extent + Vector2.one * a.radius;

        if (centerA.x <= inflatedExtents.x && centerA.x >= -inflatedExtents.x && centerA.y <= inflatedExtents.y && centerA.y >= -inflatedExtents.y)
        {
            Vector2 absCenterA = new Vector2(Mathf.Abs(centerA.x), Mathf.Abs(centerA.y));
            //In Corner
            if (absCenterA.x >= b.extent.x && absCenterA.y >= b.extent.y)
            {
                if ((absCenterA - b.extent).sqrMagnitude <= a.radius * a.radius)
                {
                    Vector2 mirrorDir = new Vector2(Mathf.Sign(centerA.x), Mathf.Sign(centerA.y));
                    contactPoint     = b.extent;
                    contactNormal    = (contactPoint - absCenterA).normalized * mirrorDir;
                    contactPoint    *= mirrorDir;
                    penetrationDepth = a.radius - (absCenterA - b.extent).magnitude;
                }
                else
                {
                    return(false);
                }
            }
            //Right
            else if (centerA.x > b.extent.x)
            {
                contactPoint     = new Vector2((b.extent.x + centerA.x - a.radius) * 0.5f, centerA.y);
                contactNormal    = Vector2.left;
                penetrationDepth = Mathf.Abs(b.extent.x - (centerA.x - a.radius));
            }
            //Left
            else if (centerA.x < -b.extent.x)
            {
                contactPoint     = new Vector2((-b.extent.x + centerA.x + a.radius) * 0.5f, centerA.y);
                contactNormal    = Vector2.right;
                penetrationDepth = Mathf.Abs(-b.extent.x - (centerA.x + a.radius));
            }
            //Up
            else if (centerA.y > b.extent.y)
            {
                contactPoint     = new Vector2(centerA.x, (b.extent.y + centerA.y - a.radius) * 0.5f);
                contactNormal    = Vector2.down;
                penetrationDepth = Mathf.Abs(b.extent.y - (centerA.y - a.radius));
            }
            //Down
            else if (centerA.y < -b.extent.y)
            {
                contactPoint     = new Vector2(centerA.x, (-b.extent.y + centerA.y + a.radius) * 0.5f);
                contactNormal    = Vector2.up;
                penetrationDepth = Mathf.Abs(-b.extent.y - (centerA.y + a.radius));
            }
            else
            {
                return(false);
            }
            contactPoint  = b.GetCenter() + Util.RotateVector2(contactPoint, b.attachedPhysicsBody.rotation);
            contactNormal = Util.RotateVector2(contactNormal, b.attachedPhysicsBody.rotation);
            return(true);
        }

        return(false);
    }