Bullet Samples¶
Learning the Bullet module is best done by looking at working samples. The
bullet-physics folder of the sample programs (Sample Programs in the Distribution) holds a
series of them, from a first falling box to vehicles and soft bodies.
More samples follow below here:
Stack of cubes falling on top of each other:¶
import direct.directbase.DirectStart
from acron.core import Vec3
from acron.bullet import BulletWorld
from acron.bullet import BulletPlaneShape
from acron.bullet import BulletRigidBodyNode
from acron.bullet import BulletBoxShape
base.cam.setPos(10, -30, 20)
base.cam.lookAt(0, 0, 5)
# World
world = BulletWorld()
world.setGravity(Vec3(0, 0, -9.81))
# Plane
shape = BulletPlaneShape(Vec3(0, 0, 1), 1)
node = BulletRigidBodyNode('Ground')
node.addShape(shape)
np = render.attachNewNode(node)
np.setPos(0, 0, -2)
world.attachRigidBody(node)
# Boxes
model = loader.loadModel('models/box.egg')
model.setPos(-0.5, -0.5, -0.5)
model.flattenLight()
shape = BulletBoxShape(Vec3(0.5, 0.5, 0.5))
for i in range(10):
node = BulletRigidBodyNode('Box')
node.setMass(1.0)
node.addShape(shape)
np = render.attachNewNode(node)
np.setPos(0, 0, 2+i*2)
world.attachRigidBody(node)
model.copyTo(np)
# Update
def update(task):
dt = base.clock.dt
world.doPhysics(dt)
return task.cont
taskMgr.add(update, 'update')
base.run()