8,303
社区成员
发帖
与我相关
我的任务
分享
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;
}