两个AABB相交测试算法详解
判断两个AABB是否相交
基本思想为当两个AABB包围盒a和b的中心位置pos的距离小于两个的hafl_size的和的时候则相交。
bool AABBIntersectionTest(const Vector3& a_pos, const Vector3& b_pos,
const Vector3& a_half_size, const Vector3& b_half_size)
{Vector3 delta = b_pos - a_pos;Vector3 totalSize = a_half_size + b_half_size;if (abs(delta.x) < totalSize.x && abs(delta.y) < totalSize.y &&abs(delta.z) < totalSize.z) {return true;}return false;
}
求碰撞信息
在通过上文中的相交测试之后,可以通过如下方法获取渗入深度 penetration 。基本思想为分别计算出 b_max - a_min 和 a_max - b_min 的值。然后求出二者各三个轴的分量(即共六个分量)中的最小值即为渗入深度。如图所示,在非高速的情况下,如果发生碰撞即能检测到,则碰撞法线所在的轴的渗入深度是一定小于其他轴的。
碰撞法线即位于最大渗入深度所出现的轴。CollisionInfo 包含的信息为a的碰撞点的local坐标,b的碰撞点的local坐标,a的local碰撞法线 normal 和渗入深度 penetration 。
bool AABBIntersectionTest(const Vector3& a_pos, const Vector3& b_pos, const Vector3& a_half_size, const Vector3& b_half_size, CollisionInfo& collision_info) {if (AABBIntersectionTest(a_pos, b_pos, a_half_size, b_half_size)) {static const Vector3 faces[6] = //a的六个面的方向{Vector3(-1, 0, 0), Vector3(1, 0, 0),Vector3(0, -1, 0), Vector3(0, 1, 0),Vector3(0, 0, -1), Vector3(0, 0, 1),};Vector3 a_max = a_pos + a_half_size;Vector3 a_min = a_pos - a_half_size;Vector3 b_max = b_pos + b_half_size;Vector3 b_min = b_pos - b_half_size;float distances[6] ={(b_max.x - a_min.x),// distance of box ’b’ to ’left ’ of ’a ’.(a_max.x - b_min.x),// distance of box ’b’ to ’right ’ of ’a ’.(b_max.y - a_min.y),// distance of box ’b’ to ’bottom ’ of ’a ’.(a_max.y - b_min.y),// distance of box ’b’ to ’top ’ of ’a ’.(b_max.z - a_min.z),// distance of box ’b’ to ’far ’ of ’a ’.(a_max.z - b_min.z) // distance of box ’b’ to ’near ’ of ’a ’.};float penetration = FLT_MAX;Vector3 best_axis;for (int i = 0; i < 6; i++){if (distances[i] < penetration) { //求渗入深度为求最小值(最小值也必为正数)penetration = distances[i];best_axis = faces[i];}}collision_info.AddContactPoint({ 0, 0, 0 }, {0, 0, 0},bestAxis, penetration); //由于是AABB,此处将两个AABB的碰撞点简化的设为各自的中心return true;}return false;
}
两个AABB相交测试算法详解相关推荐
- html5 游戏 算法,JS/HTML5游戏常用算法之碰撞检测 包围盒检测算法详解【圆形情况】...
JS/HTML5游戏常用算法之碰撞检测 包围盒检测算法详解[圆形情况] 发布时间:2020-10-10 13:42:43 来源:脚本之家 阅读:95 作者:krapnik 本文实例讲述了JS/HTML ...
- YOLOv4算法详解
YOLOv4: Optimal Speed and Accuracy of Object Detection-论文链接-代码链接 目录 1.需求解读 2.YOLOv4算法简介 3.YOLOv4算法详解 ...
- 基础排序算法详解与优化
文章图片存储在GitHub,网速不佳的朋友,请看<基础排序算法详解与优化> 或者 来我的技术小站 godbmw.com 1. 谈谈基础排序 常见的基础排序有选择排序.冒泡排序和插入排序.众 ...
- 【目标检测】Faster RCNN算法详解
转载自:http://blog.csdn.net/shenxiaolu1984/article/details/51152614 Ren, Shaoqing, et al. "Faster ...
- Faster RCNN算法详解
Ren, Shaoqing, et al. "Faster R-CNN: Towards real-time object detection with region proposal ne ...
- c4.5算法 程序语言,决策树之C4.5算法详解-Go语言中文社区
决策树之C4.5算法详解 主要内容 C4.5算法简介 分裂属性的选择--信息增益率 连续型属性的离散化处理 剪枝--PEP(Pessimistic Error Pruning)剪枝法 缺失属性值的处理 ...
- YOLOv5算法详解
目录 1.需求解读 2.YOLOv5算法简介 3.YOLOv5算法详解 3.1 YOLOv5网络架构 3.2 YOLOv5实现细节详解 3.2.1 YOLOv5基础组件 3.2.2 输入端细节详解 3 ...
- CenterNet算法详解
Objects as Points-论文链接-代码链接 目录 1.需求解读 2.CenterNet算法简介 3.CenterNet算法详解 3.1 CenterNet网络结构 3.2 CenterNe ...
- SoftPool算法详解
Refining activation downsampling with SoftPool-论文链接-代码链接 目录 1.需求解读 2.SoftPool算法简介 3.SoftPool算法详解 3.1 ...
最新文章
- 自动驾驶软件工程之全局规划
- python字典成绩_python_字典
- linux如何运行安卓程序,如何在Android系统上运行C语言的程序
- Linux-2.6.25 TCPIP函数调用大致流程
- [POJ 1330] Nearest Common Ancestors (倍增法)
- xmlHttpRequest无刷新验证用户名
- lintcode 最大子数组III
- 95-140-122-源码-transform-算子reduce
- Java中下载jar包
- ArcGIS Desktop 10.1 的系统自带的 prj 文件的去哪了?
- 2021年,中国程序员前景一片灰暗,真的是这样吗?
- mac装虚拟机真的好吗?
- 基于射频技术的门禁管理系统
- 2021年茶艺师(中级)考试总结及茶艺师(中级)证考试
- Elasticsearch语法大全
- 你的代码会说话吗?(下)
- ff15测试软件翻译,最终幻想15数据详细分析 FF15详细的数值参数测试
- 苹果电脑查看python版本_Mac终端查看python版本号
- 最新双十一淘宝查历史最低价流量主小程序源码/亲测
- 自动化部署工具瓦力(walle)的安装