Archived

This topic is now archived and is closed to further replies.

executor_2k2

Sphere intersection

Recommended Posts


    
double intersectSphere(Point rayOrigin, Vector rayVector, Point sphereOrigin, double sphereRadius)
{
   Vector Q = sphereOrigin - rayOrigin;
   double c = length of Q;
   double v = Q * rayVector;
   double d = sphereRadius*sphereRaidus - (c*c – v*v);

   // If there was no intersection, return -1


   if (d < 0.0) return -1.0;

   // Return the distance to the [first] intersecting point


   return v - sqrt(d);
} 
  
This is a fucntion im studying for collision detection.I am having trouble figuring out what is going on in the calculatioon of v and d. Could someone step me through it? What major concept is working behind the scenes? Thanks. [edited by - executor_2k2 on July 17, 2002 6:47:30 PM]

Share this post


Link to post
Share on other sites
Yes, '*' would be an overloaded inner (dot) product operation, since in the event of scalars it just gives the multiplication.

Q is the vector defining the displacement of the sphere from the ray origin.
v would therefore be the scalar projection of the ray onto Q and the length of v will therefore be less than or equal to c. Basically the scalar projection is the length of the component of the ray vector in the direction of Q.

The inner product of two vectors is defined as

A.B = a1b1 + a2b2 + ... + anbn, for an n-dimensional vector basis.


c2-v2 is therefore the distance from the end of the ray to the nearest point on Q. If this distance is greater than the sphere's radius then the end of the ray must not lie inside the sphere.

I hope this clears things up for you!

Cheers,

Timkin

[edited by - Timkin on July 18, 2002 3:06:03 AM]

Share this post


Link to post
Share on other sites
Ah, well now that I know the '*' was overloaded (you just never know with these Vector classes), it all makes sense now. I call it the dot product, but whatever floats your boat :D

EDIT: Well, actually I knew the '*' has to overloaded, but I didn't know to what. Ok I lied again, I knew it probably meant the dot product but I was too busy to read the code so I stalled

[edited by - Zipster on July 18, 2002 3:32:54 AM]

Share this post


Link to post
Share on other sites
This is Paul Nettle''s code from his ellipsoids collision detection tutorial, right? When I first saw this a while back, I was fairly baffled. Timkin''s explanation will surely help you, and you might also try sketching it on paper, which is what I did (i.e., draw a circle, draw a ray, etc.). Eventually you''ll see a right triangle; the hypotenuse goes from the sphere center to the first collision point and its length is sphereRadius; the squared length of one of the legs is (c*c - v*v). The expression for d is using the Pythagorean Theorem to calculate the squared length of the other leg.

Share this post


Link to post
Share on other sites