碰撞检测之OBB-OBB检测

2D情况

首先回顾一下SAP



两个凸包多边形,当且仅当存在一条线,这两个多边形在这条线上的投影不相交,则这两个多边形也不相交.


这条线称为Separating Axis.垂直Separating Axis存在一条Separating Line将两个多边形分开。


这里还有一个要确定的,就是如果两个矩形之间存在Separating Line,则一定存在一条和两个矩形中的一条边平行。每个矩形对边平行,则我们只需要检查四个方向是否存在Separating Line,如下图



找Separating Line 就是找Separating Axis, Separating Axis也只有四个方向,所以将矩形在四个轴上投影就可以了,如下图









下面来看具体的计算


定义下面几个变量

PA = coordinate position of the center of rectangle A
Ax = unit vector representing the local x-axis of A
Ay = unit vector representing the local y-axis of A
WA = half width of A (corresponds with the local x-axis of A)
HA = half height of A (corresponds with the local y-axis of A)


PB = coordinate position of the center of rectangle B

Bx = unit vector representing the local x-axis of B
By = unit vector representing the local y-axis of B
WB = half width of B (corresponds with the local x-axis of B)
HB = half height of B (corresponds with the local y-axis of B)


T= PB - PA

轴L是Seprating Axis的条件是

|Proj ( T )| > 0.5 * |Proj ( RectangleA )| + 0.5 *|Proj ( RectangleB )|


Proj是投影计算, 展开

| T • L | > | ( WA*Ax ) • L | + | ( HA*Ay ) • L | + | ( WB*Bx ) • L | + |( HB*By ) • L |

L只有四种情况,AX, AY, BX, BY

CASE 1:
// L = Ax
| T • Ax | > | ( WA*Ax ) • Ax | + | ( HA*Ay ) • Ax | + | ( WB*Bx ) • Ax | + |( HB*By ) • Ax |
| T • Ax | > WA + 0 + | ( WB*Bx ) • Ax | + |( HB*By ) • Ax |
| T • Ax | > WA + | ( WB*Bx ) • Ax | + |( HB*By ) • Ax |

如果成立,存在Separating Axis平行Ax。


CASE 2:
// L = Ay
| T • Ay | > HA + | ( WB*Bx ) • Ay | + |( HB*By ) • Ay |

如果成立,存在Separating Axis平行Ay。


CASE 3:
// L = Bx
| T • Bx | > | ( WA* Ax ) • Bx | + | ( HA*Ay ) • Bx | + WB

如果成立,存在Separating Axis平行Bx。


CASE 4:
// L = By
| T • By | > | ( WA* Ax ) • By | + | ( HA*Ay ) • By | + HB

如果成立,存在Separating Axis平行By。



三维情况

在三维情况下的,之前的Separating Line就变成了Separating Plane,如下图



每个Box都有三组面,每组面都是平行的,Separating Plane都是平行其中的一个面。则两个box的SAT中可能的Separating Axis有六个




下图中Separating Plane就平行于右边Box的一个面



然而还有一种情况,如下



这种情况,两个Box并不相交,但是他们并没有发生碰撞,这种情况,Separating Plane的法线是两条红线的叉乘



所以在三维情况下Box和Box的碰撞检测需要判定的情况有6+9种

CASE 1:L = Ax  CASE 2:L = Ay  CASE 3:L = Az   CASE 4:L = Bx   CASE 5:L = By  CASE 6:L = Bz

CASE 7:L = Ax Bx  CASE 8:L = Ax By  CASE 9:L = Ax Bz  CASE 10:L = Ay Bx  

CASE 11:L = Ay By  CASE 12:L = Ay Bz  CASE 13:L = Az Bx  CASE 14:L = Az By  CASE 15:L = Az Bz


判断条件还是

| T • L | > | ( WA*Ax ) • L | + | ( HA*Ay ) • L | + |( DA*Az ) • L |+ | ( WB*Bx ) • L | + |( HB*By ) • L | + |( DB*Bz ) • L |

一点优化,关于T • L 

T • (Ax * Bx) =(T •  Az)(Ay •  Bx) - (T•  Ay)(Az •  Bx)

这里将有叉乘的地方进行了转化,要看证明的请看参考资料。


