def test_ctors(self): self.a = bullet.btTransform(bullet.btMatrix3x3(*self.v1)) self.b = bullet.btTransform(bullet.btMatrix3x3(*self.v1), bullet.btVector3(0, 0, 0)) self.c = bullet.btTransform(bullet.btQuaternion.identity) self.d = bullet.btTransform(bullet.btQuaternion.identity, bullet.btVector3(0, 0, 0)) self.assertEqual(self.a, self.b)
def test_origin(self): self.a = bullet.btTransform(self.q) self.b = bullet.btTransform(self.q) self.vec = self.a.get_origin() self.assertEqual(self.vec, self.a.get_origin()) self.assertEqual(self.a.get_origin(), bullet.btVector3(0, 0, 0)) self.a.origin.set_value(1, 0, 0) self.assertEqual(self.a.get_origin(), bullet.btVector3(1, 0, 0)) self.a.set_origin(bullet.btVector3(0, 1, 0)) self.assertEqual(self.a.get_origin(), bullet.btVector3(0, 1, 0))
def setUp(self): self.v1 = tuple(float(i) for i in range(0, 9)) self.vec = bullet.btVector3(0, 0, 0) self.q = bullet.btQuaternion.identity self.m = bullet.btMatrix3x3(*self.v1) self.a = bullet.btTransform() self.b = bullet.btTransform() self.c = bullet.btTransform() self.d = bullet.btTransform() self.e = bullet.btTransform()
def test_inverse(self): self.a = bullet.btTransform(self.q, bullet.btVector3(0, 0, 1)) self.b = bullet.btTransform(self.q, bullet.btVector3(0, 0, -1)) self.c = self.a.inverse() self.d = bullet.btTransform(bullet.btQuaternion.identity, bullet.btVector3(1, 0, 0)) self.assertEqual(self.b, self.c) self.vec = self.d.inv_xform(bullet.btVector3(0, 1, 0)) self.assertEqual(self.vec, bullet.btVector3(-1, 1, 0)) self.e = self.b.inverse_times(self.a) self.assertEqual(self.e, bullet.btTransform( bullet.btQuaternion.identity, bullet.btVector3(0, 0, 2)))
def shootBox(self, destination): if not self.m_dynamicsWorld: return print 'shootBox' mass = 1.0; startTransform=bullet.btTransform(); startTransform.setIdentity(); camPos = self.getCameraPosition(); startTransform.setOrigin(camPos); self.setShootBoxShape (); body = self.localCreateRigidBody(mass, startTransform, self.m_shootBoxShape); body.setLinearFactor((1,1,1)); linVel= vector3.normalize( (destination[0]-camPos[0],destination[1]-camPos[1],destination[2]-camPos[2])); linVel=vector3.mul(linVel, self.m_ShootBoxInitialSpeed); body.getWorldTransform().setOrigin(camPos); body.getWorldTransform().setRotation(bullet.btQuaternion(0,0,0,1)); body.setLinearVelocity(linVel); body.setAngularVelocity((0,0,0)); body.setCcdMotionThreshold(0.5); body.setCcdSweptSphereRadius(0.9);
def test_implemented(self): self.m = MyMotionState() t1 = bullet.btTransform(bullet.btQuaternion.identity, bullet.btVector3(0, 10, 0)) print('t1', t1) self.m.getWorldTransform(t1) self.assertEqual(t1, bullet.btTransform.identity)
def shootBox(self, destination): if not self.m_dynamicsWorld: return print 'shootBox' mass = 1.0 startTransform = bullet.btTransform() startTransform.setIdentity() camPos = self.getCameraPosition() startTransform.setOrigin(camPos) self.setShootBoxShape() body = self.localCreateRigidBody(mass, startTransform, self.m_shootBoxShape) body.setLinearFactor((1, 1, 1)) linVel = vector3.normalize( (destination[0] - camPos[0], destination[1] - camPos[1], destination[2] - camPos[2])) linVel = vector3.mul(linVel, self.m_ShootBoxInitialSpeed) body.getWorldTransform().setOrigin(camPos) body.getWorldTransform().setRotation(bullet.btQuaternion(0, 0, 0, 1)) body.setLinearVelocity(linVel) body.setAngularVelocity((0, 0, 0)) body.setCcdMotionThreshold(0.5) body.setCcdSweptSphereRadius(0.9)
def test_basis(self): self.a = bullet.btTransform(self.m) self.assertEqual(self.a.get_basis(), self.m) w = (4, 5, 6) self.a.basis[0] = bullet.btVector3(*w) self.assertFalse(self.a.get_basis() == self.m) self.a.set_basis(self.m) self.assertEqual(self.a.get_basis(), self.m)
def initGroundShape(self): # create a few basic rigid bodies groundShape = bullet.btBoxShape((50.0, 50.0, 50.0)) groundTransform = bullet.btTransform() groundTransform.setIdentity() groundTransform.setOrigin((0, -50, 0)) self.createAndAppendBody(groundShape, 0.0, groundTransform)
def initGroundShape(self): # create a few basic rigid bodies groundShape = bullet.btBoxShape( (50.0, 50.0, 50.0)); groundTransform=bullet.btTransform(); groundTransform.setIdentity(); groundTransform.setOrigin((0,-50,0)); self.createAndAppendBody(groundShape, 0.0, groundTransform)
def test_rotation(self): self.a = bullet.btTransform(self.q) self.assertEqual(self.a.get_rotation(), self.q) self.q = bullet.btQuaternion(0, 1, 1, 0) self.assertNotEqual(self.q, self.a.get_rotation()) self.a.set_rotation(self.q) print(self.q.normalized(), self.a.get_rotation()) # Hack to bypass numeric imprecision # TODO: Extend test case to implement assertAlmostEqual with matrices self.assertTrue(str(self.q.normalized()) == str(self.a.get_rotation()))
def test_identit(self): self.a = bullet.btTransform.identity self.b = bullet.btTransform(bullet.btQuaternion(1, 0, 0, 0), bullet.btVector3(0, 1, 0)) self.assertEqual(self.a.get_rotation(), bullet.btQuaternion.identity) self.assertEqual(self.a.get_origin(), self.vec) print(self.b.get_origin(), self.vec) self.assertFalse(self.b.get_origin() == self.vec) self.b.set_identity() self.assertEqual(self.a, self.b) self.assertEqual(bullet.btTransform.identity, self.a)
def test_add_ground(self): self.ground_shape = bullet.btStaticPlaneShape( bullet.btVector3(0, 1, 0), 1) self.ground_motion_state = bullet.btDefaultMotionState( bullet.btTransform(bullet.btQuaternion.identity, bullet.btVector3(0, -1, 0))) self.ground_rigid_body_cinfo = bullet.btRigidBodyConstructionInfo( 0, self.ground_motion_state, self.ground_shape, bullet.btVector3(0, 0, 0)) self.ground_rigid_body = \ bullet.btRigidBody(self.ground_rigid_body_cinfo) self.dynamics_world.add_rigid_body(self.ground_rigid_body)
def test_add_sphere(self, pos=bullet.btVector3(0, 0, 0)): self.sphere_shape = bullet.btSphereShape(1) self.sphere_motion_state = bullet.btDefaultMotionState( bullet.btTransform(bullet.btQuaternion.identity, pos)) self.sphere_mass = 1.0 self.fall_inertia = bullet.btVector3(0, 0, 0) self.sphere_shape.calculate_local_inertia(self.sphere_mass, self.fall_inertia) self.sphere_rigid_body_cinfo = bullet.btRigidBodyConstructionInfo( self.sphere_mass, self.sphere_motion_state, self.sphere_shape, self.fall_inertia) self.sphere_rigid_body = \ bullet.btRigidBody(self.sphere_rigid_body_cinfo) self.dynamics_world.add_rigid_body(self.sphere_rigid_body)
def test_add_ground(self): self.ground_shape = bullet.btStaticPlaneShape( bullet.btVector3(0, 1, 0), 1 ) self.ground_motion_state = bullet.btDefaultMotionState( bullet.btTransform(bullet.btQuaternion.identity, bullet.btVector3(0, -1, 0)) ) self.ground_rigid_body_cinfo = bullet.btRigidBodyConstructionInfo( 0, self.ground_motion_state, self.ground_shape, bullet.btVector3(0, 0, 0) ) self.ground_rigid_body = \ bullet.btRigidBody(self.ground_rigid_body_cinfo) self.dynamics_world.add_rigid_body(self.ground_rigid_body)
def test_add_sphere(self, pos=bullet.btVector3(0, 0, 0)): self.sphere_shape = bullet.btSphereShape(1) self.sphere_motion_state = bullet.btDefaultMotionState( bullet.btTransform(bullet.btQuaternion.identity, pos) ) self.sphere_mass = 1.0 self.fall_inertia = bullet.btVector3(0, 0, 0) self.sphere_shape.calculate_local_inertia(self.sphere_mass, self.fall_inertia) self.sphere_rigid_body_cinfo = bullet.btRigidBodyConstructionInfo( self.sphere_mass, self.sphere_motion_state, self.sphere_shape, self.fall_inertia ) self.sphere_rigid_body = \ bullet.btRigidBody(self.sphere_rigid_body_cinfo) self.dynamics_world.add_rigid_body(self.sphere_rigid_body)
def initRigidBodies(self): # create a few dynamic rigidbodies # Re-using the same collision is better for memory usage and performance colShape = bullet.btBoxShape((SCALING * 1, SCALING * 1, SCALING * 1)) # Create Dynamic Objects startTransform = bullet.btTransform() startTransform.setIdentity() start_x = START_POS_X - ARRAY_SIZE_X / 2 start_y = START_POS_Y start_z = START_POS_Z - ARRAY_SIZE_Z / 2 for k in range(ARRAY_SIZE_Y): for i in range(ARRAY_SIZE_X): for j in range(ARRAY_SIZE_Z): startTransform.setOrigin( (SCALING * (2.0 * i + start_x), SCALING * (20 + 2.0 * k + start_y), SCALING * (2.0 * j + start_z))) self.createAndAppendBody(colShape, 1.0, startTransform)
def initRigidBodies(self): # create a few dynamic rigidbodies # Re-using the same collision is better for memory usage and performance colShape = bullet.btBoxShape((SCALING*1,SCALING*1,SCALING*1)); # Create Dynamic Objects startTransform=bullet.btTransform(); startTransform.setIdentity(); start_x = START_POS_X - ARRAY_SIZE_X/2; start_y = START_POS_Y; start_z = START_POS_Z - ARRAY_SIZE_Z/2; for k in range(ARRAY_SIZE_Y): for i in range(ARRAY_SIZE_X): for j in range(ARRAY_SIZE_Z): startTransform.setOrigin(( SCALING* (2.0*i + start_x), SCALING* (20+2.0*k + start_y), SCALING* (2.0*j + start_z) )); self.createAndAppendBody(colShape, 1.0, startTransform)
def mouseFunc(self, button, state, x, y): print('mouseFunc', button, state, x, y) if (state == 0): self.m_mouseButtons |= 1<<button; else: self.m_mouseButtons = 0; self.m_mouseOldX = x; self.m_mouseOldY = y; self.updateModifierKeys(); if ((self.m_alt_key) and (state==0)): return; rayTo = self.getRayTo(x,y); print 'rayTo', rayTo if button==2: if (state==0): self.shootBox(rayTo); elif button== 1: if (state==0): pass elif button== 0: if (state==0): # add a point to point constraint for picking if not self.m_dynamicsWorld: self.removePickingConstraint(); else: print 'picking' if (self.m_ortho): rayFrom = (rayTo[0], rayTo[1], -100.0); else: rayFrom = self.m_cameraPosition; rayCallback=bullet.ClosestRayResultCallback(rayFrom, rayTo); self.m_dynamicsWorld.rayTest(rayFrom, rayTo, rayCallback); if (rayCallback.hasHit()): body = bullet.btRigidBody.upcast(rayCallback.m_collisionObject); if (body): print 'hit' # other exclusions? if (not (body.isStaticObject() or body.isKinematicObject())): print body self.pickedBody = body; self.pickedBody.setActivationState(bullet.DISABLE_DEACTIVATION); #pickPos = rayCallback.m_hitPointWorld; pickPos = rayCallback.m_hitPointWorld # printf("pickPos=%f,%f,%f\n",pickPos.getX(),pickPos.getY(),pickPos.getZ()); localPivot = body.getCenterOfMassTransform().inverse().apply(pickPos); self.m_keep.append(self.m_pickConstraint) if (self.m_use6Dof): print 'm_use6Dof' tr=bullet.btTransform(); tr.setIdentity(); tr.setOrigin(localPivot); dof6 = bullet.btGeneric6DofConstraint(body, tr, False); dof6.setLinearLowerLimit((0,0,0)); dof6.setLinearUpperLimit((0,0,0)); dof6.setAngularLowerLimit((0,0,0)); dof6.setAngularUpperLimit((0,0,0)); self.m_dynamicsWorld.addConstraint(dof6); self.m_pickConstraint = dof6; dof6.setParam(bullet.BT_CONSTRAINT_STOP_CFM,0.8,0); dof6.setParam(bullet.BT_CONSTRAINT_STOP_CFM,0.8,1); dof6.setParam(bullet.BT_CONSTRAINT_STOP_CFM,0.8,2); dof6.setParam(bullet.BT_CONSTRAINT_STOP_CFM,0.8,3); dof6.setParam(bullet.BT_CONSTRAINT_STOP_CFM,0.8,4); dof6.setParam(bullet.BT_CONSTRAINT_STOP_CFM,0.8,5); dof6.setParam(bullet.BT_CONSTRAINT_STOP_ERP,0.1,0); dof6.setParam(bullet.BT_CONSTRAINT_STOP_ERP,0.1,1); dof6.setParam(bullet.BT_CONSTRAINT_STOP_ERP,0.1,2); dof6.setParam(bullet.BT_CONSTRAINT_STOP_ERP,0.1,3); dof6.setParam(bullet.BT_CONSTRAINT_STOP_ERP,0.1,4); dof6.setParam(bullet.BT_CONSTRAINT_STOP_ERP,0.1,5); else: print 'not m_use6Dof' p2p = bullet.btPoint2PointConstraint(body,localPivot); self.m_dynamicsWorld.addConstraint(p2p); self.m_pickConstraint = p2p; p2p.m_setting.m_impulseClamp = 30.0; # very weak constraint for picking p2p.m_setting.m_tau = 0.001; self.m_use6Dof = not self.m_use6Dof; # save mouse position for dragging self.gOldPickingPos = rayTo; self.gHitPos = pickPos; self.m_oldPickingDist = vector3.length( vector3.sub(pickPos, rayFrom)); print 'end'
def test_virtual(self): self.m = bullet.btMotionState() t1 = bullet.btTransform() self.assertRaises(RuntimeError, self.m.getWorldTransform, t1)
def test_start_world_transform(self): self.t2.origin = bullet.btVector3(10, 0, 0) self.t1 = bullet.btTransform(self.t2) self.start_world_transform = self.t2 self.assertEquals(self.t1, self.t2)
def mouseFunc(self, button, state, x, y): print('mouseFunc', button, state, x, y) if (state == 0): self.m_mouseButtons |= 1 << button else: self.m_mouseButtons = 0 self.m_mouseOldX = x self.m_mouseOldY = y self.updateModifierKeys() if ((self.m_alt_key) and (state == 0)): return rayTo = self.getRayTo(x, y) print 'rayTo', rayTo if button == 2: if (state == 0): self.shootBox(rayTo) elif button == 1: if (state == 0): pass elif button == 0: if (state == 0): # add a point to point constraint for picking if not self.m_dynamicsWorld: self.removePickingConstraint() else: print 'picking' if (self.m_ortho): rayFrom = (rayTo[0], rayTo[1], -100.0) else: rayFrom = self.m_cameraPosition rayCallback = bullet.ClosestRayResultCallback( rayFrom, rayTo) self.m_dynamicsWorld.rayTest(rayFrom, rayTo, rayCallback) if (rayCallback.hasHit()): body = bullet.btRigidBody.upcast( rayCallback.m_collisionObject) if (body): print 'hit' # other exclusions? if (not (body.isStaticObject() or body.isKinematicObject())): print body self.pickedBody = body self.pickedBody.setActivationState( bullet.DISABLE_DEACTIVATION) #pickPos = rayCallback.m_hitPointWorld; pickPos = rayCallback.m_hitPointWorld # printf("pickPos=%f,%f,%f\n",pickPos.getX(),pickPos.getY(),pickPos.getZ()); localPivot = body.getCenterOfMassTransform( ).inverse().apply(pickPos) self.m_keep.append(self.m_pickConstraint) if (self.m_use6Dof): print 'm_use6Dof' tr = bullet.btTransform() tr.setIdentity() tr.setOrigin(localPivot) dof6 = bullet.btGeneric6DofConstraint( body, tr, False) dof6.setLinearLowerLimit((0, 0, 0)) dof6.setLinearUpperLimit((0, 0, 0)) dof6.setAngularLowerLimit((0, 0, 0)) dof6.setAngularUpperLimit((0, 0, 0)) self.m_dynamicsWorld.addConstraint(dof6) self.m_pickConstraint = dof6 dof6.setParam( bullet.BT_CONSTRAINT_STOP_CFM, 0.8, 0) dof6.setParam( bullet.BT_CONSTRAINT_STOP_CFM, 0.8, 1) dof6.setParam( bullet.BT_CONSTRAINT_STOP_CFM, 0.8, 2) dof6.setParam( bullet.BT_CONSTRAINT_STOP_CFM, 0.8, 3) dof6.setParam( bullet.BT_CONSTRAINT_STOP_CFM, 0.8, 4) dof6.setParam( bullet.BT_CONSTRAINT_STOP_CFM, 0.8, 5) dof6.setParam( bullet.BT_CONSTRAINT_STOP_ERP, 0.1, 0) dof6.setParam( bullet.BT_CONSTRAINT_STOP_ERP, 0.1, 1) dof6.setParam( bullet.BT_CONSTRAINT_STOP_ERP, 0.1, 2) dof6.setParam( bullet.BT_CONSTRAINT_STOP_ERP, 0.1, 3) dof6.setParam( bullet.BT_CONSTRAINT_STOP_ERP, 0.1, 4) dof6.setParam( bullet.BT_CONSTRAINT_STOP_ERP, 0.1, 5) else: print 'not m_use6Dof' p2p = bullet.btPoint2PointConstraint( body, localPivot) self.m_dynamicsWorld.addConstraint(p2p) self.m_pickConstraint = p2p p2p.m_setting.m_impulseClamp = 30.0 # very weak constraint for picking p2p.m_setting.m_tau = 0.001 self.m_use6Dof = not self.m_use6Dof # save mouse position for dragging self.gOldPickingPos = rayTo self.gHitPos = pickPos self.m_oldPickingDist = vector3.length( vector3.sub(pickPos, rayFrom)) print 'end'
collisionConfiguration = bullet.btDefaultCollisionConfiguration(); dispatcher = bullet.btCollisionDispatcher(collisionConfiguration); solver = bullet.btSequentialImpulseConstraintSolver(); dynamicsWorld = bullet.btDiscreteDynamicsWorld( dispatcher, broadphase, solver, collisionConfiguration); dynamicsWorld.setGravity((0,-10,0)); groundShape = bullet.btStaticPlaneShape((0,1,0), 1); fallShape = bullet.btSphereShape(1); groundMotionState = bullet.btDefaultMotionState( bullet.btTransform( bullet.btQuaternion(0,0,0,1), (0,-1,0) ) ); groundRigidBodyCI=bullet.btRigidBodyConstructionInfo(0, groundMotionState, groundShape, (0,0,0)); groundRigidBody = bullet.btRigidBody(groundRigidBodyCI); dynamicsWorld.addRigidBody(groundRigidBody); fallMotionState = bullet.btDefaultMotionState( bullet.btTransform( bullet.btQuaternion(0,0,0,1), (0,50,0) ) );
def test_mult(self): self.a = bullet.btTransform(self.q) self.b = bullet.btTransform(bullet.btQuaternion(1, 1, 0, 0)) self.c = bullet.btTransform(self.q) self.c.mult(self.a, self.b) self.assertFalse(self.c == self.a)
def test_str(self): self.a = bullet.btTransform(self.q) self.b = bullet.btTransform(self.q) self.assertEqual(self.a, self.b)
def __drawShape(self, shape): # you can comment out any of the specific cases, and use the default # the benefit of 'default' is that it approximates the actual # collision shape including collision margin # int shapetype=m_textureenabled?MAX_BROADPHASE_COLLISION_TYPES:shape.getShapeType(); shapetype=shape.getShapeType(); if shapetype== bullet.SPHERE_SHAPE_PROXYTYPE: print 'SPHERE_SHAPE_PROXYTYPE' sphereShape = bullet.btSphereShape.downcast(shape); # radius doesn't include the margin, so draw with margin radius = sphereShape.getMargin(); drawSphere(radius,10,10); useWireframeFallback = False; elif shapetype== bullet.BOX_SHAPE_PROXYTYPE: boxShape = bullet.btBoxShape.downcast(shape); halfExtent = boxShape.getHalfExtentsWithMargin(); glPushMatrix() glScalef(halfExtent[0], halfExtent[1], halfExtent[2]) gBox.draw() glPopMatrix() useWireframeFallback = False; elif shapetype== bullet.STATIC_PLANE_PROXYTYPE: print 'STATIC_PLANE_PROXYTYPE' staticPlaneShape = bullet.btStaticPlaneShape.downcast(shape); planeConst = staticPlaneShape.getPlaneConstant(); planeNormal = staticPlaneShape.getPlaneNormal(); planeOrigin = planeNormal * planeConst; vec0, vec1=btPlaneSpace1(planeNormal); vecLen = 100.0; pt0 = planeOrigin + vec0*vecLen; pt1 = planeOrigin - vec0*vecLen; pt2 = planeOrigin + vec1*vecLen; pt3 = planeOrigin - vec1*vecLen; glBegin(GL_LINES); glVertex3f(pt0[0], pt0[1], pt0[2]); glVertex3f(pt1[0], pt1[1], pt1[2]); glVertex3f(pt2[0], pt2[1], pt2[2]); glVertex3f(pt3[0], pt3[1], pt3[2]); glEnd(); elif shapetype== bullet.MULTI_SPHERE_SHAPE_PROXYTYPE: print 'MULTI_SPHERE_SHAPE_PROXYTYPE' multiSphereShape = bullet.btMultiSphereShape.downcast(shape); childTransform=bullet.btTransform(); childTransform.setIdentity(); for i in range(multiSphereShape.getSphereCount()-1, -1, -1): sc=bullet.btSphereShape (multiSphereShape.getSphereRadius(i)); childTransform.setOrigin(multiSphereShape.getSpherePosition(i)); childMat=childTransform.getOpenGLMatrix(); self.drawOpenGL(childMat,sc,color,debugMode,worldBoundsMin,worldBoundsMax); else: print 'other' if (shape.isConvex()): poly = (shape.isPolyhedral() and bullet.btPolyhedralConvexShape.downcast(shape).getConvexPolyhedron() or 0); if (poly): glBegin (GL_TRIANGLES); for i in range(poly.m_faces.size()): centroid=(0,0,0); numVerts = poly.m_faces[i].m_indices.size(); if (numVerts>2): v1 = poly.m_vertices[poly.m_faces[i].m_indices[0]]; for v in range(poly.m_faces[i].m_indices.size()-2): v2 = poly.m_vertices[poly.m_faces[i].m_indices[v+1]]; v3 = poly.m_vertices[poly.m_faces[i].m_indices[v+2]]; normal = vector3.normalize(vector3.cross( vector3.sub(v3, v1), vector3.sub(v2, v1) )); glNormal3f(normal.getX(),normal.getY(),normal.getZ()); glVertex3f (v1[0], v1[1], v1[2]); glVertex3f (v2[0], v2[1], v2[2]); glVertex3f (v3[0], v3[1], v3[2]); glEnd (); else: sc=self.cache(bullet.btConvexShape.downcast(shape)); # glutSolidCube(1.0); hull = sc.m_shapehull if (hull.numTriangles () > 0): index = 0; idx = hull.getIndexPointer(); vtx = hull.getVertexPointer(); glBegin (GL_TRIANGLES); for i in range(hull.numTriangles()): i1 = index; i2 = index+1; i3 = index+2; index+=3 assert(i1 < hull.numIndices () and i2 < hull.numIndices () and i3 < hull.numIndices ()); index1 = idx[i1]; index2 = idx[i2]; index3 = idx[i3]; assert(index1 < hull.numVertices () and index2 < hull.numVertices () and index3 < hull.numVertices ()); v1 = vtx[index1]; v2 = vtx[index2]; v3 = vtx[index3]; normal = (v3-v1).cross(v2-v1); normal.normalize (); glNormal3f(normal.getX(),normal.getY(),normal.getZ()); glVertex3f (v1.x(), v1.y(), v1.z()); glVertex3f (v2.x(), v2.y(), v2.z()); glVertex3f (v3.x(), v3.y(), v3.z()); glEnd ();
def __drawShape(self, shape): # you can comment out any of the specific cases, and use the default # the benefit of 'default' is that it approximates the actual # collision shape including collision margin # int shapetype=m_textureenabled?MAX_BROADPHASE_COLLISION_TYPES:shape.getShapeType(); shapetype = shape.getShapeType() if shapetype == bullet.SPHERE_SHAPE_PROXYTYPE: print 'SPHERE_SHAPE_PROXYTYPE' sphereShape = bullet.btSphereShape.downcast(shape) # radius doesn't include the margin, so draw with margin radius = sphereShape.getMargin() drawSphere(radius, 10, 10) useWireframeFallback = False elif shapetype == bullet.BOX_SHAPE_PROXYTYPE: boxShape = bullet.btBoxShape.downcast(shape) halfExtent = boxShape.getHalfExtentsWithMargin() glPushMatrix() glScalef(halfExtent[0], halfExtent[1], halfExtent[2]) gBox.draw() glPopMatrix() useWireframeFallback = False elif shapetype == bullet.STATIC_PLANE_PROXYTYPE: print 'STATIC_PLANE_PROXYTYPE' staticPlaneShape = bullet.btStaticPlaneShape.downcast(shape) planeConst = staticPlaneShape.getPlaneConstant() planeNormal = staticPlaneShape.getPlaneNormal() planeOrigin = planeNormal * planeConst vec0, vec1 = btPlaneSpace1(planeNormal) vecLen = 100.0 pt0 = planeOrigin + vec0 * vecLen pt1 = planeOrigin - vec0 * vecLen pt2 = planeOrigin + vec1 * vecLen pt3 = planeOrigin - vec1 * vecLen glBegin(GL_LINES) glVertex3f(pt0[0], pt0[1], pt0[2]) glVertex3f(pt1[0], pt1[1], pt1[2]) glVertex3f(pt2[0], pt2[1], pt2[2]) glVertex3f(pt3[0], pt3[1], pt3[2]) glEnd() elif shapetype == bullet.MULTI_SPHERE_SHAPE_PROXYTYPE: print 'MULTI_SPHERE_SHAPE_PROXYTYPE' multiSphereShape = bullet.btMultiSphereShape.downcast(shape) childTransform = bullet.btTransform() childTransform.setIdentity() for i in range(multiSphereShape.getSphereCount() - 1, -1, -1): sc = bullet.btSphereShape(multiSphereShape.getSphereRadius(i)) childTransform.setOrigin(multiSphereShape.getSpherePosition(i)) childMat = childTransform.getOpenGLMatrix() self.drawOpenGL(childMat, sc, color, debugMode, worldBoundsMin, worldBoundsMax) else: print 'other' if (shape.isConvex()): poly = (shape.isPolyhedral() and bullet.btPolyhedralConvexShape .downcast(shape).getConvexPolyhedron() or 0) if (poly): glBegin(GL_TRIANGLES) for i in range(poly.m_faces.size()): centroid = (0, 0, 0) numVerts = poly.m_faces[i].m_indices.size() if (numVerts > 2): v1 = poly.m_vertices[poly.m_faces[i].m_indices[0]] for v in range(poly.m_faces[i].m_indices.size() - 2): v2 = poly.m_vertices[poly.m_faces[i].m_indices[ v + 1]] v3 = poly.m_vertices[poly.m_faces[i].m_indices[ v + 2]] normal = vector3.normalize( vector3.cross(vector3.sub(v3, v1), vector3.sub(v2, v1))) glNormal3f(normal.getX(), normal.getY(), normal.getZ()) glVertex3f(v1[0], v1[1], v1[2]) glVertex3f(v2[0], v2[1], v2[2]) glVertex3f(v3[0], v3[1], v3[2]) glEnd() else: sc = self.cache(bullet.btConvexShape.downcast(shape)) # glutSolidCube(1.0); hull = sc.m_shapehull if (hull.numTriangles() > 0): index = 0 idx = hull.getIndexPointer() vtx = hull.getVertexPointer() glBegin(GL_TRIANGLES) for i in range(hull.numTriangles()): i1 = index i2 = index + 1 i3 = index + 2 index += 3 assert (i1 < hull.numIndices() and i2 < hull.numIndices() and i3 < hull.numIndices()) index1 = idx[i1] index2 = idx[i2] index3 = idx[i3] assert (index1 < hull.numVertices() and index2 < hull.numVertices() and index3 < hull.numVertices()) v1 = vtx[index1] v2 = vtx[index2] v3 = vtx[index3] normal = (v3 - v1).cross(v2 - v1) normal.normalize() glNormal3f(normal.getX(), normal.getY(), normal.getZ()) glVertex3f(v1.x(), v1.y(), v1.z()) glVertex3f(v2.x(), v2.y(), v2.z()) glVertex3f(v3.x(), v3.y(), v3.z()) glEnd()
collisionConfiguration = bullet.btDefaultCollisionConfiguration() dispatcher = bullet.btCollisionDispatcher(collisionConfiguration) solver = bullet.btSequentialImpulseConstraintSolver() dynamicsWorld = bullet.btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration) dynamicsWorld.setGravity((0, -10, 0)) groundShape = bullet.btStaticPlaneShape((0, 1, 0), 1) fallShape = bullet.btSphereShape(1) groundMotionState = bullet.btDefaultMotionState( bullet.btTransform(bullet.btQuaternion(0, 0, 0, 1), (0, -1, 0))) groundRigidBodyCI = bullet.btRigidBodyConstructionInfo( 0, groundMotionState, groundShape, (0, 0, 0)) groundRigidBody = bullet.btRigidBody(groundRigidBodyCI) dynamicsWorld.addRigidBody(groundRigidBody) fallMotionState = bullet.btDefaultMotionState( bullet.btTransform(bullet.btQuaternion(0, 0, 0, 1), (0, 50, 0))) mass = 1 fallInertia = (0, 0, 0) fallShape.calculateLocalInertia(mass, fallInertia) fallRigidBodyCI = bullet.btRigidBodyConstructionInfo( mass, fallMotionState, fallShape, fallInertia) fallRigidBody = bullet.btRigidBody(fallRigidBodyCI)