I’ve written this python octree script for my current project in pyOgre, but its just occured to me that you could quite easily use it in BGE as well, so here it is…
if you dont know what an octree is, check out wikipedia. Basically, this script represents a very quick way to get rid of objects that are not important (ie: not close enough to collide with, or not in the camera’s viewing direction). There is some test code at the end which shows how to make use of the script. All this does is store whatever objects you wish in an octree. If you use it to query a position in the tree, it will very quickly return a list of objects closest to that position, without actually doing any distance calculations. In a way, its similar in principle to the BSP scene structure found in Quake etc., but more suited to outdoor environments.
PLEASE NOTE: this will not be the best solution for all circumstances, and I would carefully consider exactly what you are trying to do before making use of this.
Hope its useful, any comments or suggestions welcomed.
Sadly, I’ve migrated my game development to pyOgre now, instead of the BGE, (still using Blender to make all the content for my game), so I’m not sure I could write a useful tutorial on how to use this script in BGE. If anyone else wants to try, be my guest.