1. 步骤
- 计算球心到平面的距离 d ;
- 若距离 d 小于球半径 r 则相交。
2. 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
int classifySpherePlane( const Vector3 &planeNormal, float planeD, const Vector3 &sphereCenter, float sphereRadius ){ float d = planeNormal * sphereCenter - planeD; if(d >= sphereRadius){ return +1; } if(d <= -sphereRadius){ return -1; } return 0; }
|
3. 动态检测
设平面为静止的,球作所有的相对位移。
平面的定义 :p⋅n=d
球的定义 :半径 r 和初始球心位置 c
球的位移 :单位向量 d 指明方向, l 代表位移的距离。t 从 0 变化到 l ,用直线方程 c+td 计算球心的运动轨迹。

图 1 球向平面移动
显然,不管在平面上的哪一点上发生相交,在球上的相交点总是固定点,用 c−rn 来计算交点。

图 2 球和平面接触的点
在得到球上的相交点之后,使用 《04 - 射线和平面的相交性检测》 中简单的射线与平面相交性检测的方法,替换掉公式中的 p0 :
t=d⋅nd−p0⋅n=d⋅nd−(c−rn)⋅n=d⋅nd−c⋅n+r