当前位置:首页>编程日记>正文

Bullet 物理引擎 详细分析 Dbvt (4)

光线与AABB 相交检测:
这是一个非常经典的问题, <<real time collision detection>> 5.33章节有非常详尽的讨论。
下面是光线的方程

   t是可变参数, P是光线的起始点 RayFrom, d是光线的方向向量

下面这个是平面的方程

  

向量n是平面的法向量,所以如果光线与任何一个平面相交,应该有如下等式

 

对于AABB来说是六个平面, 每个面的法向量为(0,0,1)(0,1,0) ..... 总之三个坐标有两个是0,另外的一个是1或者-1
AABB同时是3个平面槽的交集, 光线与AABB最多有2个相交点,一个是前景点,一个后景点,分别与两个平面相交。
特例是完全与一个平面重合。可以被认为是和相邻的两个平面相交。
btRayAabb2.是bullet中用于检测的相关函数

view plaincopy to clipboardprint?
SIMD_FORCE_INLINE bool btRayAabb2(const btVector3& rayFrom,  // point P  
const btVector3& rayInvDirection,  // Direction Vector inverse  
const unsigned int raySign[3],  
const btVector3 bounds[2],    // min x, max x AABB volume  
btScalar& tmin,  
btScalar lambda_min,  
btScalar lambda_max)  
{  
btScalar tmax, tymin, tymax, tzmin, tzmax;  
// get the far plane intersect param t along x axis  
tmax = (bounds[1-raySign[0]].getX() - rayFrom.getX()) * rayInvDirection.getX();  
// get the near plane intersect param t along y axis  
tymin = (bounds[raySign[1]].getY() - rayFrom.getY()) * rayInvDirection.getY();  
// get the far plane intersect param t along y axis  
tymax = (bounds[1-raySign[1]].getY() - rayFrom.getY()) * rayInvDirection.getY();  
// if it is not intersect with any plane then exit  
if ( (tmin > tymax) || (tymin > tmax) )  
return false;  
if (tymin > tmin)  
tmin = tymin;  // update the tmin  
if (tymax < tmax)  
tmax = tymax; // update tmax  
// get the near plane intersect param t along Z axis  
tzmin = (bounds[raySign[2]].getZ() - rayFrom.getZ()) * rayInvDirection.getZ();  
// get the far plane intersect param t along Z axis  
tzmax = (bounds[1-raySign[2]].getZ() - rayFrom.getZ()) * rayInvDirection.getZ();  
// if it is not intersect with any plane then exit  
if ( (tmin > tzmax) || (tzmin > tmax) )  
return false;  
// caculate the interval  
if (tzmin > tmin)  
tmin = tzmin;   // if find nearer point update tmin  
if (tzmax < tmax)  
tmax = tzmax;   // if found the farer point update tmax  
return ( (tmin < lambda_max) && (tmax > lambda_min) );  

SIMD_FORCE_INLINE bool btRayAabb2(const btVector3& rayFrom,  // point P
const btVector3& rayInvDirection,  // Direction Vector inverse
const unsigned int raySign[3],
const btVector3 bounds[2],    // min x, max x AABB volume
btScalar& tmin,
btScalar lambda_min,
btScalar lambda_max)
{
btScalar tmax, tymin, tymax, tzmin, tzmax;
// get the far plane intersect param t along x axis
tmax = (bounds[1-raySign[0]].getX() - rayFrom.getX()) * rayInvDirection.getX();
// get the near plane intersect param t along y axis
tymin = (bounds[raySign[1]].getY() - rayFrom.getY()) * rayInvDirection.getY();
// get the far plane intersect param t along y axis
tymax = (bounds[1-raySign[1]].getY() - rayFrom.getY()) * rayInvDirection.getY();
// if it is not intersect with any plane then exit
if ( (tmin > tymax) || (tymin > tmax) )
return false;
if (tymin > tmin)
tmin = tymin;  // update the tmin
if (tymax < tmax)
tmax = tymax; // update tmax
// get the near plane intersect param t along Z axis
tzmin = (bounds[raySign[2]].getZ() - rayFrom.getZ()) * rayInvDirection.getZ();
// get the far plane intersect param t along Z axis
tzmax = (bounds[1-raySign[2]].getZ() - rayFrom.getZ()) * rayInvDirection.getZ();
// if it is not intersect with any plane then exit
if ( (tmin > tzmax) || (tzmin > tmax) )
return false;
// caculate the interval
if (tzmin > tmin)
tmin = tzmin;   // if find nearer point update tmin
if (tzmax < tmax)
tmax = tzmax;   // if found the farer point update tmax
return ( (tmin < lambda_max) && (tmax > lambda_min) );
}

现在可以讨论ayTestInternal 函数, 这个函数基本的算法就是遍历所有的节点(基于栈)
光线讲和树中的每个节点做相交测试,如果相交就继续处理对应的子结点,否则就跳过。
对应所有最终的相交叶子节点,调用相交处理逻辑(回调函数)来处理。

view plaincopy to clipboardprint?
do 
{  
//pop out the top of stack  
const btDbvtNode* node=stack[--depth];  
//set up the AABB BOX  
bounds[0] = node->volume.Mins()-aabbMax;  
bounds[1] = node->volume.Maxs()-aabbMin;  
btScalar tmin=1.f,lambda_min=0.f;  
unsigned int result1=false;  
// Do the intersect tes t!!  
result1 = btRayAabb2(rayFrom,rayDirectionInverse,  
signs,bounds,tmin,lambda_min,  
lambda_max);  
if(result1)  
{  
//if test pass  
if(node->isinternal())  
{ //if node is intertal  
if(depth>treshold) //dynamic expand stack  
{  
stack.resize(stack.size()*2);  
treshold=stack.size()-2;  
}  
//push the left child into stack  
stack[depth++]=node->childs[0];  
//push the right child into stack  
stack[depth++]=node->childs[1];  
}  
else 
{  
//if node is leaf node,then process it by callback  
policy.Process(node);  
}  
}  
} while(depth); 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/superwiles/archive/2010/03/16/5383896.aspx


http://www.coolblog.cn/news/498ee2f527847ac3.html

相关文章:

  • asp多表查询并显示_SpringBoot系列(五):SpringBoot整合Mybatis实现多表关联查询
  • s7day2学习记录
  • 【求锤得锤的故事】Redis锁从面试连环炮聊到神仙打架。
  • 矿Spring入门Demo
  • 拼音怎么写_老师:不会写的字用圈代替,看到孩子试卷,网友:人才
  • Linux 实时流量监测(iptraf中文图解)
  • Win10 + Python + GPU版MXNet + VS2015 + RTools + R配置
  • 美颜
  • shell访问php文件夹,Shell获取某目录下所有文件夹的名称
  • 如何优雅的实现 Spring Boot 接口参数加密解密?
  • LeCun亲授的深度学习入门课:从飞行器的发明到卷积神经网络
  • Mac原生Terminal快速登录ssh
  • java受保护的数据与_Javascript类定义语法,私有成员、受保护成员、静态成员等介绍...
  • mysql commit 机制_1024MySQL事物提交机制
  • 支撑微博千亿调用的轻量级RPC框架:Motan
  • jquery 使用小技巧
  • 2019-9
  • 法拉利虚拟学院2010 服务器,法拉利虚拟学院2010
  • vscode pylint 错误_将实际未错误的py库添加到pylint白名单
  • 科学计算工具NumPy(3):ndarray的元素处理
  • 工程师在工作电脑存 64G 不雅文件,被公司开除后索赔 41 万,结果…
  • linux批量创建用户和密码
  • newinsets用法java_Java XYPlot.setInsets方法代碼示例
  • js常用阻止冒泡事件
  • 气泡图在开源监控工具中的应用效果
  • 各类型土地利用图例_划重点!国土空间总体规划——土地利用
  • php 启动服务器监听
  • dubbo简单示例
  • 【设计模式】 模式PK:策略模式VS状态模式
  • [iptables]Redhat 7.2下使用iptables实现NAT
  • Ubuntu13.10:[3]如何开启SSH SERVER服务
  • CSS小技巧——CSS滚动条美化
  • JS实现-页面数据无限加载
  • 阿里巴巴分布式服务框架 Dubbo
  • 最新DOS大全
  • Django View(视图系统)
  • 阿里大鱼.net core 发送短信
  • 程序员入错行怎么办?
  • 两张超级大表join优化
  • 第九天函数
  • HDU 5988 最小费用流
  • Linux软件安装-----apache安装
  • 《看透springmvc源码分析与实践》读书笔记一
  • Sorenson Capital:值得投资的 5 种 AI 技术
  • 正式开课!如何学习相机模型与标定?(单目+双目+鱼眼+深度相机)
  • Arm芯片的新革命在缓缓上演
  • nagios自写插件—check_file
  • 通过Spark进行ALS离线和Stream实时推荐
  • python3 错误 Max retries exceeded with url 解决方法
  • 行为模式之Template Method模式