0
\$\begingroup\$

I am having some trouble peforming an overlap test on a axis aligned bounding box.

Here is my code

public boolean overlapps(AABB other) {
    return false;
   }

}

I don't know how to implement this method.


Here is what I tried

public boolean overlapps(AABB other) {
    if(min.x <= other.max.x && max.x >= other.min.x) {
        if(min.y <= other.max.y && max.y >= other.min.y) {
            if(min.z <= other.max.z && max.z >= other.min.z) {
                return true;
            }
        }
    }
    return false;
}
\$\endgroup\$
0
0
\$\begingroup\$

The easiest, and in some cases most efficient, way to test for this is to try to find an axis on which the AABBs do not overlap, since this avoid dealing with all the possible partial overlaps:

return !(this.xmax < other.xmin || this.xmin > other.xmax || this.ymax < other.ymin || this.ymin > other.ymax || this.zmax < other.zmin || this.zmin > other.zmax)

You'll find that this approach also translates to other related algorithms such as the separating axis test (this pretty much is a special case of it).

The actual sweep and prune algorithm has many variations (arrays or linked lists, single- or multiple axis, persistent etc.) and can be a lot harder to implement depending on which variation you choose.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.