检測思路

首先要做的是将Box转为AABB,然后推断圆心是否在Box内。用的就是之前的SAT

假设圆心在Box内,肯定相交,

假设不在圆心内。则有四种情况,与顶点相交,与楞相交,与面相交,这里的确定也是通过SAT来确定。

在二维中,假设圆心不box内。有两种情况

仅仅要对照红色线段的长度和圆的半径就能够了。

代码

 public static bool IntersectSphereBox(Sphere sphere, Box box)
{
Vector3 delta = sphere.center - box.center;
Matrix4x4 boxRotMatrix = Matrix4x4.TRS(Vector3.zero, box.rotation, Vector3.one);
Vector3 dRot = boxRotMatrix.inverse.MultiplyVector(delta); bool outside = false;
if (dRot.x < -box.extents.x)
{
outside = true;
dRot.x = -box.extents.x;
}
else if (dRot.x > box.extents.x)
{
outside = true;
dRot.x = box.extents.x;
} if (dRot.y < -box.extents.y)
{
outside = true;
dRot.y = -box.extents.y;
}
else if (dRot.y > box.extents.y)
{
outside = true;
dRot.y = box.extents.y;
} if (dRot.z < -box.extents.z)
{
outside = true;
dRot.z = -box.extents.z;
}
else if (dRot.z > box.extents.z)
{
outside = true;
dRot.z = box.extents.z;
} if (outside) //if clipping was done, sphere center is outside of box.
{
Vector3 clippedDelta = boxRotMatrix.MultiplyVector(dRot); //get clipped delta back in world coords.
Vector3 clippedVec = delta - clippedDelta; //what we clipped away. float lenSquared = clippedVec.sqrMagnitude;
float radius = sphere.radius;
if (lenSquared > radius * radius) // PT: objects are defined as closed, so we return 'true' in case of equality
return false; //disjoint
}
return true;
}

測试代码

public class SphereBoxTester : MonoBehaviour {
public GameObject sphere;
public GameObject box;
Box _box;
Sphere _sphere;
// Use this for initialization
void Start () {
_box = new Box(); _sphere = new Sphere();
} // 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; _sphere.center = sphere.transform.position;
_sphere.radius = 0.5f * sphere.transform.localScale.x; if (NIntersectTests.IntersectSphereBox(_sphere, _box))
{
sphere.GetComponent<MeshRenderer>().materials[0].SetColor("_Color", new Color(1, 0, 0));
}
else
{
sphere.GetComponent<MeshRenderer>().materials[0].SetColor("_Color", new Color(1, 1, 1));
}
}
}

执行结果

碰撞检測之Sphere-Box检測的更多相关文章

  1. (各个公司面试原题)在线做了一套CC++综合測试题,也来測一下你的水平吧(二)

    刚才把最后的10道题又看了下.也发上来吧. 以下给出试题.和我对题目的一些理解 前10道题地址 (各个公司面试原题)在线做了一套CC++综合測试题.也来測一下你的水平吧(一) 11.设已经有A,B,C ...

  2. (4.5.4)Android測试TestCase单元(Unit test)測试和instrumentationCase单元測试

    Android单元和instrumentation单元測试 Developing Android unit and instrumentation tests Android的单元測试是基于JUnit ...

  3. 【转】Visual Studio單元測試小應用-測執行時間

    [转]Visual Studio單元測試小應用-測執行時間 Visual Studio的單元測試會記錄每一個測試的執行時間,如果有幾個Method要測效能,以前我會用Stopwatch,最近我都改用單 ...

  4. Maven实现Web应用集成測试自己主动化 -- 測试自己主动化(WebTest Maven Plugin)

    近期在appfuse看到使用webtest-maven-plugin实现Web应用的集成測试,研究了下.感觉很不错.对于Web应用自己主动构建很有帮助,在性能測试之前能够保证Web应用的基本功能工作正 ...

  5. Unity3D入门(二):碰撞检測

    碰撞器由来 1.系统默认会给每一个对象(GameObject)加入一个碰撞组件(ColliderComponent),一些背景对象则能够取消该组件. 2.在unity3d中,能检測碰撞发生的方式有两种 ...

  6. Cocos2d-x教程(34)-三维物体OBB碰撞检測算法

    欢迎增加Cocos2d-x 交流群:193411763 个中心点.1个旋转矩阵和3个1/2边长(注:一个旋转矩阵包括了三个旋转轴,若是二维的OBB包围盒则是一个中心点,两个旋转轴,两个1/2边长). ...

  7. 3D空间中射线与三角形的交叉检測算法

    引言 射线Ray,在3D图形学中有非常多重要的应用.比方,pick操作就是使用射线Ray来实现的,还有诸如子弹射线的碰撞检測等等都能够使用射线Ray来完毕. 所以,在本次博客中,将会简单的像大家介绍下 ...

  8. 目标检測的图像特征提取之(一)HOG特征

    1.HOG特征: 方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检測的特征描写叙述子.它通过计算和统计图像局部区 ...

  9. 【OpenCV新手教程之十二】OpenCV边缘检測:Canny算子,Sobel算子,Laplace算子,Scharr滤波器合辑

    本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/25560901 作者:毛星云(浅墨) ...

随机推荐

  1. Yii2 advance swiftmailer 不能发送邮件

    我用的是Yii2高级模板,在配置好邮箱后,并编写测试,测试结果表明是发送成功的,但我的邮箱就是接受不了邮件. 经过排查发现,是由 common/config/main-local.php 文件的 'u ...

  2. Linux环境下挂载SD卡的教程

    1.插入SD卡 如果系统能够识别SD卡,则会打印一些信息: 2.查看系统给SD卡分配的设备名 命令如下: fdisk -l 命令 说明:通常是根据SD卡的存储容量来确定的. 比如下面的信息: 3.挂载 ...

  3. poj3710 Christmas Game

    题目描述 题解: 树上删边. 对于奇数长度的环,可以看做一条边. 对于偶数长度的环,可以看做什么都没有. 没有特别好的解释…… 代码: #include<cstdio> #include& ...

  4. float 和 clear

    float 特性1:可以为行内浮动元素设置宽高 <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  5. Mac下复制粘贴的快捷键是什么?随记

    刚从window换成Mac OS系统的用户对于一些常用的快捷键一定非常的不习惯,“mac复制粘贴快捷键是什么?”这一简单的问题相信很多刚刚从Windows平台转到Mac平台的用户会问到的问题,因为Ma ...

  6. [Noip2017][Day 1][T1]玩具谜题(toy.cpp)

    题目描述 小南有一套可爱的玩具小人, 它们各有不同的职业. 有一天, 这些玩具小人把小南的眼镜藏了起来. 小南发现玩具小人们围成了一个圈,它们有的面朝圈内,有的面朝圈外.如下图: 这时singer告诉 ...

  7. 使用 wsgiref 创建WSGI APP

    wsgify装饰器将一个普通函数转变成WSGI应用程序. class webob.dec.wsgify(func=None, RequestClass=None, args=(), kwargs=No ...

  8. php 数据库的增删改查

    <!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>&l ...

  9. VirtualBox - 虚拟机下主机与虚拟机、虚拟机与虚拟机之间通信配置

    看了一下网上别人写的文章:http://www.it165.net/os/html/201401/7063.html 文章里面使用的是Debian,我这里配置的虚拟机系统一个是Ubuntu 14.10 ...

  10. [codevs] 1699 开关灯

    题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...