好,可以上代码了

  public static bool IntersectBoxBox(Box box0, Box box1)
        {
            Vector3 v = box1.center - box0.center;

            //Compute A's basis
            Vector3 VAx = box0.rotation * new Vector3(1, 0, 0);
            Vector3 VAy = box0.rotation * new Vector3(0, 1, 0);
            Vector3 VAz = box0.rotation * new Vector3(0, 0, 1);

            Vector3[] VA = new Vector3[3];
            VA[0] = VAx;
            VA[1] = VAy;
            VA[2] = VAz;

            //Compute B's basis
            Vector3 VBx = box1.rotation * new Vector3(1, 0, 0);
            Vector3 VBy = box1.rotation * new Vector3(0, 1, 0);
            Vector3 VBz = box1.rotation * new Vector3(0, 0, 1);

            Vector3[] VB = new Vector3[3];
            VB[0] = VBx;
            VB[1] = VBy;
            VB[2] = VBz;

            Vector3 T = new Vector3(Vector3.Dot(v, VAx), Vector3.Dot(v, VAy), Vector3.Dot(v, VAz));

            float[,] R = new float[3, 3];
            float[,] FR = new float[3, 3];
            float ra, rb, t;

            for (int i = 0; i < 3; i++)
            {
                for (int k = 0; k < 3; k++)
                {
                    R[i, k] = Vector3.Dot(VA[i], VB[k]);
                    FR[i, k] = 1e-6f + Mathf.Abs(R[i, k]);
                }
            }

            // A's basis vectors
            for (int i = 0; i < 3; i++)
            {
                ra = box0.extents[i];
                rb = box1.extents[0] * FR[i, 0] + box1.extents[1] * FR[i, 1] + box1.extents[2] * FR[i, 2];
                t = Mathf.Abs(T[i]);
                if (t > ra + rb) return false;
            }

            // B's basis vectors
            for (int k = 0; k < 3; k++)
            {
                ra = box0.extents[0] * FR[0, k] + box0.extents[1] * FR[1, k] + box0.extents[2] * FR[2, k];
                rb = box1.extents[k];
                t = Mathf.Abs(T[0] * R[0, k] + T[1] * R[1, k] + T[2] * R[2, k]);
                if (t > ra + rb) return false;
            }

            //9 cross products

            //L = A0 x B0
            ra = box0.extents[1] * FR[2, 0] + box0.extents[2] * FR[1, 0];
            rb = box1.extents[1] * FR[0, 2] + box1.extents[2] * FR[0, 1];
            t = Mathf.Abs(T[2] * R[1, 0] - T[1] * R[2, 0]);
            if (t > ra + rb) return false;

            //L = A0 x B1
            ra = box0.extents[1] * FR[2, 1] + box0.extents[2] * FR[1, 1];
            rb = box1.extents[0] * FR[0, 2] + box1.extents[2] * FR[0, 0];
            t = Mathf.Abs(T[2] * R[1, 1] - T[1] * R[2, 1]);
            if (t > ra + rb) return false;

            //L = A0 x B2
            ra = box0.extents[1] * FR[2, 2] + box0.extents[2] * FR[1, 2];
            rb = box1.extents[0] * FR[0, 1] + box1.extents[1] * FR[0, 0];
            t = Mathf.Abs(T[2] * R[1, 2] - T[1] * R[2, 2]);
            if (t > ra + rb) return false;

            //L = A1 x B0
            ra = box0.extents[0] * FR[2, 0] + box0.extents[2] * FR[0, 0];
            rb = box1.extents[1] * FR[1, 2] + box1.extents[2] * FR[1, 1];
            t = Mathf.Abs(T[0] * R[2, 0] - T[2] * R[0, 0]);
            if (t > ra + rb) return false;

            //L = A1 x B1
            ra = box0.extents[0] * FR[2, 1] + box0.extents[2] * FR[0, 1];
            rb = box1.extents[0] * FR[1, 2] + box1.extents[2] * FR[1, 0];
            t = Mathf.Abs(T[0] * R[2, 1] - T[2] * R[0, 1]);
            if (t > ra + rb) return false;

            //L = A1 x B2
            ra = box0.extents[0] * FR[2, 2] + box0.extents[2] * FR[0, 2];
            rb = box1.extents[0] * FR[1, 1] + box1.extents[1] * FR[1, 0];
            t = Mathf.Abs(T[0] * R[2, 2] - T[2] * R[0, 2]);
            if (t > ra + rb) return false;

            //L = A2 x B0
            ra = box0.extents[0] * FR[1, 0] + box0.extents[1] * FR[0, 0];
            rb = box1.extents[1] * FR[2, 2] + box1.extents[2] * FR[2, 1];
            t = Mathf.Abs(T[1] * R[0, 0] - T[0] * R[1, 0]);
            if (t > ra + rb) return false;

            //L = A2 x B1
            ra = box0.extents[0] * FR[1, 1] + box0.extents[1] * FR[0, 1];
            rb = box1.extents[0] * FR[2, 2] + box1.extents[2] * FR[2, 0];
            t = Mathf.Abs(T[1] * R[0, 1] - T[0] * R[1, 1]);
            if (t > ra + rb) return false;

            //L = A2 x B2
            ra = box0.extents[0] * FR[1, 2] + box0.extents[1] * FR[0, 2];
            rb = box1.extents[0] * FR[2, 1] + box1.extents[1] * FR[2, 0];
            t = Mathf.Abs(T[1] * R[0, 2] - T[0] * R[1, 2]);
            if (t > ra + rb) return false;

            return true;
        }

