Exemplo n.º 1
0
    public static bool Cross(Box2D box, Box2D other, Vector2_ tp, ref Vector2_ hit)
    {
        if (box.Intersect(other))
        {
            return(true);
        }

        var o = box.center_;

        tp -= o;
        other.Set(other.center_ - o, other.size_ + box.size_);
        box.SetCenter(Vector2_.zero);

        double minx = Mathd.Min(0, tp.x), maxx = Mathd.Max(0, tp.x), miny = Mathd.Min(0, tp.y), maxy = Mathd.Max(0, tp.y);

        var l1 = Mathd.Abs(other.leftEdge) < Mathd.Abs(other.rightEdge) ? other.leftEdge : other.rightEdge;
        var l2 = Mathd.Abs(other.topEdge) < Mathd.Abs(other.bottomEdge) ? other.topEdge : other.bottomEdge;

        bool b1 = false, b2 = false;

        if (other.leftEdge < 0 && other.rightEdge > 0)
        {
            goto _l2;
        }

_l1:
        b1 = true;
        if (l1 > minx && l1 < maxx)
        {
            var yy = tp.y / tp.x * l1;
            if (yy > other.bottomEdge && yy < other.topEdge)
            {
                hit.Set(l1, yy);
                hit += o;

                return(true);
            }
        }

        if (b2)
        {
            return(false);
        }

_l2:
        b2 = true;
        if (l2 > miny && l2 < maxy)
        {
            var xx = l2 * tp.x / tp.y;
            if (xx > other.leftEdge && xx < other.rightEdge)
            {
                hit.Set(xx, l2);
                hit += o;

                return(true);
            }
        }

        if (!b1)
        {
            goto _l1;
        }

        return(false);
    }