-1

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

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

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

noob
  • 83
  • 1
  • 1
  • 4

2 Answers2

39

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

the cartesian equation of the sphere is:

  • (x-xC)²+(y-yC)²+(z-zC)²=r²

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

  • 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)²+(yA + d(yB-yA) - yC)²+(zA + d(zB-zA) - zC)²=r²

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

  • Delta=b²-4*a*c

with:

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

if Delta<0 then there is no intersection

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)

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)


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
Stéphane
  • 6,920
  • 2
  • 43
  • 53
  • What does "the ray of the sphere" mean exactly? – 3Dave Jun 30 '11 at 13:00
  • sorry I meant radius... edited! – Stéphane Jun 30 '11 at 14:00
  • Thanks for replies I will see what I can do. BTW I found this on internet but do not know how to use as it is java. I got netbeans does anyone know how I can run this program. It seems this is also some ray-sphere intersection program: http://www.pixelnerve.com/v/2009/02/08/ray-sphere-intersection/ – noob Jul 01 '11 at 19:09
  • damn, no newline in comment or code allowed: since you only want to know weather ray hits, this can be simpyfied even more. this is my hlsl-code float intersectRaySphere(float4 rayOrigin, float4 rayDirection, float4 sphere) { float4 sphereCenter = sphere; sphereCenter.w = 1.0f; float4 dist = rayOrigin - sphereCenter; float B_half = dot(dist, rayDirection); float C = dot(dist, dist) - sphere.w; return B_half * B_half - C; } the w component of vector4s are 1 on position coordinate, 0 on direction normal and squared radius of the sphere. result above zero means hit :D – thewhiteambit Oct 19 '14 at 22:33
  • It shows weird functionality if there are minus coordinates in A or B. Any suggestions? – Bence Pattogato Apr 23 '15 at 14:59
3

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.

datenwolf
  • 159,371
  • 13
  • 185
  • 298