# Unsure how to decide ray intersection interval

I'm coding an intersection algorithm with AABB, as found here. However, I'm absolutely unsure what to pass in as t0 and t1. The paper states that it's a ray intersection interval, but the only material I've been able to find on this is that it's some kind of guess or bound as to the final values. Any suggestions as to how to define t0 and t1?

I actually used this implementation myself. As far as I remember t0 and t1 are like the "clipping planes". So t0 you would think of as the ray origin or eye/camera position. So you might use t0 = 0.0 or t0 = 0.0 + epsilon. The idea being, if an intersection occurs behind the ray origin, the intersection point will be t < t0 and therefore is not the intersection you are looking for.

Now, for t1 is it sort of a guess. This is the furthest distance that you want to allow an intersection to occur. Usually when building a ray tracer you specify a maximum distance (or maximum ray length), beyond which you ignore any intersections. So you might specify t1 to be this distance as well.

Hope that helps.

• Since my rays have a definite length, not infinite, then t1 being the length of the ray would guarantee all collisions, right? – DeadMG Apr 7 '12 at 21:25
• See @seanmiddleditch's answer: You should pass 1.0f to t1 to get all collisions on the line segment between ray.origin and ray.origin+ray.direction, or you should pass numeric_limits<float>::infinity() to get all collisions on your ray. – Eric Apr 8 '12 at 11:55
• @DeadMG When you say ray length, do you mean the length of the ray's direction vector? I'll assume that you are normalizing the ray direction vectors (as in all direction vectors have a length of 1.0). In that case, the "length" of the ray is not what you want for t1. You want it to be the maximum distance that you want to allow an intersection to be found. So the suggestion of using infinity is a good idea. – Captain Head Apr 11 '12 at 22:08

The explanation of the math goes something like this:

A line can be represented as two points, call them A and B. Any point on a line can be represented with a parametric equation L(t) = A + t(AB), where (AB) is the vector from A to B (that is, B - A). If you work through that, you see that L(0) = A and L(1) = B. For t=0, you get A + 0(AB) = A, and for t=1, you can A + 1(AB) = A + 1(B - A) = A + B - A = B.

Now let's think about what a line, ray, and line segment really are. A line is an infinite series of points that contains a reference point and for which all points can be formed by adding that reference point to a scalar multiple of a vector. Which is exactly what L(t) = A + t(AB) says.

A ray however is a a line in which the scalar multiple must be positive. So R(t) = A + t(AB) for all t >= 0. A ray is contained in a line, but a negative t value plugged into the equation would give you a point on the line that is "behind" the ray's reference point. That is, if your ray starts at (1,1) and goes in the direction (1,0), and you put in t=-1, you get (1,1) + -1(1,0) = (0,1), which is clearly to the left of the reference point while the ray is pointing to the right.

A line segment is similar, except that now the t value must be constrained to [0,1]. That is, LS(t) = A + t(AB) for all t in [0,1]. If you put a t value larger than 1 in, you'll get a point "past" the point B. So if your line segment goes from (1,0) to (5,0), and you plug in t=1.5, you get (1,0) + 1.5((5,0)-(1,0)) = (1,0) + 1.5(4,0) = (7,0), which is clearly farther to the right than the rightmost point of the line segment.

Remember, however, that for all three line-like objects, the only thing you need to represent them is a single point and a vector. In the case of a line, the vector can be pointing either "left" or "right" along the line, because t can literally be any scalar value. In the case of a ray, the vector must point in the direction the ray points, but it doesn't matter how long the vector is, because t can be any non-negative value, including infinity. In the case of a line-segment, the vector must be the vector from the first end-point to the second end-point, because t must be constrained to [0,1] and the reference point plus the vector must be equal to the second end-point.

The AABB intersection code you're looking at is working with a Ray object, but that object is really just a point and a vector. Since we know that a point and a vector can represent a line, a ray, or a line segment, we see that the code is actually valid for any line-like object; the use of the name Ray is just a tad misleading.

Since the only differences between a line, ray, or line segment is what range the t values must be constrained to in its parametric form, and since the AABB intersection test is calculating the t value for the parametric form, the code will work for all three objects. The t0 and t1 values are the limits of t value required. So for a line, t0 and t1 should be -infinity and +infinity (-FLT_MAX and +FLT_MAX are reasonable and reliable alternatives in C/C++ code). For a ray, the values should be 0 and +infinity. For a line segment, the values should be 0 and 1.

Note that in the case of the line segment, the t values assuming that the vector provided really will point from A to B. If the Ray object you're using always normalized its vector, then the line segment equation becomes LS(t) = A + len(AB)*t*v for all t in [0,len(AB)]. In other words, if the vector passed to the test is normalized, pass in 0 for t0 and the distance between the two end-points of the line segment for t1.

To summarize: for a ray, pass in t0=0 and t1=+infinity (or FLT_MAX).