所以,我需要做Ray-AABB交集这是我的代码,摘自一本书
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;
}
然而,它不适合我。上面的交集返回false(因为tminY> tmin但小于tmax),而它应该返回true(测试时没有AABB边界)
观察:
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
如有任何帮助,不胜感激
// a degenerated bounding box, actually a segment parallel
// with the yAxis, with a length of -5 and starting in (x,z)=(-5, 0)
// Because:
// xMin=xMax=-5 - so a zero x extent
// zMin=zMax=0 - so a zero x extent
bounding box: max(-5, 0, 0) and min(-5, -5, 0)
你确定那是你想要的吗?检查射线是否与线段相交?