2 Replies - 9833 Views - Last Post: 06 September 2009 - 03:01 AM Rate Topic: -----

#1 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 17
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Ray-Sphere intersection

Post icon  Posted 06 September 2009 - 01:12 AM

Hi there

I've been searching on the internetn for a while now, and I can't seem to find a function that determines wether a given sphere and ray intersect, and if so, what's the 'normal' at that point.

All help is welcome!
Is This A Good Question/Topic? 0
  • +

Replies To: Ray-Sphere intersection

#2 Lillefix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 204
  • Joined: 19-September 08

Re: Ray-Sphere intersection

Posted 06 September 2009 - 02:49 AM

This snippet from http://www.peroxide.dk looks like what you want.
double intersectRaySphere(D3DVECTOR rO, D3DVECTOR rV, D3DVECTOR sO, double sR) {
	
   D3DVECTOR Q = sO-rO;
   double c = lengthOfVector(Q);
   double v = dot(Q,rV);
   double d = sR*sR - (c*c - v*v);

   // If there was no intersection, return -1
   if (d < 0.0) return (-1.0f);

   // Return the distance to the [first] intersecting point
   return (v - sqrt(d));
}

This post has been edited by Lillefix: 06 September 2009 - 02:55 AM

Was This Post Helpful? 0
  • +
  • -

#3 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 17
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Ray-Sphere intersection

Posted 06 September 2009 - 03:01 AM

Thanks!

This post has been edited by Pontus: 06 September 2009 - 06:06 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1