# Getting the point of intersection from this fast ray box intersection algorithm

I have seen lots of such algorithms that return a Boolean value, but I also need to get back the intersection point and ray length.

I am looking into this example, but I'm not sure how I can extract the point of intersection from it.

I did a small experiment in JS https://jsfiddle.net/j7htk96n/ . I assumed tmin is the needed distance (or intersection point parts), but doesn't seem so.

function intersection(b, r) {
var t1 = (b.min[0] - r.origin[0])*r.dir_inv[0];
var t2 = (b.max[0] - r.origin[0])*r.dir_inv[0];

var tmin = Math.min(t1, t2);
var tmax = Math.max(t1, t2);

console.log(tmin);

for (var i = 1; i < 3; ++i) {
t1 = (b.min[i] - r.origin[i])*r.dir_inv[i];
t2 = (b.max[i] - r.origin[i])*r.dir_inv[i];

tmin = Math.max(tmin, Math.min(t1, t2));
tmax = Math.min(tmax, Math.max(t1, t2));
console.log(tmin);
}

return tmax > Math.max(tmin, 0.0);
}

var box = {
min: [0,0,0],
max: [5,5,5]
};

var ray = {
origin: [-2,-2,0],
dir_inv: [0.66667,0.33333,0.66667]
};

console.log(intersection(box, ray));

• We have lots of past Q&A about this topic. How have you tried modifying the one you linked to so that it serves your needs, and what specific problem did you encounter in that attempt that we can help you overcome? This looks like a trivial task to just advance the ray to time tmin when you've detected an intersection, so it's not clear what you need to ask about here. May 10, 2021 at 13:10
• Comments are not for extended discussion; this conversation has been moved to chat. May 10, 2021 at 16:52

Let's update your function to return the time of intersection if there is one, or a negative value otherwise. This will treat rays that start inside the box as non-intersecting, but you can output max(tmin, 0) instead of tmin if you want to count the ray origin inside the box as an honorary intersection.

function intersection(b, r) {
var t1 = (b.min[0] - r.origin[0])*r.dir_inv[0];
var t2 = (b.max[0] - r.origin[0])*r.dir_inv[0];

var tmin = Math.min(t1, t2);
var tmax = Math.max(t1, t2);

console.log(tmin);

for (var i = 1; i < 3; ++i) {
t1 = (b.min[i] - r.origin[i])*r.dir_inv[i];
t2 = (b.max[i] - r.origin[i])*r.dir_inv[i];

tmin = Math.max(tmin, Math.min(t1, t2));
tmax = Math.min(tmax, Math.max(t1, t2));
console.log(tmin);
}

return tmax > Math.max(tmin, 0.0) ? tmin : -1;
}


Then we can check for a hit and report the position like so:

var t = intersection(box, ray);

if (t >= 0) {
var point = ray.origin.slice();
for (var i = 0; i < 3; ++i) P
point[i] += t / ray.dir_inv[i];
}

// TODO: do something with the intersection point.
}


If you store the non-inverted direction as part of your ray structure, then you can replace that / ray.dir_inv[i] with * ray.dir[i] instead.

The distance along the ray where the hit occurred is tmin times the length of the direction vector. (So if you use a unit vector for your direction, then tmin is automatically the distance you need)

• I optimized the code a bit jsfiddle.net/thw2186y . I will skip 1/ part, left that by mistake. May 10, 2021 at 17:05