碰撞检测之Box-Box检测

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

时间: 2024-07-31 22:17:12

碰撞检测之Box-Box检测的相关文章

碰撞检测之Ray-Box检测

Separating Axis Theorem(分离轴理论) 在学习Ray-Box检测之前,首先来学习一些这个分离轴理论! 先说二维情况,一句话 Two convex polygons do not intersect if and only if there exists a line such that the projections of the two polygons onto the line do not intersect. 咳咳,翻译一下 两个凸包多边形,当且仅当存在一条线,这

碰撞检测之Sphere-Box检测

检测思路 首先要做的是将Box转为AABB,然后判断圆心是否在Box内,用的就是之前的SAT 如果圆心在Box内,肯定相交, 如果不在圆心内,则有四种情况,与顶点相交,与楞相交,与面相交,这里的确定也是通过SAT来确定. 在二维中,如果圆心不box内,有两种情况 只要对比红色线段的长度和圆的半径就可以了. 代码 public static bool IntersectSphereBox(Sphere sphere, Box box) { Vector3 delta = sphere.center

[C++]const Box * p || Box const * p || Box * const p的区别

const与指针结合使用时,容易让人迷惑的是: 1. const到底是限定该指针不可再指向其它内存呢? 2. 还是禁止通过该指针修改其指向的内存块的内容?(PS:这里说的是禁止通过该指针修改内存块,所以不是不能修改,而是要通过其它方式去修改.) 下面来探究一下,先上基础代码. // Box.h author:[email protected] #ifndef _BOX_H #define _BOX_H class Box { public : Box(); int volume(); void

CSS3与页面布局学习总结——Box Model、边距折叠、内联与块标签、CSSReset

目录 一.盒子模型(Box Model) 1.1.宽度测试 1.2.溢出测试 1.3.box-sizing属性 1.4.利用CSS画图 二.边距折叠 2.1.概要 2.2.垂直方向外边距合并计算 三.内联与块级标签 3.1.行内标签与块标签区别 3.2.隐藏 3.3.行内块标签 3.4.菜单示例 四.重置浏览器默认样式 4.1.CSSReset 4.1.1.MT css reset 4.1.2.PC css reset 4.1.3.PPTV css reset 4.1.4 YUI css res

edge box

先介绍一下matlab与c混合编程 主要步骤: 使用c语言编写函数 利用mexFunction()函数创建C与matlab接口 从Matlab中编译函数 # include <mex.h>: 为了实现matlab与c混合编程的接口 //例如实现一个 add函数在接口 #include "mex.h" double add(double x, double y) { return x+y; } 这个代码是算法真正实现的地方. 然后创建接口.mex函数库中的mexFunctio

CSS3与页面布局学习总结(二)——Box Model、边距折叠、内联与块标签、CSSReset

一.盒子模型(Box Model) 盒子模型也有人称为框模型,HTML中的多数元素都会在浏览器中生成一个矩形的区域,每个区域包含四个组成部分,从外向内依次是:外边距(Margin).边框(Border).内边距(Padding)和内容(Content),其实盒子模型有两种,分别是 ie 盒子模型和标准 w3c 盒子模型,加上了doctype声明,让所有浏览器都会采用标准 w3c 盒子模型去解释你的盒子.当设置一个元素的样式如下: <!DOCTYPE html> <html> <

display:table、box和width百分比来均分盒子的比较

box box适合均分内部子盒子没有边框的父盒子,否则内部盒子的边框重叠问题不太好处理,另外如果自盒子间有margin值,中间的margin会是两侧的double,这时可通过nth-child(index)来进行个别调整. box-flex是对除开子盒子内容区.定宽子盒子之外的剩余部分进行划分,而不是把父盒子的整个宽度进行划分,因此就会造成所有子盒子都设置为box-flex:1,但因为子盒子内容多少不一而造成不能均分. box的特点 1.当父元素设置为display:box后,内部的自盒子无论是

CSS3(二)Box Model、边距折叠、内联与块标签、CSSReset

目录 一.盒子模型(Box Model) 1.1.宽度测试 1.2.溢出测试 1.3.box-sizing属性 1.4.利用CSS画图 二.边距折叠 2.1.概要 2.2.垂直方向外边距合并计算 三.内联与块级标签 3.1.行内标签与块标签区别 3.2.隐藏 3.3.行内块标签 3.4.菜单示例 四.重置浏览器默认样式 4.1.CSSReset 4.1.1.MT css reset 4.1.2.PC css reset 4.1.3.PPTV css reset 4.1.4 YUI css res

DirectX Box

通过该Demo,可以清楚了解DirectX流程.将完整程序贴出,自己加以注释,方面查看理解. 1 /* 2 2015.4 3 d3dUtil.h 4 5 d3d 常用工具代码 6 */ 7 8 #ifndef D3DUTIL_H 9 #define D3DUTIL_H 10 11 // Let VC++ know we are compiling for WinXP and up. This let's us use 12 // API functions specific to WinXP (

(转)Unity3d中的碰撞检测

很多时候,当我们的主角与其他GameObject发生碰撞时, 我们需要做一些特殊的事情,比如:子弹击中敌人,敌人就得执行一系列的动作.这时,我们就需要检测到碰撞现象,即碰撞检测.这一篇,我来具体谈谈自己所了解的碰撞检测,希望高手不佞赐教. 首先,我们得明确一点:即产生碰撞信息所需要的条件.事实上,在unity3d中,能检测碰撞发生的方式有两种,一种是利用碰撞器,另一种则是利用触发器.这两种方式的应用非常广泛.为了完整的了解这两种方式,我们必须理解以下概念:    (一)碰撞器是一群组件,它包含了