-2

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

RLin
  • 25
  • 3
  • 1
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Oct 23 '16 at 03:27
  • Once you do your debugging homework, just for the fun of it: verify how the snippet of code above reacts to intersection along the Ox/Oy/Oz axes (hint: float division by zero). – Adrian Colomitchi Oct 23 '16 at 03:45
  • @πάντα ῥεῖ I've debugged it. It really gives tminY that's greater than tmax, which is why i asked on Stack Overflow. If you found the err in my code please tell me – RLin Oct 23 '16 at 06:23
  • @Adrian Colomitchi not a single of my dirs is zero – RLin Oct 23 '16 at 06:25

1 Answers1

1
// 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)

Are you sure that's what you want? Check if a ray intersects a segment?

Adrian Colomitchi
  • 3,974
  • 1
  • 14
  • 23
  • Not really, But in this case, I only have 4 faces and all of them is on the same Z coordinates. I've also checked the above code with the teapot.obj, but also didn't work on some parts. – RLin Oct 23 '16 at 07:52
  • ^ (forgot to mention your name) – RLin Oct 23 '16 at 07:57