Esempio n. 1
0
    //圆与矩形
    public static bool RadiusToRectangle(CollRadius CollA, CollRectange CollB)
    {
        //长轴距离过滤
        if (Vector2.Distance(CollA.GetV2Pos(), CollB.GetV2Pos()) > CollB.GetLength() + CollA.Radius)
        {
            return(false);
        }

        //获得圆相对矩形中心的相对坐标
        float newRx = CollA.x - CollB.x;
        float newRy = CollA.y - CollB.y;

        if (Mathf.RoundToInt(CollB.GetCalRotate()) != 0)
        {
            //计算最新角度(与X轴的角度),同数学X Y轴 以矩形中心为坐标中心
            float rotate = Mathf.Atan((CollB.y - CollA.y) / (CollB.x - CollA.x)) * Mathf.Rad2Deg;
            float newrot = rotate - CollB.GetCalRotate(); //减去矩形旋转角度 与矩形同时旋转对应的角度

            float distance = Vector2.Distance(CollA.GetV2Pos(), CollB.GetV2Pos());
            //圆相对矩形的 x y
            newRx = Mathf.Cos(newrot * Mathf.Deg2Rad) * distance;
            newRy = Mathf.Sin(newrot * Mathf.Deg2Rad) * distance;
        }


        //矩形 与圆 相交公式 获得在矩形 x y范围内 与圆心最近的点
        float dx  = Mathf.Min(newRx, CollB.width * 0.5f);
        float dx1 = Mathf.Max(dx, -CollB.width * 0.5f);
        float dy  = Mathf.Min(newRy, CollB.height * 0.5f);
        float dy1 = Mathf.Max(dy, -CollB.height * 0.5f);

        return((dx1 - newRx) * (dx1 - newRx) + (dy1 - newRy) * (dy1 - newRy) <= CollA.Radius * CollA.Radius);
    }
Esempio n. 2
0
    //矩形与扇形(分离轴 三角形碰撞)
    public static bool RectangleToSector(CollRectange CollA, CollSector CollB)
    {
        //长轴距离过滤
        if (Vector2.Distance(CollA.GetV2Pos(), CollB.GetV2Pos()) > CollB.outCircle + CollA.GetLength())
        {
            return(false);
        }

        //扇形两个顶点
        float angle = CollB.angle + CollB.GetCalRotate();
        float x1    = CollB.x + Mathf.Cos(angle * 0.5f * Mathf.Deg2Rad);
        float y1    = CollB.y + Mathf.Sin(angle * 0.5f * Mathf.Deg2Rad);

        float x2 = CollB.x + Mathf.Cos(-angle * 0.5f * Mathf.Deg2Rad);
        float y2 = CollB.y + Mathf.Sin(-angle * 0.5f * Mathf.Deg2Rad);


        //三角形三个顶点
        Vector2[] secPos = new Vector2[] { CollB.GetV2Pos(), new Vector2(x1, y1), new Vector2(x2, y2) };
        //三角形三条中轴
        //Vector2[] secAxies = new Vector2[] { GetV2Nor(secPos[0] - secPos[1]), GetV2Nor(secPos[0] - secPos[2]), GetV2Nor(secPos[1] - secPos[2]) };
        //矩形四个顶点
        float rectR  = CollA.extents.magnitude;
        float rotate = Mathf.Atan((CollA.width * 0.5f) / (CollA.height * 0.5f)) * Mathf.Rad2Deg + CollA.GetCalRotate();
        float x      = Mathf.Cos(rotate) * rectR;
        float y      = Mathf.Sin(rotate) * rectR;

        Vector2[] rectPos = new Vector2[] {
            new Vector2(CollA.x + x, CollA.y + y),
            new Vector2(CollA.x + x, CollA.y - y),
            new Vector2(CollA.x - x, CollA.y + y),
            new Vector2(CollA.x - x, CollA.y - y)
        };
        //矩形中轴
        //Vector2[] rectAxies = new Vector2[] { GetV2Nor(secPos[0] - secPos[1]), GetV2Nor(secPos[0] - secPos[3]), GetV2Nor(secPos[1] - secPos[2]),GetV2Nor(secPos[2] - secPos[3]) };

        Vector2[] axises = new Vector2[] {
            GetV2Nor(secPos[0] - secPos[1]), GetV2Nor(secPos[0] - secPos[2]), GetV2Nor(secPos[1] - secPos[2]),
            GetV2Nor(secPos[0] - secPos[1]), GetV2Nor(secPos[0] - secPos[3]), GetV2Nor(secPos[1] - secPos[2]), GetV2Nor(secPos[2] - secPos[3])
        };

        for (int i = 0, len = axises.Length; i < len; i++)
        {
            var axis = axises[i];

            Vector2 proA = GetProjection(axis, secPos);
            Vector2 proB = GetProjection(axis, rectPos);

            if (IsOverlay(proA, proB))
            {
                return(false);
            }
        }

        return(true);
    }