def setUp(self): self.step = 1.0/60 self.motion_state1 = bullet.btDefaultMotionState() self.motion_state2 = bullet.btDefaultMotionState() self.radius = 1.0 self.inertia = bullet.btVector3(1, 1, 1) self.sphere_shape = bullet.btSphereShape(self.radius) self.points = [ bullet.btVector3(-1, -1, -1), bullet.btVector3(-1, 1, -1), bullet.btVector3(-1, -1, 1), bullet.btVector3(-1, 1, 1), bullet.btVector3(1, 1, 1), bullet.btVector3(1, -1, 1), bullet.btVector3(1, -1, -1), bullet.btVector3(1, 1, -1) ] self.hull = bullet.btConvexHullShape(self.points) self.info = bullet.btRigidBodyConstructionInfo( 1.0, self.motion_state1, self.sphere_shape, self.inertia ) self.v1 = bullet.btVector3(0, 0, 0) self.v2 = bullet.btVector3(0, 0, 0) self.v3 = bullet.btVector3(0, 0, 0) self.t1 = bullet.btTransform.identity self.t2 = bullet.btTransform.identity self.body1 = bullet.btRigidBody(self.info) self.body2 = bullet.btRigidBody(1.0, self.motion_state2, self.hull, self.v1) self.m1 = bullet.btMatrix3x3.identity self.m2 = bullet.btMatrix3x3.identity
def setUp(self): self.step = 1.0 / 60 self.motion_state1 = bullet.btDefaultMotionState() self.motion_state2 = bullet.btDefaultMotionState() self.radius = 1.0 self.inertia = bullet.btVector3(1, 1, 1) self.sphere_shape = bullet.btSphereShape(self.radius) self.points = [ bullet.btVector3(-1, -1, -1), bullet.btVector3(-1, 1, -1), bullet.btVector3(-1, -1, 1), bullet.btVector3(-1, 1, 1), bullet.btVector3(1, 1, 1), bullet.btVector3(1, -1, 1), bullet.btVector3(1, -1, -1), bullet.btVector3(1, 1, -1) ] self.hull = bullet.btConvexHullShape(self.points) self.info = bullet.btRigidBodyConstructionInfo(1.0, self.motion_state1, self.sphere_shape, self.inertia) self.v1 = bullet.btVector3(0, 0, 0) self.v2 = bullet.btVector3(0, 0, 0) self.v3 = bullet.btVector3(0, 0, 0) self.t1 = bullet.btTransform.identity self.t2 = bullet.btTransform.identity self.body1 = bullet.btRigidBody(self.info) self.body2 = bullet.btRigidBody(1.0, self.motion_state2, self.hull, self.v1) self.m1 = bullet.btMatrix3x3.identity self.m2 = bullet.btMatrix3x3.identity
def setUp(self): self.o1 = bullet.btCollisionObject() self.shape = bullet.btSphereShape(1.0) self.bp = bullet.btDbvtBroadphase() self.v1 = bullet.btVector3(0, 0, 0) self.v2 = bullet.btVector3(0, 0, 0) self.v3 = bullet.btVector3(0, 0, 0) self.t1 = bullet.btTransform.identity self.t2 = bullet.btTransform.identity
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_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 __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 ();
import bullet if __name__=="__main__": broadphase = bullet.btDbvtBroadphase(); 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(
if __name__ == "__main__": broadphase = bullet.btDbvtBroadphase() 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)