I am creating a list of projectiles, and every time one is created, it adds itself to this list.
LivingProjectiles=[]
def updateProjectiles():
for i in LivingProjectiles:
i.model.setPos(i.RenderWorld, i.body.getPosition())
i.model.setQuat(i.RenderWorld,Quat(i.body.getQuaternion()))
class Projectile(demobase.Att_base):
def __init__(self, parent, muzzleNode, ODEWorld, space, modelFile, RenderWorld):
demobase.Att_base.__init__(self,False, "Projectile")
self.ODEWorld= ODEWorld
self.RenderWorld= RenderWorld
self.space= space
self.myParent = parent
self.model = loader.loadModel(modelFile)
self.model.setHpr(muzzleNode.getHpr(self.RenderWorld))
curRot = self.model.getMat()
newPos = (muzzleNode.getPos(self.RenderWorld) + curRot.xformVec(Vec3(0,100,0)))
self.model.setPos(muzzleNode.getPos(self.RenderWorld))
self.model.setShaderAuto()
self.model.reparentTo(self.RenderWorld)
# Create the body and set the mass
self.body = OdeBody(self.ODEWorld)
M = OdeMass()
M.setSphere(0.5, 1)
self.body.setMass(M)
dir = self.model.getMat().getRow3(1)
self.body.addForce(dir[0]*100000,dir[1]*100000,dir[2]*100000)
self.body.setPosition(self.model.getPos(self.RenderWorld))
self.body.setQuaternion(self.model.getQuat())
self.bodyGeom = OdeSphereGeom(self.space, 1)
self.bodyGeom.setBody(self.body)
self.bodyGeom.setCollideBits(BitMask32(0x00000002))
self.bodyGeom.setCategoryBits(BitMask32(0x00000002))
LivingProjectiles.append( self )
I call UpdateProjectiles() every frame from my main, but the ODE update comes from a task.
def simulationTask(self,task):
iterations = 5
# limit the maximum time not to receive explosion of physics system if application stuck
dt=globalClock.getDt()
if dt>0.02: dt=0.02
dt=dt / iterations * 3
#Some iterations for the more stable simulation
for i in xrange(iterations):
self.world.quickStep(dt)
cc=self.space.autoCollide()
self.contactgroup.empty() #clear contacts before next step
self.parent.car.Sync()#sync the car
return task.cont
I also use this to set up the callback for collision detections
self.space.setCollisionEvent("ode-collision")
base.accept("ode-collision", self.onCollision)
now the question I have is when I detect a collision between something and one of my projectiles, I simply want to delete it. But I can’t find the documentation regarding the proper way to clean things up.
def onCollision(self, entry):
geom1 = entry.getGeom1()
geom2 = entry.getGeom2()
body1 = entry.getBody1()
body2 = entry.getBody2()
removeList = []
for i in LivingProjectiles:
if i.bodyGeom == geom1 or i.bodyGeom == geom2:
i.body.destroy()
removeList.append(i)
print "Collision"
for j in removeList:
LivingProjectiles.remove(j)
j.model.removeNode()
I get the “Collision” print, and it seems teh model is gone, but man-oh-man am I ever leaking. After 20 or so shots my framerate is abysmal.
Please help! I need to be educated on proper cleanup of Nodes! (I never thought I could miss pointers so much)