Search code examples
javaandroidcollision-detectionlibgdxgame-physics

Collision detection between two BoundingBoxes in libgdx


In my libgdx game I have 3D BoundingBoxes for map and player objects. I want to find out whether they collide in 3D space. How can I do this?


Solution

  • You can use the following method:

        public static boolean intersectsWith(BoundingBox boundingBox1, BoundingBox boundingBox2) {
            Vector3 otherMin = boundingBox1.getMin();
            Vector3 otherMax = boundingBox1.getMax();
            Vector3 min = boundingBox2.getMin();
            Vector3 max = boundingBox2.getMax();
    
            return (min.x < otherMax.x) && (max.x > otherMin.x)
                && (min.y < otherMax.y) && (max.y > otherMin.y)
                && (min.z < otherMax.z) && (max.z > otherMin.z);
        }
    

    It's modeled after this method: https://github.com/MasDennis/Rajawali/blob/master/src/rajawali/bounds/BoundingBox.java#L186