Sphere intersection

Started by
4 comments, last by executor_2k2 21 years, 9 months ago

    
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]
Well, that was a waste of 2 minutes of my life. Now I have to code faster to get 'em back...
Advertisement

  double v = Q * rayVector;  

Q is a vector, and rayVector is a vector. How do you get a scalar from multiplying two vectors? Is ''*'' overloaded?
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]
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]
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.
Does this work with ellipsoids if for radius you use the ellipsoids radius coincident with the plane''s normal?
Well, that was a waste of 2 minutes of my life. Now I have to code faster to get 'em back...

This topic is closed to new replies.

Advertisement