Example #1
0
    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 ();
Example #2
0
import swigbullet as 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(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)