Scene Culling Script

Hi Folks,

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…

http://www.zerofilter.com/pyOgre/octree.py

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. :slight_smile:

i’m gonna try it out

Cool, I’d be interested to know how you get on!

As I said, its probably only useful if you have a lot of objects in your scene, and need to do a lot of collision checks every frame. It means that you dont have to check for every object against every other object in in the level, just the ones that the octree returns. I havent yet tried it out for view culling, as Ogre does that automatically. I forgot to mention, you should probably comment out all the stuff at the end as thats just to test it out.

If you have any questions I’ll try my best to answer them :slight_smile:

Hi
Your script seems to be very interesting.
Can I use it directly or should I (or anybody who knows python scripting, because I don’t know to make script) modify the script to use it ?

Thank
And sorry for my english :wink:

Hi,

yes you should be able to use it without modifying it I think. You need to add all of your static objects into the octree at the game startup, and then test the positions of your moving objects against the tree every frame to get a list of collisions. Heres a script that may get it to work, although I have not tried this out yet :slight_smile:

attach this to all the static objects you want to put into the tree. You only need this to fire ONCE, at the start of the game.


import GameLogic as g
from octree import Octree
 
cont = g.getCurrentController()
ob = cont.getOwner()
 
if not g.Octree:
    g.Octree = Octree(5000) #or however big your game world is
 
g.Octree.insertNode(g.Octree.root, 5000, g.Octree.root, ob)

Then for every moving object, that may potentially collide with an object in the tree, attach this script, and set it to fire every 2 or 3 frames:


import GameLogic as g
 
cont = g.getCurrentController()
ob = cont.getOwner()
 
if not g.Octree:
    pass # The octree has not been set up yet!
else:
    closeObjects = g.Octree.findPosition(g.Octree.root, ob.position)
 
if closeObjects == None:
    pass
else:
    # Do Something with each object....
    for obj in closeObjects:
        print obj.name
 

the ‘closeObjects’ property will be either None, if there are no objects nearby, or it will be a list of game objects. What you do after that is up to you :slight_smile:

Remember I only THINK this will work, I have not tried it properly yet!!

EDIT: It does work ( at least in my pyogre project ). I’ve managed to get a hundred or so robots walking around a landscape, all checking for collisions against 20 buildings every frame ( 100 x 20 = 2000 collision checks per frame ), and it runs at around 80 fps on my p3 2.8ghz, am understandably quite happy with that :slight_smile: