zoukankan      html  css  js  c++  java
  • 射线与球的相交性检测

     
    图形码农 2016-07-13 13:37:33 
     3718 
     收藏 1
    分类专栏: 碰撞检测
    从图形来说
    射线和圆相交, origin是射线起点, dir是射线的方向向量。p0,p1是两个交点,center为圆心,半径为R,d为圆心到射线的距离。
    我们先以2D切面图来说明,当射线和圆相交的时候,可以看到,球心 center 到射线 ray 的距离 d <= R,这个即为相交的条件。那么射线与球相切就转化为了球心到射线的距离d的判断。先求出d:
    1. 设圆心在射线上的投影为c',则 origin,center, c' 形成了一个直角三角形。
    2. 获得射线起点到圆心的向量 Voc = Vcenter - Vorigin
    3. 在射线方向上的投影为: Poc = Voc·dir
    4. 勾股定理:d·d = Voc·Voc - Poc·Poc
    可以求出d的数值,
    • d < R,射线穿过圆,与圆有两个交点。
    • d = R,射线与圆相切,有一个交点为切点。
    • d > R,射线在圆外,没有交点。
    接下来求P0,P1:
    1. c',center,P0 or P1点构成直角三角形。
    2. P0 or P1到c'的距离 tca·tca = R·R - d·d;
    3. 有如下式子
      1. P0 = dir·( |Poc| - tca );
      2. P1 = dir·( |Poc| + tca );
    要注意,没有交点的时候, tca·tca < 0 是没办法开平方的
    推导三维情况可以照上面的去做,dot能保证投影点在同一个平面上的。
    附代码
    1. bool Intersect(const Ray& ray, const Sphere& sphere, float& t0, float& t1)
    2. {
    3. Vector3 oc = sphere.GetCenter() - ray.GetOrigin();
    4. float projoc = dot(ray.GetDirection(), oc);
    5. if (projoc < 0)
    6. return false;
    7. float oc2 = dot(oc, oc);
    8. float distance2 = oc2 - projoc * projoc; //计算出的球心到射线的距离
    9. if (distance2 > sphere.GetRadiusSquare())
    10. return false;
    11. float discriminant = sphere.GetRadiusSquare() - distance2; //使用勾股定理,计算出另一条边的长度
    12. if(discriminant < FLOAT_EPSILON) //表明只有一个交点,射线与球相切
    13. t0 = t1 = projoc;
    14. else
    15. {
    16. discriminant = sqrt(discriminant);
    17. t0 = projoc - discriminant;
    18. t1 = projoc + discriminant;
    19. if (t0 < 0)
    20. t0 = t1;
    21. }
    22. return true;
    23. }
     
    从方程角度来看
    射线方程:ray : P(t) = O + D·t ( t >= 0 )
    球的方程:sphere : sqr( P-C ) = R·R (sqr(x) = x^2 = x·x)
    O=origin, D=direction, C=center, R=radius
    射线方程表明的是如下一个点的集合P,当t从零增大时, D·t会沿着D向量的方向从零逐步变长,t 取值无限表示了射线单方向。从O点开始在D方向上无限个点构成了一条射线。
    球的方程表明了任何点P,只要到C点的距离等于半径R,则表明点在球面上,这么一个球面上的点的集合。
    因此当射线与球相交的时候,这个点既在射线上,又在球面上。等式射线的P(t) = 球的P成立。
    联立两个方程,试着求解 t 有:
    sqr( O + D·t - C ) = R·R
    设 O-C=OC,有:
    1. sqr( OC+D·t ) - R·R = 0
    2. //展开得到如下式子
    3. => D·D·t·t + 2·OC·D·t + OC·OC - R·R = 0
    4. => (D·D)·t·t + 2·(OC·D)·t + OC·OC - R·R = 0
    因为 D 是单位向量有D·D = dot(D, D) = 1最后方程为:
    t·t + 2·(OC·D)·t + OC·OC - R·R = 0;
    这是一个关于 t 的二次方程at^2 + bt + c = 0那么解就已经出来了:
    • t0 = -(b + √Δ) / 2a
    • t1 = -(b - √Δ) / 2a
    • a = D·D = dot(D, D) = 1;
    • b = 2·OC·D = 2·dot(OC, D);
    • c = OC·OC - R·R = dot(OC, OC) - R·R;
    • 判别式 Δ = sqr(b) - 4ac
    = 4·sqr( OC·D ) - 4·( OC·OC - R·R )
    = 4·( sqr( OC·D ) - OC·OC + R·R );
    如果判别式 Δ > 0,则表明球与射线相交。
    根据以上方程,我们其中试着展开 t 的式子
    t0 = -(b + √Δ) / 2a = -(b + √Δ) / 2·1
    = -b/2 - √(Δ/4)
    = -dot(OC, D) - √( sqr( dot(OC, D) ) - dot(OC, OC) + R·R )
    求出 t 后可以根据P(t) = O + D * t 得到交点。
    附chai3d中的计算代码
    1. inline int cIntersectionSegmentSphere(const cVector3d& a_segmentPointA,
    2. const cVector3d& a_segmentPointB,
    3. const cVector3d& a_spherePos,
    4. const double& a_sphereRadius,
    5. cVector3d& a_collisionPoint0,
    6. cVector3d& a_collisionNormal0,
    7. cVector3d& a_collisionPoint1,
    8. cVector3d& a_collisionNormal1)
    9. {
    10. // temp variables
    11. cVector3d AB, CA;
    12. a_segmentPointB.subr(a_segmentPointA, AB);
    13. a_segmentPointA.subr(a_spherePos, CA);
    14. double radiusSq = a_sphereRadius * a_sphereRadius;
    15. double a = AB.lengthsq();
    16. double b = 2.0 * cDot(AB, CA);
    17. double c = CA.lengthsq() - radiusSq;
    18. // invalid segment
    19. if (a == 0)
    20. {
    21. return (0);
    22. }
    23. double d = b*b - 4*a*c;
    24. // segment ray is located outside of sphere
    25. if (d < 0)
    26. {
    27. return (0);
    28. }
    29. // segment ray intersects sphere
    30. d = sqrt(d);
    31. double e = 2.0 * a;
    32. // compute both solutions
    33. double u0 = (-b + d) / e;
    34. double u1 = (-b - d) / e;
    35. // check if the solutions are located along the segment AB
    36. bool valid_u0 = cContains(u0, 0.0, 1.0);
    37. bool valid_u1 = cContains(u1, 0.0, 1.0);
    38. // two intersection points are located along segment AB
    39. if (valid_u0 && valid_u1)
    40. {
    41. if (u0 > u1) { cSwap(u0, u1); }
    42. // compute point 0
    43. AB.mulr(u0, a_collisionPoint0);
    44. a_collisionPoint0.add(a_segmentPointA);
    45. a_collisionPoint0.subr(a_spherePos, a_collisionNormal0);
    46. a_collisionNormal0.normalize();
    47. // compute point 1
    48. AB.mulr(u1, a_collisionPoint1);
    49. a_collisionPoint1.add(a_segmentPointA);
    50. a_collisionPoint1.subr(a_spherePos, a_collisionNormal1);
    51. a_collisionNormal1.normalize();
    52. return (2);
    53. }
    54. // one intersection point is located along segment AB
    55. else if (valid_u0)
    56. {
    57. // compute point 0
    58. AB.mulr(u0, a_collisionPoint0);
    59. a_collisionPoint0.add(a_segmentPointA);
    60. a_collisionPoint0.subr(a_spherePos, a_collisionNormal0);
    61. a_collisionNormal0.normalize();
    62. // check dot product to see if the intial segment point is located
    63. // inside the sphere.
    64. double dotProduct = cDot(AB, a_collisionNormal0); //如果射线在球内部与球发生相交,就不能算作相交,保证射线运动的方向性,射线从球体出来时的交点不算作相交点
    65. if (dotProduct < 0.0)
    66. {
    67. return (1);
    68. }
    69. else
    70. {
    71. return (0);
    72. }
    73. }
    74. // one intersection point is located along segment AB
    75. else if (valid_u1)
    76. {
    77. // compute point 0
    78. AB.mulr(u1, a_collisionPoint0);
    79. a_collisionPoint0.add(a_segmentPointA);
    80. a_collisionPoint0.subr(a_spherePos, a_collisionNormal0);
    81. a_collisionNormal0.normalize();
    82. // check dot product to see if the intial segment point is located
    83. // inside the sphere.
    84. double dotProduct = cDot(AB, a_collisionNormal0);
    85. if (dotProduct < 0.0)
    86. {
    87. return (1);
    88. }
    89. else
    90. {
    91. return (0);
    92. }
    93. }
    94. // both points are located outside of the segment AB
    95. else
    96. {
    97. return (0);
    98. }
    99. }</span>
    文/goteet(简书作者)
    原文链接:http://www.jianshu.com/p/1b008ed86627
    著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

    三维空间
  • 相关阅读:
    MyBatis-Generator 最佳实践
    Http请求工具
    多线程编程-之并发编程:阻塞队列
    如数据不存在就插入,存在就更新
    MySQL触发器
    13.multi_match实现dis_max+tie_breaker
    12. tie_breaker的使用原因和使用方法
    11.best fields策略(dis_max参数设置)
    10.多shard场景下relevence score可能不准确
    9.boost权重控制
  • 原文地址:https://www.cnblogs.com/shaochangming/p/13920555.html
Copyright © 2011-2022 走看看