Jump to content
  • Advertisement
Sign in to follow this  
GameGeazer

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!