Newbie Question - Bullet Triangle Mesh

Hey. I’m trying to basically create a ground plane, and create a Bullet static triangle mesh plane to represent that ground plane. I know that I could use just a BulletPlane (right?), but I don’t know if the ground would be always facing up, and if it would always be flat (I might change the mesh myself).

So, I’m trying to basically set Bullet physics up to work on a small section of ground, and then I could piece together the ground ‘tiles’, or import a whole ground mesh.

Here’s the code:

from direct.showbase.ShowBase import ShowBase

from panda3d.core import Vec3
from panda3d.bullet import BulletWorld
from panda3d.bullet import BulletPlaneShape
from panda3d.bullet import BulletRigidBodyNode
from panda3d.bullet import BulletBoxShape
from panda3d.bullet import BulletTriangleMeshShape
from panda3d.bullet import BulletTriangleMesh

from panda3d.bullet import BulletDebugNode

class MyApp(ShowBase):
    
    def __init__(self):
        
        ShowBase.__init__(self)
        
        dnode = BulletDebugNode('Debug')
        dnode.showWireframe(True)
        dnode.showConstraints(True)
        dnode.showBoundingBoxes(False)
        dnode.showNormals(False)
        
        debugnp = self.render.attachNewNode(dnode)
        debugnp.show()
        
        world = BulletWorld()
        world.setGravity(Vec3(0, 0, -9.81))
        world.setDebugNode(debugnp.node())

        streetsquare = self.loader.loadModel("Models/StreetSquare")
        streetsquare.reparentTo(self.render)
        
        geomnodes = streetsquare.findAllMatches('**/+GeomNode')
        gn = geomnodes.getPath(0).node()
        geom = gn.getGeom(0)
       
        mesh = BulletTriangleMesh()
        mesh.addGeom(geom)
        
app = MyApp()
app.run()

Now, the problem I’m having is pretty much just this:

I’m creating a Bullet triangle mesh from the geometry above, but I can’t quite seem to see why it’s not visible in the game with Debug rendering. I think the reparenting doesn’t work, and the attachRigidBody function doesn’t work either. Also, why do I have to create a new node (path?) for the debug node - isn’t it already a node? :S Any ideas?

I have managed to get this exact same thing working (debug rendering on a Bullet trimesh imported from a model). So you will succeed eventually. Beware, it’s really slow. Did you loop through every Geom node to make sure they’re all added to the mesh?

It’s slow, huh? Doesn’t seem like it should be slow… Do I have to loop through the nodes to make sure they’re all added? I didn’t see any mention of that in the manual - pretty much just this:

geomnodes = streetsquare.findAllMatches('**/+GeomNode')
gn = geomnodes.getPath(0).node()
geom = gn.getGeom(0)
       
mesh = BulletTriangleMesh()
mesh.addGeom(geom)

EDIT: Not really getting anywhere. The manual’s really foggy as to what you do after you get the geometry into the triangle mesh object. Now what? How do I actually place it in the game world? On the other examples, you would create a node and attach the mesh to the node, but that doesn’t work with triangle meshes.

The reason why you don’t see anything is because you have not created a rigid body. This is what you have to do:
(1) create the triangle mesh
(2) create a collison shape for this mesh
(3) create a rigid body and add the shape to it.

The manual pages will help you with this.

Wait, I did create the triangle mesh, but isn’t that the collision shape? When you say ‘create the triangle mesh’, what do you mean? The collision shape is not the triangle mesh?

Code after getting the geometry.

geom = gn.getGeom(0)
        
mesh = BulletTriangleMesh()
mesh.addGeom(geom)

So I get the geometry, create a triangle mesh, add the geometry to the mesh, and then…?

Somehow, I was missing the shape creation part of the manual. It’s probably just me, but Panda and Bullet seem a bit roundabout (the world is not the render (an object placed in render may not be updated, and an object placed in world isn’t in the scenegraph), a collision shape is not a collision mesh, etc). :confused: Anyway, thanks.

EDIT 2: I see now. The section of the manual dealing with triangle mesh collisions doesn’t have the shape creation part after you add the geometry to the mesh. That should probably be updated.

EDIT 3: Figured it out. Thanks again! Ended up beginning a helper module. Here’s the start. :slight_smile:

from panda3d.core import Vec3
from panda3d.bullet import BulletWorld
from panda3d.bullet import BulletPlaneShape
from panda3d.bullet import BulletRigidBodyNode
from panda3d.bullet import BulletBoxShape
from panda3d.bullet import BulletTriangleMeshShape
from panda3d.bullet import BulletTriangleMesh

from panda3d.bullet import BulletDebugNode

def ModelToBTS(model):
    
    geomnodes = model.findAllMatches('**/+GeomNode')

    gn = geomnodes.getPath(0).node()
    
    geom = gn.getGeom(0)
    
    mesh = BulletTriangleMesh()
    
    mesh.addGeom(geom)
    
    shape = BulletTriangleMeshShape(mesh, dynamic=False)
    
    return shape

That function converts a model into a triangle mesh shape and returns it. Some unnecessary imports at the beginning, but it works!

Thanks again, guys.