//包围盒数据结构 using System.Collections; using System.Collections.Generic; using UnityEngine;
publicclassCollisionData : MonoBehaviour { public Vector3[] vertexts = new Vector3[8]; public Vector3[] axes = new Vector3[3]; public Vector3 center = Vector3.zero; } --------------------------------------------- ///<summary> /// SAT分离轴碰撞检测之OBB检测 ///<param name="data1"></param> ///<param name="data2"></param> ///</summary> privateboolCollisionOBB(CollisionData data1,CollisionData data2) { //求与两个OBB包围盒之间两两坐标轴垂直的法线轴 共9个 int len1 = data1.axes.Length; int len2 = data2.axes.Length; Vector3[] axes = new Vector3[len1 + len2 + len1 * len2]; int k = 0; int initJ = len2; for (int i = 0; i < len1; i++) { axes[k++] = data1.axes[i]; for (int j = 0; j < len2; j++) { if (initJ > 0) { initJ--; axes[k++] = data2.axes[j]; } axes[k++] = Vector3.Cross(data1.axes[i], data2.axes[j]); } }
for (int i = 0, len = axes.Length; i < len; i++) { if (NotInteractiveOBB(data1.vertexts, data2.vertexts, axes[i])) { //有一个不相交就退出 returnfalse; } } returntrue; }