C++ 射线球相交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6533856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:19:43  来源:igfitidea点击:

Ray-Sphere intersection

c++opengl

提问by noob

Is there any Ray sphere interestion code in OpenGL. I really need to understand that program so I am looking for it but can't find it. This seems to be popular problem but I can only find the formula and basic principle not any example. http://wiki.cgsociety.org/index.php/Ray_Sphere_Intersection

OpenGL 中是否有任何射线球兴趣代码。我真的需要了解那个程序,所以我正在寻找它但找不到它。这似乎是一个流行的问题,但我只能找到公式和基本原理,没有任何例子。 http://wiki.cgsociety.org/index.php/Ray_Sphere_Intersection

I have no idea how to go about it. This is what I got: http://www.sendspace.com/file/8gb6fj

我不知道该怎么做。这就是我得到的:http: //www.sendspace.com/file/8gb6fj

Has anyone of you done it or know for some source where I can get the program ?

你们中有人做过或知道我可以从哪里获得该程序的来源吗?

回答by Stéphane

let:

让:

  • A(xA,yA,zA) and B(xB,yB,zB) be two different points on the line
  • C(xC,yC,zC) be the center of the sphere
  • r be the radius of the sphere
  • A(xA,yA,zA) 和 B(xB,yB,zB) 是线上的两个不同点
  • C(xC,yC,zC) 是球体的中心
  • r 是球体的半径

the cartesian equation of the sphere is:

球体的笛卡尔方程为:

  • (x-xC)2+(y-yC)2+(z-zC)2=r2
  • (x-xC)2+(y-yC)2+(z-zC)2=r2

let us write the parametric equation of the line (parameter d):

让我们写出直线的参数方程(参数 d):

  • x = xA + d*(xB-xA)
  • y = yA + d*(yB-yA)
  • z = zA + d*(zB-zA)
  • x = xA + d*(xB-xA)
  • y = yA + d*(yB-yA)
  • z = zA + d*(zB-zA)

replacing in the sphere equation yields:

替换球体方程产生:

  • (xA + d(xB-xA) - xC)2+(yA + d(yB-yA) - yC)2+(zA + d(zB-zA) - zC)2=r2
  • (xA + d(xB-xA) - xC)2+(yA + d(yB-yA) - yC)2+(zA + d(zB-zA) - zC)2=r2

This is a quadratic equation in d, where the discrimant is:

这是 d 中的二次方程,其中判别式为:

  • Delta=b2-4*a*c
  • Delta=b2-4*a*c

with:

和:

  • a = (xB-xA)2+(yB-yA)2+(zB-zA)2
  • b = 2*((xB-xA)(xA-xC)+(yB-yA)(yA-yC)+(zB-zA)(zA-zC))
  • c = (xA-xC)2+(yA-yC)2+(zA-zC)2-r2
  • a = (xB-xA)2+(yB-yA)2+(zB-zA)2
  • b = 2*((xB-xA)(xA-xC)+(yB-yA)(yA-yC)+(zB-zA)(zA-zC))
  • c = (xA-xC)2+(yA-yC)2+(zA-zC)2-r2

if Delta<0 then there is no intersection

如果 Delta<0 则没有交集

if Delta==0 then there is a single intersection point (the line touches the sphere) the unique solution is d=-b/2a (from there use the parametric equations to compute the coordinates of the intersection point)

如果 Delta==0 那么有一个交点(线接触球体)唯一的解决方案是 d=-b/2a (从那里使用参数方程来计算交点的坐标)

if Delta>0 then there are a two intersection points the solutions are d1=(-b-sqrt(Delta))/(2a) and d2=(-b+sqrt(Delta))/(2a) (from there use the parametric equations to compute the coordinates of the intersection points)

如果 Delta>0 那么有两个交点,解决方案是 d1=(-b-sqrt(Delta))/(2a) 和 d2=(-b+sqrt(Delta))/(2a) (从那里使用参数方程来计算交点的坐标)



So what you have to do is:

所以你要做的是:

  • compute a, b, c, and then Delta using the formulas above
  • depending on its value, compute d or (d1 and d2)
  • compute the coordinates of the intersection points if there are any
  • 使用上面的公式计算 a、b、c 和 Delta
  • 根据其值,计算 d 或 (d1 和 d2)
  • 如果有任何交点,则计算交点的坐标

回答by datenwolf

OpenGL is a pure drawing API, i.e. the whole thing is designed to provide drawing tools. There is no scene management, geometry tools, etc. Those must be provided by other means.

OpenGL 是一个纯粹的绘图 API,即整个事物旨在提供绘图工具。没有场景管理、几何工具等,必须通过其他方式提供。