碰撞检测之OBB-OBB检测

碰撞检测之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 AxisTheorem)中可能的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)

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


好,可以上代码了

[csharp] view plain copy
  1. public static bool IntersectBoxBox(Box box0, Box box1)  
  2.       {  
  3.           Vector3 v = box1.center - box0.center;  
  4.   
  5.           //Compute A's basis  
  6.           Vector3 VAx = box0.rotation * new Vector3(1, 0, 0);  
  7.           Vector3 VAy = box0.rotation * new Vector3(0, 1, 0);  
  8.           Vector3 VAz = box0.rotation * new Vector3(0, 0, 1);  
  9.   
  10.           Vector3[] VA = new Vector3[3];  
  11.           VA[0] = VAx;  
  12.           VA[1] = VAy;  
  13.           VA[2] = VAz;  
  14.   
  15.           //Compute B's basis  
  16.           Vector3 VBx = box1.rotation * new Vector3(1, 0, 0);  
  17.           Vector3 VBy = box1.rotation * new Vector3(0, 1, 0);  
  18.           Vector3 VBz = box1.rotation * new Vector3(0, 0, 1);  
  19.   
  20.           Vector3[] VB = new Vector3[3];  
  21.           VB[0] = VBx;  
  22.           VB[1] = VBy;  
  23.           VB[2] = VBz;  
  24.   
  25.           Vector3 T = new Vector3(Vector3.Dot(v, VAx), Vector3.Dot(v, VAy), Vector3.Dot(v, VAz));  
  26.   
  27.           float[,] R = new float[3, 3];  
  28.           float[,] FR = new float[3, 3];  
  29.           float ra, rb, t;  
  30.   
  31.           for (int i = 0; i < 3; i++)  
  32.           {  
  33.               for (int k = 0; k < 3; k++)  
  34.               {  
  35.                   R[i, k] = Vector3.Dot(VA[i], VB[k]);  
  36.                   FR[i, k] = 1e-6f + Mathf.Abs(R[i, k]);  
  37.               }  
  38.           }  
  39.   
  40.           // A's basis vectors  
  41.           for (int i = 0; i < 3; i++)  
  42.           {  
  43.               ra = box0.extents[i];  
  44.               rb = box1.extents[0] * FR[i, 0] + box1.extents[1] * FR[i, 1] + box1.extents[2] * FR[i, 2];  
  45.               t = Mathf.Abs(T[i]);  
  46.               if (t > ra + rb) return false;  
  47.           }  
  48.   
  49.           // B's basis vectors  
  50.           for (int k = 0; k < 3; k++)  
  51.           {  
  52.               ra = box0.extents[0] * FR[0, k] + box0.extents[1] * FR[1, k] + box0.extents[2] * FR[2, k];  
  53.               rb = box1.extents[k];  
  54.               t = Mathf.Abs(T[0] * R[0, k] + T[1] * R[1, k] + T[2] * R[2, k]);  
  55.               if (t > ra + rb) return false;  
  56.           }  
  57.   
  58.           //9 cross products  
  59.   
  60.           //L = A0 x B0  
  61.           ra = box0.extents[1] * FR[2, 0] + box0.extents[2] * FR[1, 0];  
  62.           rb = box1.extents[1] * FR[0, 2] + box1.extents[2] * FR[0, 1];  
  63.           t = Mathf.Abs(T[2] * R[1, 0] - T[1] * R[2, 0]);  
  64.           if (t > ra + rb) return false;  
  65.   
  66.           //L = A0 x B1  
  67.           ra = box0.extents[1] * FR[2, 1] + box0.extents[2] * FR[1, 1];  
  68.           rb = box1.extents[0] * FR[0, 2] + box1.extents[2] * FR[0, 0];  
  69.           t = Mathf.Abs(T[2] * R[1, 1] - T[1] * R[2, 1]);  
  70.           if (t > ra + rb) return false;  
  71.   
  72.           //L = A0 x B2  
  73.           ra = box0.extents[1] * FR[2, 2] + box0.extents[2] * FR[1, 2];  
  74.           rb = box1.extents[0] * FR[0, 1] + box1.extents[1] * FR[0, 0];  
  75.           t = Mathf.Abs(T[2] * R[1, 2] - T[1] * R[2, 2]);  
  76.           if (t > ra + rb) return false;  
  77.   
  78.           //L = A1 x B0  
  79.           ra = box0.extents[0] * FR[2, 0] + box0.extents[2] * FR[0, 0];  
  80.           rb = box1.extents[1] * FR[1, 2] + box1.extents[2] * FR[1, 1];  
  81.           t = Mathf.Abs(T[0] * R[2, 0] - T[2] * R[0, 0]);  
  82.           if (t > ra + rb) return false;  
  83.   
  84.           //L = A1 x B1  
  85.           ra = box0.extents[0] * FR[2, 1] + box0.extents[2] * FR[0, 1];  
  86.           rb = box1.extents[0] * FR[1, 2] + box1.extents[2] * FR[1, 0];  
  87.           t = Mathf.Abs(T[0] * R[2, 1] - T[2] * R[0, 1]);  
  88.           if (t > ra + rb) return false;  
  89.   
  90.           //L = A1 x B2  
  91.           ra = box0.extents[0] * FR[2, 2] + box0.extents[2] * FR[0, 2];  
  92.           rb = box1.extents[0] * FR[1, 1] + box1.extents[1] * FR[1, 0];  
  93.           t = Mathf.Abs(T[0] * R[2, 2] - T[2] * R[0, 2]);  
  94.           if (t > ra + rb) return false;  
  95.   
  96.           //L = A2 x B0  
  97.           ra = box0.extents[0] * FR[1, 0] + box0.extents[1] * FR[0, 0];  
  98.           rb = box1.extents[1] * FR[2, 2] + box1.extents[2] * FR[2, 1];  
  99.           t = Mathf.Abs(T[1] * R[0, 0] - T[0] * R[1, 0]);  
  100.           if (t > ra + rb) return false;  
  101.   
  102.           //L = A2 x B1  
  103.           ra = box0.extents[0] * FR[1, 1] + box0.extents[1] * FR[0, 1];  
  104.           rb = box1.extents[0] * FR[2, 2] + box1.extents[2] * FR[2, 0];  
  105.           t = Mathf.Abs(T[1] * R[0, 1] - T[0] * R[1, 1]);  
  106.           if (t > ra + rb) return false;  
  107.   
  108.           //L = A2 x B2  
  109.           ra = box0.extents[0] * FR[1, 2] + box0.extents[1] * FR[0, 2];  
  110.           rb = box1.extents[0] * FR[2, 1] + box1.extents[1] * FR[2, 0];  
  111.           t = Mathf.Abs(T[1] * R[0, 2] - T[0] * R[1, 2]);  
  112.           if (t > ra + rb) return false;  
  113.   
  114.           return true;  
  115.       }  

