Collision Cleanup [SOLVED]

The time has come to remove bullets that have collided with things from the scene. Here is how I’m creating the bullets:

bulletCN = CollisionNode(self.name + "_Bullet")
bulletCS = CollisionSphere(0,0,0,.1)
bulletCN.addSolid(bulletCS)
bulletCN.setFromCollideMask(BitMask32.bit(3))
bulletCN.setIntoCollideMask(BitMask32.allOff())
bulletCNP = self.muzzle.attachNewNode(bulletCN)
bulletCNP.show()
self.cTrav.addCollider(bulletCNP, self.cHan)
self.bullets.append(bulletCNP)

I took a stab at the cleanup with this code:

bulletCNP = event.getFromNodePath()
bulletCNP.clearSolids()
self.cTrav.removeCollider(bulletCNP)
bulletCNP.removeNode()
del self.bullets[self.bullets.index(bulletCNP)]
		

and I discovered two things: I can’t call clearSolids on the nodepath I get from the collision entry. Also, that calling removeNode() also removed the node from my list. That made me wonder “what else is removeNode() doing?”

Ultimately, though, my question is what do I need to do to ensure that my bullet is completely removed from the scene graph?

clearSolids() is a method of CollisionNode, not of NodePath, so you would call it like this:

bulletCNP.node().clearSolids()

However, that’s a little paranoid. There’s no need to remove all of the CollisionSolids from the node; simply removing the node itself (from the scene graph and from the traverser) is sufficient.

It didn’t. But it changed your local bulletCNP NodePath to “removed”, while the NodePath that you’d previously added to the list was still referencing the original node, so these two NodePaths are no longer the same thing–and that’s why self.bullets.index(bulletCNP) didn’t find it in the list. Better to use detachNode() instead of removeNode() in this case–detachNode() works like removeNode() except it doesn’t clear the NodePath–or just don’t call removeNode() until after you have found it in the list and removed it.

David

Okay, I changed the cleanup code to

bulletCNP = event.getFromNodePath()
self.cTrav.removeCollider(bulletCNP)
del self.bullets[self.bullets.index(bulletCNP)]
bulletCNP.removeNode()

Everything works fine, until I put in a bunch of racers all firing their guns. Too many bullets cause serious performance issues.

I could reduce the rate of fire, or the number of guns per racer, so and so forth, but that isn’t going to achieve the feel I want in the game, so I’m going to change all the guns to an instant-hit style and just use one ray or segment per racer to determine hit or miss.

I’ll save this bullet code for missiles, which will be much more rare than bullets.

Thanks once again for your help, David. I’m planning on giving you a mention in my game’s credits.