I just wrote this, and I am told module mode can save speed,
can someone help me get every last cpu cycle milked out of this?
this same system can also process LOL (level of logic) and be a dynamic lighting manager. (but that is for later)
import bge
import mathutils
cont = bge.logic.getCurrentController()
scene = bge.logic.getCurrentScene()
own = cont.owner
if 'Master' not in own:
# build list of objects to add to kdtree
Master = []
for objects in scene.objects:
if 'StaticTag' in objects:
Master.append(objects)
own['Master']=Master
# make empty tree of correct size
size = len(Master)
kd = mathutils.kdtree.KDTree(size)
index = 0
# add all objects from list to kdtree
for gameObject in Master:
if 'StaticTag' in gameObject:
kd.insert(gameObject.worldPosition, index)
index+=1
L=[]
# Store children that will have physics shapes
for child in gameObject.children:
if 'Shape' in child:
L.append(child)
if 'Physical' in child:
child.removeParent()
if len(L)>0:
gameObject['ChildPhys']=L
# prep kdtree for use
kd.balance()
own['Kd']=kd
else:
kd = own['Kd']
# this will be needed later for light LOD
#for (co, index, dist) in kd.find_n(own.worldPosition, 10):
# build close list
co_find = own.worldPosition
total=[]
near =[]
for (co, index, dist) in kd.find_range(co_find, 40):
#print(" ", co, index, dist)
target = own['Master'][index]
if 'Phys' not in target:
target['Phys'] = bge.logic.getCurrentScene().addObject(target['StaticTag'],target,0)
#if target.visible==False:
# target.visible=True
if 'ChildPhys' in target:
for ob in target['ChildPhys']:
if 'Phys' not in ob:
ob['Phys']=scene.addObject(ob['Shape'],ob,0)
#if ob.visible ==False:
#ob.visible=True
total.append(index)
near.append(index)
# build far list
far =[]
for (co, index, dist) in kd.find_range(co_find, 50):
if index not in near:
far.append(index)
total.append(index)
target = own['Master'][index]
if not target.invalid:
if 'Phys' in target:
target['Phys'].endObject()
del target['Phys']
if 'ChildPhys' in target:
for ob in target['ChildPhys']:
if 'Phys' in ob:
if not ob['Phys'].invalid:
ob['Phys'].endObject()
del ob['Phys']
#if ob.visible==True:
# ob.visible=False
# object not in near or far should be invisble
#index = 0
#for object in own['Master']:
# if index not in total:
# if own['Master'][index].visible==True:
# own['Master'][index].visible=False
index+=1
#new = []
#for objects in own['Master']:
#new.append([objects,objects.getDistanceTo(own.worldPosition)])
#new = sorted(new, key=lambda tup: tup[1])
import bge
import mathutils
cont = bge.logic.getCurrentController()
scene = bge.logic.getCurrentScene()
own = cont.owner
if 'Master' not in own:
# build list of objects to add to kdtree
Master = []
for objects in scene.objects:
if 'StaticTag' in objects:
Master.append(objects)
own['Master']=Master
# make empty tree of correct size
size = len(Master)
kd = mathutils.kdtree.KDTree(size)
index = 0
# add all objects from list to kdtree
for gameObject in Master:
if 'StaticTag' in gameObject:
kd.insert(gameObject.worldPosition, index)
index+=1
L=[]
# Store children that will have physics shapes
for child in gameObject.children:
if 'Shape' in child:
L.append(child)
if 'Physical' in child:
child.removeParent()
if len(L)>0:
gameObject['ChildPhys']=L
# prep kdtree for use
kd.balance()
own['Kd']=kd
else:
kd = own['Kd']
# this will be needed later for light LOD
#for (co, index, dist) in kd.find_n(own.worldPosition, 10):
# build close list
co_find = own.worldPosition
total=[]
near =[]
for (co, index, dist) in kd.find_range(co_find, 40):
#print(" ", co, index, dist)
target = own['Master'][index]
if 'Phys' not in target:
target['Phys'] = bge.logic.getCurrentScene().addObject(target['StaticTag'],target,0)
#if target.visible==False:
# target.visible=True
if 'ChildPhys' in target:
for ob in target['ChildPhys']:
if 'Phys' not in ob:
ob['Phys']=scene.addObject(ob['Shape'],ob,0)
#if ob.visible ==False:
#ob.visible=True
total.append(index)
near.append(index)
# build far list
far =[]
for (co, index, dist) in kd.find_range(co_find, 50):
if index not in near:
far.append(index)
total.append(index)
target = own['Master'][index]
if not target.invalid:
if 'Phys' in target:
target['Phys'].endObject()
del target['Phys']
if 'ChildPhys' in target:
for ob in target['ChildPhys']:
if 'Phys' in ob:
if not ob['Phys'].invalid:
ob['Phys'].endObject()
del ob['Phys']
#if ob.visible==True:
# ob.visible=False
# object not in near or far should be invisble
#index = 0
#for object in own['Master']:
# if index not in total:
# if own['Master'][index].visible==True:
# own['Master'][index].visible=False
index+=1
#new = []
#for objects in own['Master']:
#new.append([objects,objects.getDistanceTo(own.worldPosition)])
#new = sorted(new, key=lambda tup: tup[1])
Attachments
PhysicsLOD.blend (3.55 MB)