• Create Account

## Ray Sphere intersection

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

1 reply to this topic

### #1GameGeezer  Members

1079
Like
0Likes
Like

Posted 20 November 2012 - 12:48 PM

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, 20 November 2012 - 12:50 PM.

### #2Bacterius  Members

13102
Like
1Likes
Like

Posted 20 November 2012 - 04:59 PM

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).

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.