Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import sys
- from direct.showbase.ShowBase import ShowBase
- from direct.gui.OnscreenText import OnscreenText
- from panda3d.core import PointLight, VBase4, DirectionalLight, LVector3f, LPoint3f, Vec3, GeomEnums, GeomNode, TransformState, NodePath
- from panda3d.bullet import BulletWorld, BulletTriangleMesh, BulletRigidBodyNode, BulletTriangleMeshShape, BulletConvexHullShape, BulletPlaneShape, BulletBoxShape, BulletDebugNode
- class MyApp(ShowBase):
- def __init__(self):
- ShowBase.__init__(self)
- self.text = None
- self.rebuild()
- self.accept('escape', sys.exit)
- self.accept('f', self.toggleWireframe)
- self.accept('r', self.rebuild)
- self.taskMgr.add(self.update, 'update')
- def rebuild(self):
- self.clean()
- if self.load_dynamic_body == self.load_dynamic_body1:
- self.load_dynamic_body = self.load_dynamic_body2
- self.text = OnscreenText(text = 'With triangle mesh (press r to change)', pos = (-0.5, 0.5))
- else:
- self.load_dynamic_body = self.load_dynamic_body1
- self.text = OnscreenText(text = 'With convex hull (press r to change)', pos = (-0.5, 0.5))
- self.world = BulletWorld()
- self.world.setGravity(Vec3(0, 0, -9.81))
- debugNP = self.render.attachNewNode(BulletDebugNode('Debug'))
- debugNP.show()
- self.world.setDebugNode(debugNP.node())
- self.load_scene('myscene')
- self.cam.setPos(10, 10, 10)
- self.cam.lookAt(0, 0, 0)
- def clean(self):
- for child in self.render.getChildren():
- if child.getName() != 'camera':
- child.removeNode()
- if self.text:
- self.text.removeNode()
- def update(self, task):
- dt = globalClock.getDt()
- self.world.doPhysics(dt/3)
- return task.cont
- def load_scene(self, filename):
- scene_root_np = self.loader.loadModel(filename)
- scene_root_np.flattenLight()
- for object_root_np in scene_root_np.findAllMatches('**/coll_dyn*'):
- self.load_dynamic_body(scene_root_np, object_root_np, mass=1)
- for object_root_np in scene_root_np.findAllMatches('**/coll_stat*'):
- self.load_static_body(scene_root_np, object_root_np)
- for object_root_np in scene_root_np.findAllMatches('**/coll_ghost*'):
- self.load_ghost(scene_root_np, object_root_np)
- self.render.ls()
- def load_dynamic_body1(self, scene_root_np, object_root_np, mass):
- body = BulletRigidBodyNode('BodyNode')
- for geom_np in object_root_np.findAllMatches('**/+GeomNode'):
- shape = BulletConvexHullShape()
- for geom in geom_np.node().getGeoms():
- geom = geom.decompose()
- shape.addGeom(geom)
- body.addShape(shape)
- body.setMass(mass)
- body_np = self.render.attachNewNode(body)
- object_root_np.reparentTo(body_np)
- self.world.attachRigidBody(body)
- def load_dynamic_body2(self, scene_root_np, object_root_np, mass):
- body = BulletRigidBodyNode('BodyNode')
- for geom_np in object_root_np.findAllMatches('**/+GeomNode'):
- triangle_geom = BulletTriangleMesh()
- for geom in geom_np.node().getGeoms():
- geom = geom.decompose()
- triangle_geom.addGeom(geom)
- shape = BulletTriangleMeshShape(triangle_geom, dynamic=(mass==0))
- body.addShape(shape)
- body.setMass(mass)
- body_np = self.render.attachNewNode(body)
- object_root_np.reparentTo(body_np)
- self.world.attachRigidBody(body)
- def load_static_body(self, scene_root_np, object_root_np):
- return self.load_dynamic_body(scene_root_np, object_root_np, mass=0)
- def load_ghost(self, scene_root_np, object_root_np):
- object_root_np.reparentTo(self.render)
- load_dynamic_body = load_dynamic_body1
- app = MyApp()
- app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement