Esempio n. 1
0
def main():
    dynamicsWorld = DiscreteDynamicsWorld()

    groundShape = BoxShape(Vector3(50, 50, 50))
    groundTransform = Transform()
    groundTransform.setIdentity()
    groundTransform.setOrigin(Vector3(0, -56, 0))
    groundMotion = DefaultMotionState()
    groundMotion.setWorldTransform(groundTransform)
    ground = RigidBody(groundMotion, groundShape)
    dynamicsWorld.addRigidBody(ground)

    ballShape = BoxShape(Vector3(1, 1, 1))
    ballTransform = Transform()
    ballTransform.setIdentity()
    ballTransform.setOrigin(Vector3(2, 10, 0))
    ballMotion = DefaultMotionState()
    ballMotion.setWorldTransform(ballTransform)
    ball = RigidBody(ballMotion, ballShape, 1.0)
    dynamicsWorld.addRigidBody(ball)

    for i in range(100):
        dynamicsWorld.stepSimulation(1.0 / 60.0, 10)

        for obj in ballMotion, groundMotion:
            o = obj.getWorldTransform().getOrigin()
            print 'world pos = %0.6f,%0.6f,%0.6f' % (o.x, o.y, o.z)
Esempio n. 2
0
 def __init__(self):
     self.boxHalfExtents = Vector3(20, 2, 20)
     groundShape = BoxShape(self.boxHalfExtents)
     groundTransform = Transform()
     groundTransform.setIdentity()
     groundTransform.setOrigin(Vector3(0, -4, 0))
     groundMotion = DefaultMotionState()
     groundMotion.setWorldTransform(groundTransform)
     self.body = RigidBody(groundMotion, groundShape)
     self.body.setRestitution(0.5)
     self.motion = groundMotion
Esempio n. 3
0
 def __init__(self, position, color, radius=2):
     self.radius = radius
     ballShape = SphereShape(self.radius)
     ballTransform = Transform()
     ballTransform.setIdentity()
     ballTransform.setOrigin(position)
     ballMotion = DefaultMotionState()
     ballMotion.setWorldTransform(ballTransform)
     self.body = RigidBody(ballMotion, ballShape, 2.0)
     self.body.setRestitution(0.9)
     self.motion = ballMotion
     self.quad = gluNewQuadric()
     self.color = color
Esempio n. 4
0
    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