Sign in to follow this  

Ray Sphere intersection

Recommended Posts

Using this tutorial: 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;
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
return t1;
//else its at t0
return t0;
[/CODE] Edited by GameGeazer

Share this post

Link to post
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).

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this