I need to retrieve all the objects visible by the active camera in the Game Engine.
I would like to use a combination of GameLogic.getCurrentScene().getObjectList() for the list of objects and GameLogic.getCurrentScene().active_camera.boxInsideFrustrum() to select the objects I actually see, but the later method needs the bouding boxes of the objects, and I can’t figure out a way to get then from my KX_GameObjects list.
Ok, I came up with a working solution. The chunck of scripts below allow to know if an object is currently visible by the active camera.
Store the list of the object you want to track (in my case, all the object with a property named “tracked”) in a global variable:
import Blender, GameLogic
scene = GameLogic.getCurrentScene()
GameLogic.trackedObjects = dict.fromkeys([ obj for obj in scene.getObjectList() if obj.getPropertyNames().count(‘tracked’)!=0 ])
Then, store their bounding boxes obtained from the equivalent Blender objects:
for obj in GameLogic.trackedObjects.keys():
GameLogic.trackedObjects[obj] = Blender.Object.Get(obj.name[2:]).getBoundBox(0)
Once this is done, the visibility of the objects can be checked like that:
def checkVisible(obj):
bb = trackedObjects[obj]
pos = obj.getPosition()
# translate the bounding box according to the current position of the object in the game engine.
if cam.boxInsideFrustum([[bb_corner[i] + pos[i] for i in range(3)] for bb_corner in bb]) != cam.OUTSIDE:
return True
The code could be improved to reflect the rotation of the object.