so, I need to do Ray-AABB intersection Here's my code, taken from a book
bool intersection(point start, point dir){
float tmax, tmin, tmaxY, tminY, tmaxZ, tminZ;
float a;
point temp1, temp2;
//check x
a = 1 / dir[0];
if(a > 0){
tmax = (max[0] - start[0]) * a;
tmin = (min[0] - start[0]) * a;
}else{
tmax = (min[0] - start[0]) * a;
tmin = (max[0] - start[0]) * a;
}
if(tmin > tmax) return false;
//check y
a = 1 / dir[1];
if(a > 0){
tmaxY = (max[1] - start[1]) * a;
tminY = (min[1] - start[1]) * a;
}else{
tmaxY = (min[1] - start[1]) * a;
tminY = (max[1] - start[1]) * a;
}
if(tminY > tmin) tmin = tminY;
if(tmaxY < tmax) tmax = tmaxY;
if(tmin > tmax) return false;
//check z
a = 1 / dir[2];
if(a > 0){
tmaxZ = (max[2] - start[2]) * a;
tminZ = (min[2] - start[2]) * a;
}else{
tmaxZ = (min[2] - start[2]) * a;
tminZ = (max[2] - start[2]) * a;
}
if(tminZ > tmin) tmin = tminZ;
if(tmaxZ < tmax) tmax = tmaxZ;
if(tmin > tmax) return false;
return true;
}
however, it doesn't work for me. The intersection above returned false (coz tminY > tmin but smaller than tmax) when it is supposed to return true (tested without AABB bounding)
Observations:
start(0, 0, 5) and dir(-89.5, -99.5, -100)
bounding box: max(-5, 0, 0) and min(-5, -5, 0)
tmin = 0.055865921
tmax = 0.055865921
tminY = 0
tmaxY = 0.050251257, therefor tmax = 0.050251257
Because tmin > tmax -> return false
It is supposed to be true
Any help will be appreciated