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 createGround(self): if not self.world: return groundShape = bullet.btBoxShape((50.0, 50.0, 50.0)); groundTransform=bullet.btTransform(); groundTransform.setIdentity(); groundTransform.setOrigin((0,-50,0)); self.world.localCreateRigidBody(0.0, groundTransform, groundShape)
def shootBox(self, world, camPos, destination): startTransform=bullet.btTransform() startTransform.setIdentity() body = world.localCreateRigidBody(1.0, startTransform, self.m_shootBoxShape) body.setLinearFactor((1.0,1.0,1.0)) body.getWorldTransform().setOrigin(camPos); body.getWorldTransform().setRotation(bullet.btQuaternion(0,0,0,1)); linVel=Vector3( destination[0]-camPos[0], destination[1]-camPos[1], destination[2]-camPos[2]).normalize()*self.m_ShootBoxInitialSpeed body.setLinearVelocity(linVel) body.setAngularVelocity((0.0, 0.0, 0.0)) body.setCcdMotionThreshold(0.5) body.setCcdSweptSphereRadius(0.9)
def createCubes(self): if not self.world: return colShape = bullet.btBoxShape((SCALING*1,SCALING*1,SCALING*1)); 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( Vector3( (2.0*i + start_x), (20+2.0*k + start_y), (2.0*j + start_z) )* SCALING); self.world.localCreateRigidBody(1.0, startTransform, colShape)
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 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'
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) fallRigidBodyCI = bullet.btRigidBodyConstructionInfo(mass, fallMotionState, fallShape, fallInertia) fallRigidBody = bullet.btRigidBody(fallRigidBodyCI) dynamicsWorld.addRigidBody(fallRigidBody) for i in range(300):