CollisionBox Help

Hey all, i tried to use collisionBox()'s in some of my code only to find out either im using them wrong or im just failing at collisions altogether. Heres the code with the CollisionBox()'s

 def createLevel(self):
    ##################################    
    #ok so! index = x pos! i = z pos!#
    ##################################
        self.rootNode = render.attachNewNode("ohaidernode")
        self.rootCollisionNode = render.attachNewNode(CollisionNode('worldCollider'))
        for i in range(len(self.levelArray)):
            for index, item in enumerate(self.levelArray[i]):
                if item == 0:
                    self.spawn(index, item, i)
                if item == 1:
                    self.addFloor(index, item, i)
                if item == 2:
                    self.addHole(index, item, i)
                if item == 3:
                    self.addWall(index, item, i)
                    
    def spawn(self, index, item, i):
        index = index
        item = item
        i = i
        cube = Cube(1, 0-1, 1)
        cube.setTwoSided(True)
        cube.setTexture(self.Texture)
        cube.reparentTo(self.rootNode)
        cube.setX(index)
        cube.setZ(i)
        box = CollisionBox(index, 0, i, .5, .5, .5)
        self.rootCollisionNode.node().addSolid(box)
        self.playerModel.setX(index + .5)
        self.playerModel.setZ(i + .5)
        self.playerModel.setY(-2)
        print(index, item, i)
                    
    def addFloor(self, index, item, i):
        index = index
        item = item
        i = i
        cube = Cube(1, 0-1, 1)
        cube.setTwoSided(True)
        cube.setTexture(self.Texture)
        cube.reparentTo(self.rootNode)
        cube.setX(index)
        cube.setZ(i)
        box = CollisionBox(index, 0, i, .5, .5, .5)
        self.rootCollisionNode.node().addSolid(box)
        print(index, item, i)
        
    def addHole(self, index, item, i):
        index = index
        item = item
        i = i
        print(index, item, i)
        
    def addWall(self, index, item, i):
        index = index
        item = item
        i = i
        cube = Cube(1, 0-3, 1)
        cube.setTwoSided(True)
        cube.setTexture(self.Texture)
        cube.reparentTo(self.rootNode)
        cube.setX(index)
        cube.setZ(i)
        box = CollisionBox(index, 0, i, .5, 1.5, .5)
        self.rootCollisionNode.node().addSolid(box)
        print(index, item, i)
        
    def flattenStrongRootNode(self):
        self.rootNode.flattenStrong()

and the collision code

class Collisions(ShowBase, object):
    
    def __init__(self, From, model, Into):
        self.playerCol = From
        self.playerModel = model
        self.rootCollisionNode = Into
        
        globalforcesFN = ForceNode('world-forces')
        globalforcesFNP = base.render.attachNewNode(globalforcesFN)
        globalforcesGravity = LinearVectorForce(0,9.81,0)
        globalforcesFN.addForce(globalforcesGravity)
        base.physicsMgr.addLinearForce(globalforcesGravity)
        
        def __init__(self):
            self.addCollisions()
            
        def addcollisions(self):
            base.cTrav = CollisionTraverser()
            CollisionHandler = PhysicsCollisionHandler()
            
            CollisionHandler.addCollider(self.playercol, self.playerModel)
            base.cTrav.addCollider(self.playerCol, collisionHandler)

Tips you can use to help debug problems with collisions:

(1) Turn on collision visualization.

base.cTrav.showCollisions(render)

This will light up collision solids that are being tested for intersection. If you see things lighting up in yellow, it means the collision solids are being tested; if you see them in white, it means a collision is detected.

(2) Turn on notify debugging:

notify-level-collide debug

This will report collisions that are discovered

notify-level-collide spam

This is very noisy, but it will report everything the collision traversal is doing each frame, including all of the nodes it visits. It may be useful if you can’t figure out why your nodes aren’t being tested for collisions.

(3) Reveal collision solids:

self.rootCollisionNode.show()

Simply making your collision nodes visible can help reveal problems with placement or size that you weren’t aware of. This interferes a little with showCollisions(), above, so don’t do both at once.

David

Thanks David! Always so helpful ^^ cant wait to get home and see what happens

ok so. None of those did a thing. it looks like i have no collision solids =/ Any ideas? Ive never used collisionBox() before and there is almost no documentation on them. am i using them right?

I don’t see anything obviously wrong in the CollisionBox creation itself. CollisionBox isn’t any different from any other collision solid, really.

No collision solids? Really? Not even one box? Are you sure you’re actually calling the code that creates them? Try putting a print statement when you create a CollisionBox. Also try doing render.ls() afterwards just to see what you’ve got.

David

i tried the render().ls (EDIT - just realized it should be render.ls() ill try again later XD) this morn and nothing happened. I also made an extra collisionBox() floating in empty space to see what happens and the result was surprising and entertaining. ColliionBox(0,-2,0,10,10,10) parented to a collision node with show() enabled gave me a a blob of collision planes. I’ll post a screenshot when i get home. Any ideas? I think my models are hiding the other collisioncubes, ill try the self.rootollisionNode.show() once i get home with the models hidden.

lol im not sure whats up with that. Heres the code for that

cb = CollisionBox(0,-2,0,10,10,10)
        node = render.attachNewNode(CollisionNode('nodey'))
        node.node().addSolid(cb)
        node.show()

i also checked if the collision solids were bweing hidden by my models and they werent. Theres no collision solids =/ idk why.

Oh, I see the problem now. There’s a bug in the six-parameter CollisionBox constructor.

To avoid this bug, use CollisionBox((0,-2,0),10,10,10) instead of CollisionBox(0,-2,0,10,10,10).

David

Kk, thanks David ^^ I’ll give it a shot and reply back :slight_smile:

so the CollisionBox((0,-2,0),10,10,10) worked great and i applied it to my other collisionBoxes but they still arent being created for some reason =/

Sounds like an opportunity to flex your debugging muscles. :slight_smile:

David

:stuck_out_tongue:

OMG over 2 weeks and i FINALLY REALIZED (well one week) that i had 2

def __init__(self):

in my collisions class and the one calling the collisions was tghe second one so its being treated as a regular function that hasnt been caleld yet =/ i feel stupid

That’s a good one!

Not to worry, being a programmer means making lots of random stupid mistakes. Being a good programmer means being able to find them and fix them. :slight_smile:

David

well after more debugging with the collisions i found out what else was making the collisions not work. The collisionBox()'s D: using the same collision system i replaced the players collisionBox with a sphere and noticed something strange. The sphere collides with some but not all of the collisionBox()'s sides. i still cant figure out which sides are “broken” i also put a CollisionPlane() below the level when i was us ing the collisionBox() for the player and the player slid right through the plane. Is there a setTwoSided() parameter for the collisionBox() so all sides listen for collisions or am i missing something?

Hmm, perhaps the CollisionBox needs to be larger than the sphere for it to work effectively.