Apply rotation to collision box

Hi folks,

I have position, rotation and scale data that I want to assign to collision boxes.

def add_box(self, position, rotation, scale):
        box_n = self.scene.loader.loadModel("models/box")
        box_n.setPos(position - Point3(scale.x/2, scale.y/2, scale.z/2))
        box_n.setScale(scale.x, scale.y, scale.z)
        box_n.setHpr(rotation)
        box_n.flattenLight()
        box_n.setTextureOff()

        colliderNode = CollisionNode("box")
        colliderNode.setIntoCollideMask(GeomNode.getDefaultCollideMask())
        colliderNode.addSolid(CollisionBox(position, scale.x, scale.y, scale.z))

        collider = box_n.attachNewNode(colliderNode)
        collider.show()

        box_n.reparentTo(self.scene.render)

but it looks like the rotation is only applied on the box itself and not the collisionbox attached to it…
both the boxes and the collision boxes need to have the same pos, rot, scale parameters
what am I doing wrong?

flattenLight removes the pos, hpr and scale from the model. You are henceforth not applying a rotation to the CollisionBox (and in fact, a CollisionBox is an AABB and can’t have an applied rotation), only the position and scale.

To fix, move the setHpr line to below the flattenLight line.

It worked, thanks!
I had to adjust the collisionbox initiation a little

These are the node params

box_n.setPos(position)
box_n.setScale(scale.x, scale.y, scale.z)
box_n.setHpr(rotation)

and that’s the adjusted collisionbox

colliderNode.addSolid(CollisionBox(Point3(0, 0, 0), 1, 1, 1))

but I don’t seem to get how the box and bollisionbox relate to one another.
which one has the correct position / scale?

My basic assumption is that the box model’s size is 1, 1, 1… is it possible that it’s actually a quarter of it?

image

To be sure, you can call get_tight_bounds on box_n.

But the problem is most likely this:

The 1, 1, 1 there are not scale values for a unit-sized cube; rather, they are the offsets (distances) of the sides of the CollisionBox from its center. So this leads to a total collision-box size of (2, 2, 2). You can check this by printing out its dimensions:

coll_box = CollisionBox(Point3(0, 0, 0), 1, 1, 1)
print(coll_box.dimensions)

So I think what you want is this:

colliderNode.addSolid(CollisionBox(Point3(0, 0, 0), .5, .5, .5))

Judging from your screenshot, it’s possible that you might have to change the center position of the collision-box to something like (.5, .5, .5) as well, but I’m not sure.

1 Like