Hi,
I’m trying to use ray tracing to detect whether a ray projected from some point intersects with objects. I’ve split this problem up into two parts, one is calculating the point on a ray and the other is detectig whether or not it inersects with some object. The part I’m stuck on is calculating the point on a ray. I’ve looked at a few resources on the web, namely
and come up with the following code:
// origins of ray
float x_origin = 10.0, y_origin = 10.0, z_origin = 10.0;
// end point of ray
float x_end = 1.0, y_end = 1.0, z_end = 1.0;
// coords of point on ray
float x_point = x_initial, y_point = y_initial, z_point = x_initial;
// distance along ray
float r = 0.0;
for (int i = 0; i < 10; i++)
{
// calculate the point on the ray
x_point = x_origin + (x_end * r);
y_point = y_origin + (y_end * r);
z_point = z_origin + (z_end * r);
r = r - 0.05;
}
When the calculations are performed using the coordinates above (for x,y,z_origin and x,y,z_end) everything works fine. I encounter problems when I start to mess with these coordinates (e.g. swap the x,y,z_origin values with the x,y,z_end values) as the point calculated is no longer the one on the ray. I therefore made the following changes to the way that I calculate the point. Instead of only having a single calculation for each of the x,y,z coords I instead split each into four checks (which take into account the orientation and direction of the line):
if ((x_initial > x_end) & (x_initial < 0.0))
x_point = x_initial + (x_end * r);
else if ((x_initial < x_end) & (x_initial < 0.0))
x_point = x_initial - (x_end * r);
else if ((x_initial > x_end) & (x_initial > 0.0))
x_point = x_initial - (x_end * r);
else if ((x_initial < x_end) & (x_initial > 0.0))
x_point = x_initial + (x_end * r);
// do the same for the y,z coordinate calculations
This seems to now work when I swap the x,y,z_origin values with the x,y,z_end values. However, if I enter random coordinates then it no longer seems to work. Am I taking the right approach here (i.e. dividing the calculation for each coordinate into an if-else block)? If not has anyone solved this problem before?
Thanks,
Ramsey