# Ray-Sphere intersection

Page 1 of 1

## 2 Replies - 20944 Views - Last Post: 06 September 2009 - 03:01 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=124203&amp;s=4d1e6dac6a46fae53808e862b21c3e96&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Pontus

• Cattlebruiser

Reputation: 18
• Posts: 612
• Joined: 28-December 06

# Ray-Sphere intersection

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

Reputation: 37
• 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

### #3 Pontus

• Cattlebruiser

Reputation: 18
• 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