def createGhostObjectEntity(self, sceneNode, radius): pos = sceneNode.getPosition() transform = bullet.btTransform() transform.setIdentity() transform.setOrigin(bullet.btVector3(pos.x, pos.y, pos.z)) ghostObject = bullet.btPairCachingGhostObject() ghostObject.setWorldTransform(transform) sphereShape = bullet.btSphereShape(1.0) ghostObject.setCollisionShape(sphereShape) #ghostObject.setCollisionFlags(bullet.btCollisionObject.CO_GHOST_OBJECT) actionEntity = ActionEntity(self.world, ghostObject, sceneNode) self.world.addCollisionObject(ghostObject, bullet.btBroadphaseProxy.SensorTrigger, bullet.btBroadphaseProxy.AllFilter) #self.world.getBroadphase().getOverlappingPairCache().setInternalGhostPairCallback(bullet.btGhostPairCallback()) #self.world.addAction(actionEntity) self.shapes.append(sphereShape) return actionEntity
def _addCube(self, pos, cubeBounds=ogre.Vector3(0.3, 0.3, 0.3), tangentBundle=None): #cubeBounds=bullet.btVector3(0.3, 0.3, 0.3)): ent = self.sceneManager.createEntity("Bulletbox.mesh") ent.setMaterialName("GridNormalMap3") node = self.sceneManager.getRootSceneNode().createChildSceneNode() node.attachObject(ent) node.scale(cubeBounds) #node.scale(cubeBounds.getX(), cubeBounds.getY(), cubeBounds.getZ()) mass = 1.0 res = 0.01 bodyFriction = 0.2 fallInertia = bullet.btVector3(0, 0, 0) cubeIdx = len(self.bodies) cubeShape = bullet.btBoxShape(collisions.OgreBtConverter.to(cubeBounds)) cubeShape.calculateLocalInertia(mass, fallInertia) cubeMotionState = OgreMotionState(bullet.btTransform(bullet.btQuaternion(0, 0, 0, 1), bullet.btVector3(pos.x, pos.y, pos.z)), node, tb=tangentBundle) cubeBodyCI = bullet.btRigidBody.btRigidBodyConstructionInfo(mass, cubeMotionState, cubeShape, fallInertia) cubeBody = bullet.btRigidBody(cubeBodyCI) cubeBody.setActivationState(4) #state is never deactivate cubeMotionState.rb = cubeBody self.world.addRigidBody(cubeBody) self.motionStates.append(cubeMotionState) """ cubeShape = collisions.BoxCollisionShape(cubeBounds) cubeBody = dynamics.RigidBody("cube" + str(cubeIdx), self.world) cubeBody.setShape(node, cubeShape, res, bodyFriction, mass, pos) cubeBody.getBulletRigidBody().setActivationState(4) """ self.bodies.append(cubeBody) self.shapes.append(cubeShape) return cubeBody
def update(self, pos, orientation, acceleration, velocity): if(self.rigidBody != None): #self.rigidBody.applyForce(physics.toBtVector3(acceleration), physics.toBtVector3(pos)); self.rigidBody.proceedToTransform(bullet.btTransform(physics.toBtQuaternion(ogre.Quaternion(0, (0, 1, 0))), physics.toBtVector3(pos))); self.rigidBody.setAngularVelocity(bullet.btVector3(0,0,0)); self.rigidBody.setLinearVelocity(bullet.btVector3(0,0,0)); self.orientation = orientation; acceleration = orientation * acceleration; self.velocity = orientation*velocity; orientationx = ogre.Quaternion((5.0*(acceleration.z))*(math.pi/180.0), (1,0,0)); orientationz = ogre.Quaternion((5.0*(acceleration.x))*(math.pi/180.0), (0,0,1)); orientation = orientation * orientationx * orientationz; self.mainCamera.setOrientation(orientation); self.mainCamera.setPosition(pos); if self.multipleCameras: sqrPt5 = math.sqrt(0.5); leftOrientation = orientation * ogre.Quaternion(self.cameraAngle * (math.pi/180.0), (0,1,0)); self.leftCamera.setOrientation(leftOrientation); self.leftCamera.setPosition(pos); rightOrientation = orientation * ogre.Quaternion(-self.cameraAngle * (math.pi/180.0), (0,1,0)); self.rightCamera.setOrientation(rightOrientation); self.rightCamera.setPosition(pos);
def createBody(self, mass, position): position = bullet.btVector3(*position) transform = bullet.btTransform(bullet.btQuaternion(0, 0, 0, 1), position) if self.hasGhostObject: body = bullet.btPairCachingGhostObject() body.setCollisionShape(self.physShape) body.setWorldTransform(transform) self.world.physWorld.addCollisionObject(body, getTagBits(self.tags), getTagBits(self.collisionTags)) else: self.motionState = self.MotionState(self, transform) self.physMass = mass localInertia = bullet.btVector3(0, 0, 0) self.physShape.calculateLocalInertia(mass, localInertia) construct = bullet.btRigidBody.btRigidBodyConstructionInfo(mass, self.motionState, self.physShape, localInertia) body = bullet.btRigidBody(construct) self.world.physWorld.addRigidBody(body, getTagBits(self.tags), getTagBits(self.collisionTags)) body.setCollisionFlags(body.getCollisionFlags() | self.physFlags) body.setUserData(self) self.physBody = body
def createBoxes( self ): print 2 ent = self.sceneManager.createEntity( "box", "cube.mesh" ) print 2 node = self.sceneManager.getRootSceneNode().createChildSceneNode() print 2 node.attachObject( ent ) print 2 ent.setMaterialName( "Examples/RustySteel") print 2 mass = 10 fallInertia = bullet.btVector3(0, 0, 0) print 2 shape=bullet.btSphereShape(1) print 2 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 add_block_without_rebuild(self, block, x, y, z): transform = bullet.btTransform(bullet.btQuaternion(0, 0, 0, 1), bullet.btVector3(x * Block.size, y * Block.size, z * Block.size)) self.collision_shape.addChildShape(transform, BOX_COLLISION_SHAPE) self.collision_shape_indicies[(x, y, z)] = self.block_count self._blocks[(x, y, z)] = block self.block_count += 1
def register_collision_object(self, size, x, y, z, rotation=bullet.btQuaternion(0, 0, 0, 1), mass=0, motion_state=bullet.btDefaultMotionState): """Register a simple collision object with bullet.""" shape = bullet.btBoxShape(bullet.btVector3(size / 2, size / 2, size / 2)) transform = bullet.btTransform(rotation, bullet.btVector3(x, y, z)) inertia = bullet.btVector3(0, 0, 0) motion_state = motion_state(transform) construction = bullet.btRigidBody.btRigidBodyConstructionInfo(mass, motion_state, shape, inertia) rigid_body = bullet.btRigidBody(construction) self.dynamics_world.addRigidBody(rigid_body, 1, 1) return rigid_body, motion_state, shape
def fireCube(self, pos, force): cube = self.cubes[(self.curCubeIdx) % len(self.cubes)] self.curCubeIdx += 1 transform = bullet.btTransform() transform.setOrigin(bullet.btVector3(pos.x, pos.y, pos.z)) cube.clearForces() cube.setLinearVelocity(bullet.btVector3(0.0, 0.0, 0.0)) cube.setWorldTransform(transform) impulse = bullet.btVector3(force.x, force.y, force.z) cube.applyImpulse(impulse, bullet.btVector3(0.0, 0.0, 0.0))
def create_compound_shape(self, x, y, z, mass=0): """Create a compound shape and register it with bullet.""" shape = bullet.btCompoundShape() transform = bullet.btTransform(bullet.btQuaternion(0, 0, 0, 1), bullet.btVector3(x, y, z)) inertia = bullet.btVector3(0, 0, 0) motion_state = bullet.btDefaultMotionState(transform) construction = bullet.btRigidBody.btRigidBodyConstructionInfo(mass, motion_state, shape, inertia) rigid_body = bullet.btRigidBody(construction) self.dynamics_world.addRigidBody(rigid_body, 1, 1) return rigid_body, motion_state, shape
def __init__(self, dynamicsWorld): """Base Collision Object for Testing""" self.mShape = None self.dynamicsWorld = dynamicsWorld self.mRigidBody = None self.mTransform = bullet.btTransform() self.mTransform.setIdentity() self.mInertia = bullet.btVector3(0, 0, 0) self.mMotionState = None self.mMass = 0.0 self.mTriMesh = None
def createGround(self, sceneNode): groundShape = bullet.btStaticPlaneShape(bullet.btVector3(0,1,0),0); # Skapa motion state motionState = bullet.btDefaultMotionState(bullet.btTransform( bullet.btQuaternion(0,0,0,1), bullet.btVector3(0,-1,0))); constructInfo = bullet.btRigidBody.btRigidBodyConstructionInfo( 0, motionState, groundShape, bullet.btVector3(0,0,0)); rigidBody = bullet.btRigidBody(constructInfo); self.bodies.append(PhysicsWorld.Body(groundShape, motionState, constructInfo, rigidBody)); self.world.addRigidBody(rigidBody);
def register_entity(self, entity, mass, motion_state): """Register an entity that is moved by the engine. This allows the physics system to work together with the engine in order to know about the movement the engine produces, but also the movement that is caused through the physics simulations""" #logging.info("Registering %s with the physics system. kinematic=%s" % (str(entity), str(kinematic))) entity.shape = self._create_collision_entity(entity) transform = bullet.btTransform(bullet.btQuaternion(0, 0, 0, 1), bullet.btVector3(entity.position.x, entity.position.y, entity.position.z)) inertia = bullet.btVector3(0, 0, 0) entity.motion_state = motion_state(transform) construction = bullet.btRigidBody.btRigidBodyConstructionInfo(mass, entity.motion_state, entity.shape, inertia) entity.rigid_body = bullet.btRigidBody(construction) self.dynamics_world.addRigidBody(entity.rigid_body, 1, 1) return entity.rigid_body
def createCameraBody(self, camera, halfSize, mass): pos = camera.getPosition(); rot = camera.getOrientation(); # Skapa motion state motionState = bullet.btDefaultMotionState(bullet.btTransform( bullet.btQuaternion(0,0,0,1), bullet.btVector3(pos.x,pos.y,pos.z))); shape = bullet.btBoxShape(toBtVector3(halfSize)); inertia = bullet.btVector3(0,0,0); shape.calculateLocalInertia(mass, inertia); constructInfo = bullet.btRigidBody.btRigidBodyConstructionInfo( mass, motionState, shape, inertia); rigidBody = bullet.btRigidBody(constructInfo); self.bodies.append(PhysicsWorld.Body(shape, motionState, constructInfo, rigidBody)); self.world.addRigidBody(rigidBody); return rigidBody;
def __init__(self, app, pos, rot): self.app = app self.shape = bullet.btEmptyShape() _tr = bullet.btTransform() _tr.setIdentity() _tr.setOrigin(bullet.btVector3(pos.x, pos.y, pos.z)) _tr.setRotation(bullet.btQuaternion(rot.x, rot.y, rot.z, rot.w)) _local_inertia = bullet.btVector3(0,0,0) self.shape.calculateLocalInertia(self.MASS, _local_inertia) self.motion_state = bullet.btDefaultMotionState(_tr) self.rb_info = bullet.btRigidBody.btRigidBodyConstructionInfo( self.MASS, self.motion_state, self.shape, _local_inertia) self.body = bullet.btRigidBody(self.rb_info) self.body.setCollisionFlags(self.body.getCollisionFlags() \ | bullet.btCollisionObject.CF_KINEMATIC_OBJECT) self.app.world.getBulletDynamicsWorld().addRigidBody(self.body)
def createSphere(self, pos, radius, node): mass = 1.0 res = 0.1 bodyFriction = 0.8 fallInertia = bullet.btVector3(0, 0, 0) sphereId = len(self.bodies) sphereShape = bullet.btSphereShape(radius) sphereShape.calculateLocalInertia(mass, fallInertia) sphereMotionState = OgreMotionState(bullet.btTransform(bullet.btQuaternion(0, 0, 0, 1), bullet.btVector3(pos.x, pos.y, pos.z)), node) sphereBodyCI = bullet.btRigidBody.btRigidBodyConstructionInfo(mass, sphereMotionState, sphereShape, fallInertia) sphereBody = bullet.btRigidBody(sphereBodyCI) sphereBody.setActivationState(4) sphereBody.setCollisionFlags(bullet.btCollisionObject.CF_KINEMATIC_OBJECT) self.world.addRigidBody(sphereBody) self.motionStates.append(sphereMotionState) self.bodies.append(sphereBody) self.shapes.append(sphereShape) return None
def updateAction(self, deltaTime): transform = bullet.btTransform() pos = self.sceneNode.getPosition() transform.setOrigin(bullet.btVector3(pos.x, pos.y, pos.z)) self.ghostObject.setWorldTransform(transform) manifoldArray = bullet.btManifoldArray() pairArray = self.ghostObject.getOverlappingPairCache().getOverlappingPairArray() collision = False for i in range(pairArray.size()): manifoldArray.clear() pair = pairArray[i] collisionPair = self.world.getPairCache().findPair(pair.m_pProxy0, pair.m_pProxy1) if collisionPair is None: continue proxyObject = collisionPair.m_pProxy1.m_clientObject if proxyObject == self.target: continue if (collisionPair.m_algorithm): collisionPair.m_algorithm.getAllContactManifolds(manifoldArray) for j in manifoldArray: directionSign = None if j.getBody0() == self.ghostObject: directionSign = -1.0 else: directionSign = 1.0 for k in range(j.getNumContacts()): pt = j.getContactPoint(k) if pt.getDistance() < 0.0: collision = True if collision: self.entity.onCollision()
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 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 "1" construct = bullet.btRigidBody.btRigidBodyConstructionInfo(mass, motionState, shape, fallInertia) print "1" Object = bullet.btRigidBody(construct) # ...this should work in my eyes print dir(Object) print "1" world.addRigidBody(Object) print "1" for x in range(90): world.stepSimulation(x * 1 / 30) print "OBJECT:", print Object.getAabb(bullet.btVector3(0, 0, 0), bullet.btVector3(10, 10, 10)) print Object.getMotionState
def setOrientation(self, rot): _tr = bullet.btTransform() self.body.getMotionState().getWorldTransform(_tr) _tr.setRotation(bullet.btQuaternion(rot.x, rot.y, rot.z, rot.w)) self.body.getMotionState().setWorldTransform(_tr)
def setPosition(self, pos): _tr = bullet.btTransform() self.body.getMotionState().getWorldTransform(_tr) _tr.setOrigin(bullet.btVector3(pos.x, pos.y, pos.z)) self.body.getMotionState().setWorldTransform(_tr)
def _create_simple_tr(): _tr = bullet.btTransform() _tr.setIdentity() _tr.setOrigin(bullet.btVector3(0, 0, 0)) _tr.setRotation(bullet.btQuaternion(0, 0, 0, 1)) return _tr
def transform_zero(): return bullet.btTransform(bullet.btQuaternion(0, 0, 0, 0), bullet.btVector3(0, 0, 0))
def transform_from_ogre(v, q): return bullet.btTransform(ogre2bullet_quaternion(q), ogre2bullet_vector(v))
import sys sys.path.insert(0,'..') import PythonOgreConfig import ogre.renderer.OGRE as ogre import ogre.physics.bullet as bullet t = bullet.btTransform() ms = bullet.btDefaultMotionState (t) s = bullet.btBoxShape(bullet.btVector3(10,10,10)) body = bullet.btRigidBody(1, ms, s) print body collisionConfiguration = bullet.btDefaultCollisionConfiguration() dispatcher = bullet.btCollisionDispatcher (collisionConfiguration) worldAabbMin = bullet.btVector3(-1000,-1000,-1000) worldAabbMax = bullet.btVector3(1000,1000,1000) maxProxies = 32766 broadphase = bullet.btAxisSweep3(worldAabbMin, worldAabbMax, maxProxies) solver = bullet.btSequentialImpulseConstraintSolver() world = bullet.btDiscreteDynamicsWorld(dispatcher, broadphase , solver, collisionConfiguration) world.getDispatchInfo().m_enableSPU = True world.setGravity(bullet.btVector3(0,-10,0)) print world print dir(world) print solver