测试代码

public class BoxBoxTester : MonoBehaviour {
    public GameObject box;
    public GameObject box1;
    Box _box;
    Box _box1;
    // Use this for initialization
    void Start()
    {
        _box = new Box();
        _box1 = new Box();
    }

    // Update is called once per frame
    void Update()
    {
        _box.center = box.transform.position;
        _box.rotation = box.transform.rotation;
        _box.extents = 0.5f * box.transform.localScale;

        _box1.center = box1.transform.position;
        _box1.rotation = box1.transform.rotation;
        _box1.extents = 0.5f * box1.transform.localScale;

        if (NIntersectTests.IntersectBoxBox(_box, _box1))
        {
            box.GetComponent<MeshRenderer>().materials[0].SetColor("_Color", new Color(1, 0, 0));
        }
        else
        {
            box.GetComponent<MeshRenderer>().materials[0].SetColor("_Color", new Color(1, 1, 1));
        }
    }
}


运行结果




参考

Separating Axis Theorem for Oriented Bounding Boxes

  • 30
    点赞
  • 95
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
以下是一个简单的基于obb碰撞检测算法代码示例: ```cpp bool obbCollision(const OBB& obb1, const OBB& obb2) { // 计算两个obb的中心点距离 Vector3d t = obb2.center - obb1.center; // 对obb1的三个轴进行遍历 for (int i = 0; i < 3; i++) { // 计算obb1在当前轴上的投影长度 float r1 = obb1.axis[i].dot(obb1.half_size); // 计算obb2在当前轴上的投影长度 float r2 = obb2.axis[i].dot(obb2.half_size); // 两个obb在当前轴上的投影长度之和 float r = r1 + r2; // 中心点距离在当前轴上的投影长度 float t_len = abs(t.dot(obb1.axis[i])); // 如果中心点距离在当前轴上的投影长度大于两个obb在当前轴上的投影长度之和,则没有碰撞 if (t_len > r) { return false; } } // 对obb2的三个轴进行遍历 for (int i = 0; i < 3; i++) { // 计算obb1在当前轴上的投影长度 float r1 = obb1.axis[i].dot(obb1.half_size); // 计算obb2在当前轴上的投影长度 float r2 = obb2.axis[i].dot(obb2.half_size); // 两个obb在当前轴上的投影长度之和 float r = r1 + r2; // 中心点距离在当前轴上的投影长度 float t_len = abs(t.dot(obb2.axis[i])); // 如果中心点距离在当前轴上的投影长度大于两个obb在当前轴上的投影长度之和,则没有碰撞 if (t_len > r) { return false; } } // 对obb1和obb2的叉积轴进行遍历 for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { // 叉积轴 Vector3d axis = obb1.axis[i].cross(obb2.axis[j]); // 计算obb1和obb2在当前轴上的投影长度之和 float r = obb1.axis[i].dot(obb1.half_size) + obb2.axis[j].dot(obb2.half_size); // 中心点距离在当前轴上的投影长度 float t_len = abs(t.dot(axis)); // 如果中心点距离在当前轴上的投影长度大于两个obb在当前轴上的投影长度之和,则没有碰撞 if (t_len > r) { return false; } } } // 所有轴都没有发现碰撞,则认为两个obb发生了碰撞 return true; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值