测试代码
[csharp] view plain copy
  1. public class BoxBoxTester : MonoBehaviour {  
  2.     public GameObject box;  
  3.     public GameObject box1;  
  4.     Box _box;  
  5.     Box _box1;  
  6.     // Use this for initialization  
  7.     void Start()  
  8.     {  
  9.         _box = new Box();  
  10.         _box1 = new Box();  
  11.     }  
  12.   
  13.     // Update is called once per frame  
  14.     void Update()  
  15.     {  
  16.         _box.center = box.transform.position;  
  17.         _box.rotation = box.transform.rotation;  
  18.         _box.extents = 0.5f * box.transform.localScale;  
  19.   
  20.         _box1.center = box1.transform.position;  
  21.         _box1.rotation = box1.transform.rotation;  
  22.         _box1.extents = 0.5f * box1.transform.localScale;  
  23.   
  24.         if (NIntersectTests.IntersectBoxBox(_box, _box1))  
  25.         {  
  26.             box.GetComponent<MeshRenderer>().materials[0].SetColor("_Color"new Color(1, 0, 0));  
  27.         }  
  28.         else  
  29.         {  
  30.             box.GetComponent<MeshRenderer>().materials[0].SetColor("_Color"new Color(1, 1, 1));  
  31.         }  
  32.     }  
  33. }  


运行结果




参考

Separating Axis Theorem for Oriented Bounding Boxes
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值