Search code examples
pythoncollision-detectionbounding-boxfrustum

Checking for bounding box intersections/collisions with a view frustum


I want to check a view frustum against the axis aligned bounding boxes of some objects, to check roughly whether those objects are in the field of view or not. Speed is not a big deal.


Solution

  • I figured out that building a world-space model of the view frustum and checking for bbox collisions with it was the wrong way to go about this.

    A much simpler method is to go the opposite way and convert each vertex of a given bbox to screen-space, and if any vertex of the bbox is within the screen bounds then count that bbox as visible. I get the screen-space position by multiplying against the camera matrix and then accounting for perspective based on the field of view of the camera.

    Here's the code:

    vertexMatrix = matrix([vertex.x,vertex.y,vertex.z,1]) 
    productMatrix = (vertexMatrix * camMatrix)
    pVectSS = vector(prodMatrix[0][0],prodMatrix[0][1],prodMatrix[0][2])
    
    pointX = ((pVectSS.x/(-pVectSS.z))/tan(radians(hFOV/2)))/2.0+.5
    pointY = ((pVectSS.y/(-pVectSS.z))/tan(radians(vFOV/2)))/2.0+.5
    

    key:

     camMatrix = camera inverse world-space matrix
     pVectSS = position vector screen-space
     hFOV = horizontal field of view
     vFOV = vertical field of view