碰撞检測之Sphere-Box检測
检測思路
首先要做的是将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检測的更多相关文章
- (各个公司面试原题)在线做了一套CC++综合測试题,也来測一下你的水平吧(二)
刚才把最后的10道题又看了下.也发上来吧. 以下给出试题.和我对题目的一些理解 前10道题地址 (各个公司面试原题)在线做了一套CC++综合測试题.也来測一下你的水平吧(一) 11.设已经有A,B,C ...
- (4.5.4)Android測试TestCase单元(Unit test)測试和instrumentationCase单元測试
Android单元和instrumentation单元測试 Developing Android unit and instrumentation tests Android的单元測试是基于JUnit ...
- 【转】Visual Studio單元測試小應用-測執行時間
[转]Visual Studio單元測試小應用-測執行時間 Visual Studio的單元測試會記錄每一個測試的執行時間,如果有幾個Method要測效能,以前我會用Stopwatch,最近我都改用單 ...
- Maven实现Web应用集成測试自己主动化 -- 測试自己主动化(WebTest Maven Plugin)
近期在appfuse看到使用webtest-maven-plugin实现Web应用的集成測试,研究了下.感觉很不错.对于Web应用自己主动构建很有帮助,在性能測试之前能够保证Web应用的基本功能工作正 ...
- Unity3D入门(二):碰撞检測
碰撞器由来 1.系统默认会给每一个对象(GameObject)加入一个碰撞组件(ColliderComponent),一些背景对象则能够取消该组件. 2.在unity3d中,能检測碰撞发生的方式有两种 ...
- Cocos2d-x教程(34)-三维物体OBB碰撞检測算法
欢迎增加Cocos2d-x 交流群:193411763 个中心点.1个旋转矩阵和3个1/2边长(注:一个旋转矩阵包括了三个旋转轴,若是二维的OBB包围盒则是一个中心点,两个旋转轴,两个1/2边长). ...
- 3D空间中射线与三角形的交叉检測算法
引言 射线Ray,在3D图形学中有非常多重要的应用.比方,pick操作就是使用射线Ray来实现的,还有诸如子弹射线的碰撞检測等等都能够使用射线Ray来完毕. 所以,在本次博客中,将会简单的像大家介绍下 ...
- 目标检測的图像特征提取之(一)HOG特征
1.HOG特征: 方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检測的特征描写叙述子.它通过计算和统计图像局部区 ...
- 【OpenCV新手教程之十二】OpenCV边缘检測:Canny算子,Sobel算子,Laplace算子,Scharr滤波器合辑
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/25560901 作者:毛星云(浅墨) ...
随机推荐
- Oracle————存储过程与函数
存储过程存储过程参数模式包括IN.OUT. IN OUT. IN(默认参数模式):表示当存储过程别调用时,实参值被传递给形参:形参起变量作用,只能读该参数,而不能修改该参数.IN模式参数可以是变量或表 ...
- java文件上传,自动判断文件类型
public enum FileType { /** * JEPG. */ JPEG("FFD8FF"), /** * PNG. */ PNG("89504E47&quo ...
- Java 垃圾回收机制 (分代垃圾回收ZGC)
什么是自动垃圾回收? 自动垃圾回收是一种在堆内存中找出哪些对象在被使用,还有哪些对象没被使用,并且将后者删掉的机制.所谓使用中的对象(已引用对象),指的是程序中有指针指向的对象:而未使用中的对象(未引 ...
- 洛谷 P1085 不高兴的津津
这道题就是经典的条件分支的题https://www.luogu.org/problemnew/show/P1085 code: #include <stdio.h> int main() ...
- MVC常见框架
Struts Struts是Apache软件基金下Jakarta项目的一部分.Struts框架的主要架构设计和开发者是Craig R.McClanahan.Struts 是Java Web MVC框架 ...
- 转载 vue的基础使用
转载https://www.cnblogs.com/majj/p/9957597.html#top vue的介绍 前端框架和库的区别 nodejs的简单使用 vue的起步 指令系统 组件的使用 过滤器 ...
- 杭电 2120 Ice_cream's world I (并查集求环数)
Description ice_cream's world is a rich country, it has many fertile lands. Today, the queen of ice_ ...
- 解决hibernate产生的id序列或者setXX不能同步到数据库到问题(this.hibernateTemplate.flush();hibernateTemplate.getSessionFactory().getCurrentSession().connection().commit())
通过WarehouseInventoryPreLog warehouseInventoryPreLog = new WarehouseInventoryPreLog();产生一个id序列 如果不flu ...
- 2. TypeScript笔记
1. 安装node.js之后 需要测试npm命令 2.命令正常安装TypeScript 3.安装Egret egret 命令
- jmeter 断言-各种分类讲解
jmeter中有个元件叫做断言(Assertion),它的作用和loadrunner中的检查点类似: 用于检查测试中得到的响应数据等是否符合预期,用以保证性能测试过程中的数据交互与预期一致. 使用断言 ...