def __init__(self, filename): obj = OBJ(filename) # The number of materials should be one assert len(obj.mfaces) is 1 material, tfaces = obj.mfaces[0] # The number of meshes should be one assert len(tfaces) is 1 desc, faces = tfaces[0] # The number of faces should be three assert desc[0] is 3 # Decouple tuples vertices = [] for v in obj.vertices: vertices.extend(v) # Note that face/quads/polys indices in Wavefront OBJ # starts from 1 and therefore must be decremented in order # to properly work with Bullet indices = [i-1 for i in faces[0]] # Build numpy arrays self.vertices = array(vertices, 'f') self.indices = array(indices, 'i') # Create mesh self.mesh = IndexedMesh() self.mesh.setVertices(self.vertices.size / 3, 3 * self.vertices.itemsize, self.vertices) self.mesh.setIndices(self.indices.size / 3, 3 * self.indices.itemsize, self.indices)
def __init__(self): # Vertices are coordinates in three-space. These four will define two # triangles, with two vertices shared between them. self.groundVertices = array([ 0, 0, 0, 10, 0, 0, 10, 0, 10, 0, 0, 10], 'f') # These indices are used to find vertices in the groundVertices array. # They index a vertex, not an individual float. So a value of 1 refers # to the 2nd triple in groundVertices (ie 10, 0, 0). Three indices # define one triangle, so this array defines two triangles. self.groundIndices = array( [0, 1, 2, 2, 3, 0], 'i') # Create a single triangle mesh to use as part of the array of triangle # meshes that will define the shape of the ground object. groundMesh = IndexedMesh() # Set the indices of this mesh. Specify the number of triangles it # will define (2), the stride in bytes between the beginning of index # triples, and the array actually containing the data. Since this # index data is tightly packed, the stride is just the size of a single # index times the number of indices per triangle (3). groundMesh.setIndices( 2, 3 * self.groundIndices.itemsize, self.groundIndices) # Set the vertex data of this mesh. Specify the number of vertices it # will define (4), the stride in bytes between the beginning of vertex # triples, and the array actually containing the data. Since the # vertex data is tightly packed, the stride is just the size of a # single vertex component (ie x, y, or z value) times the number of # components per vertex (3). groundMesh.setVertices( 4, 3 * self.groundVertices.itemsize, self.groundVertices) # Create a container for the single IndexedMesh, but we could add more # to this if we had any. groundStuff = TriangleIndexVertexArray() groundStuff.addIndexedMesh(groundMesh) # Create a shape based on that array of IndexedMesh instances. groundShape = BvhTriangleMeshShape(groundStuff) # Since the data is completely specified at this point, tell the shape # to build its optimized internal data structure. XXX This is somewhat # bad, and forgetting to do it will cause problems like segfaults. It # would be nice if it were not required. groundShape.buildOptimizedBvh() # Position the ground at an offset so that it's actually centered at # the origin (we could also have used -5s and 5s in the vertex data, # instead of 0s and 10s). groundTransform = Transform() groundTransform.setIdentity() groundTransform.setOrigin(Vector3(-5, -5, -5)) # Install a motion state object which we can inspect later to find out # if the ground has moved (it won't, though). groundMotion = DefaultMotionState() groundMotion.setWorldTransform(groundTransform) # Create the actual collision object using the motion state and the # shape. self.body = RigidBody(groundMotion, groundShape) self.motion = groundMotion
def __init__(self): # Vertices are coordinates in three-space. These four will define two # triangles, with two vertices shared between them. self.groundVertices = array([0, 0, 0, 10, 0, 0, 10, 0, 10, 0, 0, 10], 'f') # These indices are used to find vertices in the groundVertices array. # They index a vertex, not an individual float. So a value of 1 refers # to the 2nd triple in groundVertices (ie 10, 0, 0). Three indices # define one triangle, so this array defines two triangles. self.groundIndices = array([0, 1, 2, 2, 3, 0], 'i') # Create a single triangle mesh to use as part of the array of triangle # meshes that will define the shape of the ground object. groundMesh = IndexedMesh() # Set the indices of this mesh. Specify the number of triangles it # will define (2), the stride in bytes between the beginning of index # triples, and the array actually containing the data. Since this # index data is tightly packed, the stride is just the size of a single # index times the number of indices per triangle (3). groundMesh.setIndices(2, 3 * self.groundIndices.itemsize, self.groundIndices) # Set the vertex data of this mesh. Specify the number of vertices it # will define (4), the stride in bytes between the beginning of vertex # triples, and the array actually containing the data. Since the # vertex data is tightly packed, the stride is just the size of a # single vertex component (ie x, y, or z value) times the number of # components per vertex (3). groundMesh.setVertices(4, 3 * self.groundVertices.itemsize, self.groundVertices) # Create a container for the single IndexedMesh, but we could add more # to this if we had any. groundStuff = TriangleIndexVertexArray() groundStuff.addIndexedMesh(groundMesh) # Create a shape based on that array of IndexedMesh instances. groundShape = BvhTriangleMeshShape(groundStuff) # Since the data is completely specified at this point, tell the shape # to build its optimized internal data structure. XXX This is somewhat # bad, and forgetting to do it will cause problems like segfaults. It # would be nice if it were not required. groundShape.buildOptimizedBvh() # Position the ground at an offset so that it's actually centered at # the origin (we could also have used -5s and 5s in the vertex data, # instead of 0s and 10s). groundTransform = Transform() groundTransform.setIdentity() groundTransform.setOrigin(Vector3(-5, -5, -5)) # Install a motion state object which we can inspect later to find out # if the ground has moved (it won't, though). groundMotion = DefaultMotionState() groundMotion.setWorldTransform(groundTransform) # Create the actual collision object using the motion state and the # shape. self.body = RigidBody(groundMotion, groundShape) self.motion = groundMotion