I don't know how to make an object iterate in a python class and still have it be individually addressable in Bullet

This is an object from a greater class i have been unable to get to work to create multiple addressable cubes. I first tried making a separate class (init and update) to circumvent this issue. However then I couldn’t address “bullet world” so i couldn’t actually add physics to the cubes. However the issue with this code is I can’t address the cubes in the update object individually to let the player control one of the cubes, not all of them. Any help would be greatly appreciated…

def createBox(self, x, y, z):
    # Box (dynamic)
    shape = BulletBoxShape(Vec3(0.5, 0.5, 0.5))

    self.boxNP = self.worldNP.attachNewNode(BulletRigidBodyNode('Box'))
    self.boxNP.node().setMass(1.0)
    self.boxNP.node().addShape(shape)
    self.boxNP.setPos(x, y, z)
    #self.boxNP.setScale(2, 1, 0.5)
    self.boxNP.setCollideMask(BitMask32.allOn())
    #self.boxNP.node().setDeactivationEnabled(False)

    self.world.attachRigidBody(self.boxNP.node())

    visualNP = loader.loadModel('Models/Box/plainbox.egg')
    visualNP.clearModelNodes()
    visualNP.reparentTo(self.boxNP)

What I’d suggest is that you have some controlling object (perhaps your “game” class, or a “world” class) keep a list or dictionary of these box-objects. These could be gathered by having your “createBox” method return the box-object once it completes, or, if the class that holds “createBox” is itself the controlling class, by simply putting them into the list within “createBox”.

So something like this, perhaps skipping bits for brevity and clarity):

def createBox(self, x, y, z):
    # Create your shape as per usual...

    boxNP = self.worldNP.attachNewNode(BulletRigidBodyNode('Box'))
    # Note that it's no longer "self.boxNP"--this is because there
    # isn't going to be just one, but many so created (presumably)
    
    # The rest of your logic here, pretty much as above

    self.boxList.append(boxNP)

Or, alternatively:

def createBox(self, x, y, z):
    # Create your shape as per usual...

    boxNP = self.worldNP.attachNewNode(BulletRigidBodyNode('Box'))
    # Again, note that it's no longer "self.boxNP"--this is because there
    # isn't going to be just one, but many so created (presumably)
    
    # The rest of your logic here, pretty much as above

    return boxNP

# SOMEWHERE ELSE

for i in range(howeverMany):
    newBox = self.createBox(x, y, z)
    self.boxList.append(newBox)

As to picking the boxes to be manipulated, that might depend on just how you want to go about it. If the index of the given box is enough–and won’t change due to objects being removed–then you can simply use that. Otherwise, you could switch from a list to a dictionary and use a unique ID of some sort to select from the cubes.

Finally, if I may, I have a “Beginner’s Tutorial” that (amongst other things) shows an example of such a system as I recall. If you’re interested, you should find the tutorial here:

Thank you so much ^-^

1 Like