Пример #1
0
    Vector2 GetOverlap(ICollidingEntity other)
    {
        Rect r1 = GetNextRect();
        Rect r2 = other.GetNextRect();

        Vector2 dir = (GetNextFramePosition() - new Vector2(this.transform.position.x, this.transform.position.y)).normalized;

        Vector2 overlap = new Vector2(0, 0);

        if (!other.invertedCollider)
        {
            //collides on the right
            if (dir.x > 0 && r1.xMax > r2.xMin)
            {
                overlap.x += (r1.xMax - r2.xMin);
            }
            //collides on the left
            if (dir.x < 0 && r1.xMin < r2.xMax)
            {
                overlap.x -= (r2.xMax - r1.xMin);
            }
            //collides on the top
            if (dir.y > 0 && r1.yMax > r2.yMin)
            {
                overlap.y += (r1.yMax - r2.yMin);
            }
            //collides on the bottom
            if (dir.y < 0 && r1.yMin < r2.yMax)
            {
                overlap.y -= (r2.yMax - r1.yMin);
            }
        }
        else
        {
            //collides on the right
            if (dir.x > 0 && r1.xMax > r2.xMax)
            {
                overlap.x += (r1.xMax - r2.xMax);
            }
            //collides on the left
            if (dir.x < 0 && r1.xMin < r2.xMin)
            {
                overlap.x -= (r2.xMin - r1.xMin);
            }
            //collides on the top
            if (dir.y > 0 && r1.yMax > r2.yMax)
            {
                overlap.y += (r1.yMax - r2.yMax);
            }
            //collides on the bottom
            if (dir.y < 0 && r1.yMin < r2.yMin)
            {
                overlap.y -= (r2.yMin - r1.yMin);
            }
        }
        return(overlap);
    }
Пример #2
0
    public bool Overlaps(ICollidingEntity other)
    {
        Rect r1 = GetNextRect();
        Rect r2 = other.GetNextRect();

        if (!invertedCollider && !other.invertedCollider)
        {
            return(r1.Overlaps(r2));
        }
        else if (!invertedCollider && other.invertedCollider)
        {
            if (!r1.Overlaps(r2))
            {
                return(true);
            }

            var a          = r2.Contains(new Vector2(r1.xMax, r1.yMax));
            var b          = r2.Contains(new Vector2(r1.xMax, r1.yMin));
            var c          = r2.Contains(new Vector2(r1.xMin, r1.yMax));
            var d          = r2.Contains(new Vector2(r1.xMin, r1.yMin));
            int pointCount = 0;
            if (a)
            {
                pointCount++;
            }
            if (b)
            {
                pointCount++;
            }
            if (c)
            {
                pointCount++;
            }
            if (d)
            {
                pointCount++;
            }

            return(pointCount < 4);
        }
        return(false);
    }