unity3d数学公式之OBB vs AABB

  • Post author:
  • Post category:其他


上一篇写了OBB vs OBB,这一篇就把OBB vs AABB放出来吧 废话不多说 直接上代码 需要的朋友直接拿走

//OBB vs AABB
    public static bool IsOBBIntersectionWithAABB(ref Vector2 blOBB, ref Vector2 tlOBB, ref Vector2 trOBB, ref Vector2 brOBB,
        ref Vector2 blAABB, ref Vector2 tlAABB, ref Vector2 trAABB, ref Vector2 brAABB)
    {
        float minOBBx = Mathf.Min(blOBB.x, tlOBB.x);
        minOBBx = Mathf.Min(minOBBx, trOBB.x);
        minOBBx = Mathf.Min(minOBBx, brOBB.x);
        if (minOBBx > trAABB.x) return false;

        float maxOBBx = Mathf.Max(blOBB.x, tlOBB.x);
        maxOBBx = Mathf.Max(maxOBBx, trOBB.x);
        maxOBBx = Mathf.Max(maxOBBx, brOBB.x);
        if (maxOBBx < blAABB.x) return false;

        float minOBBy = Mathf.Min(blOBB.y, tlOBB.y);
        minOBBy = Mathf.Min(minOBBy, trOBB.y);
        minOBBy = Mathf.Min(minOBBy, brOBB.y);
        if (minOBBy > trAABB.y) return false;

        float maxOBBy = Mathf.Max(blOBB.y, tlOBB.y);
        maxOBBy = Mathf.Max(maxOBBy, trOBB.y);
        maxOBBy = Mathf.Max(maxOBBy, brOBB.y);
        if (maxOBBy < blAABB.y) return false;

        Vector2 axisX = trOBB - tlOBB;
        if (!IsOBBOverlapedOnAxisX(ref axisX, ref blOBB, ref tlOBB, ref trOBB, ref brOBB, ref blAABB, ref tlAABB, ref trAABB, ref brAABB)) return false;
        Vector2 axisY = trOBB - brOBB;
        if (!IsOBBOverlapedOnAxisY(ref axisY, ref blOBB, ref tlOBB, ref trOBB, ref brOBB, ref blAABB, ref tlAABB, ref trAABB, ref brAABB)) return false;

        return true;
    }

//两个OBB是否是某个方向上投影有重合
    private static bool IsOBBOverlapedOnAxisX(ref Vector2 axis, ref Vector2 blA, ref Vector2 tlA, ref Vector2 trA, ref Vector2 brA,
        ref Vector2 blB, ref Vector2 tlB, ref Vector2 trB, ref Vector2 brB)
    {
        float axisSqrLen = Vector2.Dot(axis, axis);
        if (IsApproximately(axisSqrLen, 0f)) return true;
        Vector2 scaleDir = axis / axisSqrLen;

        Vector2 axis_bla = Vector2.Dot(blA, axis) * scaleDir;
        Vector2 axis_tla = Vector2.Dot(tlA, axis) * scaleDir;
        Vector2 axis_tra = Vector2.Dot(trA, axis) * scaleDir;
        Vector2 axis_bra = Vector2.Dot(brA, axis) * scaleDir;

        Vector2 axis_blb = Vector2.Dot(blB, axis) * scaleDir;
        Vector2 axis_tlb = Vector2.Dot(tlB, axis) * scaleDir;
        Vector2 axis_trb = Vector2.Dot(trB, axis) * scaleDir;
        Vector2 axis_brb = Vector2.Dot(brB, axis) * scaleDir;

        float minAX = Mathf.Min(axis_bla.x, axis_tla.x);
        minAX = Mathf.Min(axis_tra.x, minAX);
        minAX = Mathf.Min(axis_bra.x, minAX);
        float maxBX = Mathf.Max(axis_blb.x, axis_tlb.x);
        maxBX = Mathf.Max(axis_trb.x, maxBX);
        maxBX = Mathf.Max(axis_brb.x, maxBX);
        if (minAX > maxBX) return false;

        float maxAX = Mathf.Max(axis_bla.x, axis_tla.x);
        maxAX = Mathf.Max(axis_tra.x, maxAX);
        maxAX = Mathf.Max(axis_bra.x, maxAX);
        float minBX = Mathf.Min(axis_blb.x, axis_tlb.x);
        minBX = Mathf.Min(axis_trb.x, minBX);
        minBX = Mathf.Min(axis_brb.x, minBX);
        if (maxAX < minBX) return false;
        return true;
    }
    private static bool IsOBBOverlapedOnAxisY(ref Vector2 axis, ref Vector2 blA, ref Vector2 tlA, ref Vector2 trA, ref Vector2 brA,
       ref Vector2 blB, ref Vector2 tlB, ref Vector2 trB, ref Vector2 brB)
    {
        float axisSqrLen = Vector2.Dot(axis, axis);
        if (IsApproximately(axisSqrLen, 0f)) return true;
        Vector2 scaleDir = axis / axisSqrLen;

        Vector2 axis_bla = Vector2.Dot(blA, axis) * scaleDir;
        Vector2 axis_tla = Vector2.Dot(tlA, axis) * scaleDir;
        Vector2 axis_tra = Vector2.Dot(trA, axis) * scaleDir;
        Vector2 axis_bra = Vector2.Dot(brA, axis) * scaleDir;

        Vector2 axis_blb = Vector2.Dot(blB, axis) * scaleDir;
        Vector2 axis_tlb = Vector2.Dot(tlB, axis) * scaleDir;
        Vector2 axis_trb = Vector2.Dot(trB, axis) * scaleDir;
        Vector2 axis_brb = Vector2.Dot(brB, axis) * scaleDir;

        float minAY = Mathf.Min(axis_bla.y, axis_tla.y);
        minAY = Mathf.Min(axis_tra.y, minAY);
        minAY = Mathf.Min(axis_bra.y, minAY);
        float maxBY = Mathf.Max(axis_blb.y, axis_tlb.y);
        maxBY = Mathf.Max(axis_trb.y, maxBY);
        maxBY = Mathf.Max(axis_brb.y, maxBY);
        if (minAY > maxBY) return false;

        float maxAY = Mathf.Max(axis_bla.y, axis_tla.y);
        maxAY = Mathf.Max(axis_tra.y, maxAY);
        maxAY = Mathf.Max(axis_bra.y, maxAY);
        float minBY = Mathf.Min(axis_blb.y, axis_tlb.y);
        minBY = Mathf.Min(axis_trb.y, minBY);
        minBY = Mathf.Min(axis_brb.y, minBY);       
        if (maxAY < minBY) return false;
        return true;
    }

 //计算两个数字是否接近相等
    public static bool IsApproximately(double a, double b)
    {
        return IsApproximately(a, b, minValue);
    }

    //计算两个数字是否接近相等,阈值是dvalue
    public static bool IsApproximately(double a, double b, double dvalue)
    {
        double delta = a - b;
        return delta >= -dvalue && delta <= dvalue;
    }



版权声明:本文为u013895270原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。