bharling
19-02-2009 10:05:33
Hi folks,
have been trying for a few days now to get anywhere with bullet physics in Python-Ogre, with very little success. test01.py and test02.py work for me, but everything fails when I try to do anything myself. I cant get the motionState stuff to work at all, and I'm getting some nasty crashes! Here's the code I cobbled together from the other demos if anyone can help
I notice the world object seems very 'delicate', ie: you can't pass it as an argument to a framelistener otherwise it gets very upset!
with this code or slight variants thereof I either get a crash or 'pure virtual' error... nasty!
I'm guessing the wrapper for bullet is still in the embryonic stages at the moment.
many thanks,
have been trying for a few days now to get anywhere with bullet physics in Python-Ogre, with very little success. test01.py and test02.py work for me, but everything fails when I try to do anything myself. I cant get the motionState stuff to work at all, and I'm getting some nasty crashes! Here's the code I cobbled together from the other demos if anyone can help
import sys, os
sys.path.insert(0,'..')
import PythonOgreConfig
if sys.platform == 'win32':
newpath = os.path.join ( os.path.abspath(os.path.dirname(__file__)), '../../plugins')
os.environ['PATH'] = newpath +';' + os.environ['PATH']
import ogre.renderer.OGRE as ogre
import ogre.physics.OgreBulletC as bulletC
import ogre.physics.bullet as bullet
import SampleFramework as sf
class BulletListener( sf.FrameListener ):
def __init__(self, rw, cam, world ):
sf.FrameListener.__init__(self, rw, cam)
self.world = world
def frameStarted(self, evt):
self.world.stepSimulation( evt.timeSinceLastFrame )
return sf.FrameListener.frameStarted( self, evt )
class OgreMotionState(bullet.btMotionState):
def __init__(self, initalPosition):
bullet.btMotionState.__init__(self)
self.Pos=initalPosition
self.Position=ogre.Vector3()
self.Quaternion=ogre.Quaternion()
def getWorldTransform(self, WorldTrans):
print WorldTrans
print WorldTrans.Rotation.x(),WorldTrans.Rotation.y(),WorldTrans.Rotation.z()
print self.Pos.getOrigin()
WorldTrans.setOrigin(self.Pos.getOrigin())
WorldTrans.setBasis(self.Pos.getBasis())
print WorldTrans.Rotation.x(),WorldTrans.Rotation.y(),WorldTrans.Rotation.z()
print WorldTrans
print "Pos", self.Pos
t=self.Pos
print "T", t
t.setOrigin(WorldTrans.getOrigin())
print "T1", t
def setWorldTransform(self, WorldTrans):
#print "setWorldTrans", WorldTrans
self.Position=ogre.Vector3(WorldTrans.getOrigin().x(),WorldTrans.getOrigin().y(),WorldTrans.getOrigin().z())
self.Quaternion=ogre.Quaternion(WorldTrans.getRotation().w(),WorldTrans.getRotation().x(),WorldTrans.getRotation().y(),WorldTrans.getRotation().z())
#print "setWorldTrans", WorldTrans
class BulletApplication( sf.Application ):
def _createScene( self ):
self.setupBullet()
self.createBoxes()
def __del__(self):
if self.world:
del self.world
#sf.Application.__del__(self)
def setupBullet( self ):
self.collisionConfiguration = bullet.btDefaultCollisionConfiguration()
self.dispatcher = bullet.btCollisionDispatcher (self.collisionConfiguration)
worldAabbMin = bullet.btVector3(-1000,-1000,-1000)
worldAabbMax = bullet.btVector3(1000,1000,1000)
maxProxies = 32766
self.broadphase = bullet.btAxisSweep3(worldAabbMin, worldAabbMax, maxProxies)
self.solver = bullet.btSequentialImpulseConstraintSolver()
self.world = bullet.btDiscreteDynamicsWorld(self.dispatcher, self.broadphase , self.solver, self.collisionConfiguration)
self.world.stepSimulation(0.1)
self.world.stepSimulation(1)
def createBoxes( self ):
ent = self.sceneManager.createEntity( "box", "cube.mesh" )
node = self.sceneManager.getRootSceneNode().createChildSceneNode()
node.attachObject( ent )
ent.setMaterialName( "Examples/RustySteel")
mass = 10
fallInertia = bullet.btVector3(0, 0, 0)
shape=bullet.btSphereShape(1)
shape.calculateLocalInertia(mass, fallInertia)
print "Creating motionState"
motionState=OgreMotionState(bullet.btTransform(bullet.btQuaternion(0, 0, 0, 1), bullet.btVector3(0, 50, 0)))
print "creating construct"
construct = bullet.btRigidBody.btRigidBodyConstructionInfo(mass, motionState, shape, fallInertia)
print "creating object"
Object=bullet.btRigidBody(construct) # ...this should work in my eyes
print "adding rigidBody"
self.world.addRigidBody(Object)
print "completed"
self.world.stepSimulation(1)
def _createFrameListener( self ):
self.fl = BulletListener( self.renderWindow, self.camera, None )
self.fl.world = self.world
self.root.addFrameListener( self.fl )
if __name__=="__main__":
app = BulletApplication()
app.go()
I notice the world object seems very 'delicate', ie: you can't pass it as an argument to a framelistener otherwise it gets very upset!
with this code or slight variants thereof I either get a crash or 'pure virtual' error... nasty!
I'm guessing the wrapper for bullet is still in the embryonic stages at the moment.
many thanks,