求教射线和边界球相交的算法

liuliu20036 2011-05-08 05:32:42
请问大家射线和边界球相交判断的原理具体是什么呢?
下面的代码看了半天没有看懂,高手能方便解释下吗?谢谢!


bool CBoundingSphere::Intersect(CRay &ray, float fLength, float *pDist)
{
CVector3 vecRayToSphereDir; // 射线相对于边界球圆心的方向
float fRayToSphereLength = 0.0f; // 射线起点到边界球圆心的距离
float fIntersectLength = 0.0f; // 相交距离
float fSquaredLength = 0.0f;

// 得到射线相对于边界球圆心的方向
vecRayToSphereDir = m_vecCenter - ray.m_vOrigin;

// 计算射线起点到边界球圆心的距离
fRayToSphereLength = vecRayToSphereDir.DotProduct3(vecRayToSphereDir);

// 计算相交距离
fIntersectLength = vecRayToSphereDir.DotProduct3(ray.m_vDirection);
if (fIntersectLength < 0)
{
return false;
}

// Get the squared sphere intersect distance.
fSquaredLength = (m_fRadius * m_fRadius) - fRayToSphereLength
+ (fIntersectLength * fIntersectLength);
if (fSquaredLength < 0)
{
return false;
}

float fDist = fIntersectLength - (float)sqrt(fSquaredLength);
// 如果求出的距离值大于射线的距离,则返回false.
if (fDist > fLength)
{
return false;
}

// 计算距离
if (pDist)
{
*pDist = fDist;
}
return true;
}
...全文
169 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
woaitmac1314 2011-05-08
  • 打赏
  • 举报
回复
3d数学。。表示一样蛋疼,准备买本书叫:3D数学基础:图形与游戏开发。。。推荐给lz
myj870517 2011-05-08
  • 打赏
  • 举报
回复
CBoundingSphere这个是什么类,没见过,找到篇文章,那几张图应该有点帮助。
http://www.cppblog.com/lovedday/archive/2008/04/04/46264.html

8,303

社区成员

发帖
与我相关
我的任务
社区描述
游戏开发相关内容讨论专区
社区管理员
  • 游戏开发
  • 呆呆敲代码的小Y
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