碰撞检测之OBB-OBB检测

2D情况

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

|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 |

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

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

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

三维情况

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 • (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