# Ray Sphere intersection

This topic is 2158 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Using this tutorial:http://wiki.cgsociety.org/index.php/Ray_Sphere_Intersection. I wrote a method to test ray,sphere intersections:
However I'm unsure as to what the direction vector will consist of. It's apparent that the direction vector isn't angles so I'm assuming it's the slope of the line in each direction?

 float RaySphere::intersects(Ray* ray){ Vector3f dist = this->position.x_sub(&(ray->startpos)); float a = ray->angle.dotProd(&(ray->angle)); float b = 2 * ray->angle.dotProd(&(ray->startpos)); float c = ray->startpos.dotProd(&(ray->startpos)) - this->radius_squared; float disk = b * b - 4 * a * c; //no roots if(disk <0) return -1; float distsqrt = sqrt(disk); float q; if(b < 0) q = (-b-distsqrt)/2.0; else q = (-b+distsqrt)/2.0; float t0 = q / a; float t1 = c / q; //if t0 > t1 then swap them. if(t0 > t1) { float temp = t0; t0 = t1; t1 = temp; } //the ray missed the sphere if(t1 < 0) return -1; //if t0<0 intersection is at t1 if(t0<0) return t1; //else its at t0 return t0; }  Edited by GameGeazer

##### Share on other sites
The (unit) direction vector describes the direction of the ray, in 3D space. So, yes, in a sense, it does represent the "slope" of the ray in each dimension (normalized so that the total ray length is 1, to simplify calculations).

1. 1
2. 2
Rutin
25
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 20
• 14
• ### Forum Statistics

• Total Topics
632950
• Total Posts
3009378
• ### Who's Online (See full list)

There are no registered users currently online

×