Beispiel #1
0
    def __init__(self, filename, view, mass, position=eVector3()):
        super(CueBall, self).__init__(filename, view)
        self.radius = self.model.dimension.z / 2.0
        self.mass = mass
        # Update z pos from radius
        position.z = self.radius

        # Init Transform
        transform = Transform()
        self.position = position
        self.do_transformation_matrix()
        self.mnumpy = array(self.m, 'f')
        transform.setFromOpenGLMatrix(self.mnumpy)

        # The Rigid Body
        mass = self.mass * SCALE_FACTOR
        shape = SphereShape(self.radius)
        i = shape.getLocalInertia(mass) * 0.65
        self.motion = DefaultMotionState()
        self.motion.setWorldTransform(transform)
        self.body = RigidBody.fromConstructionInfo(self.motion,         #  MotionState motion
                                                   shape,               #  CollisionShape shape
                                                   mass,                	#  btScalar mass
                                                   bVector3(i.x, i.y, i.z), #  Vector3 inertia
                                                   transform,           #  Transform worldTransform
                                                   0.2,              	#  btScalar linearDamping
                                                   0.65,              	#  btScalar angularDamping
                                                   0.25,                #  btScalar friction
                                                   0.7,          		#  btScalar restitution
                                                   1.5,                 #  btScalar linearSleepingThreshold
                                                   1.5)                 #  btScalar angularSleepingThreshold

        self.body.setContactProcessingThreshold(0)
        self.body.setCcdMotionThreshold(0)
        self.body.setHitFraction(0.1 * SCALE_FACTOR)
Beispiel #2
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)
Beispiel #3
0
    def __init__(self, filename, view):
        Model.__init__(self, filename, view)
        TriangleMesh.__init__(self, filename)

        # Create rigid body
        tvia = TriangleIndexVertexArray()
        tvia.addIndexedMesh(self.mesh)
        shape = BvhTriangleMeshShape(tvia)
        shape.buildOptimizedBvh()
        transform = Transform()
        transform.setIdentity()
        transform.setOrigin(bVector3(0, 0, 0))
        motion = DefaultMotionState()
        motion.setWorldTransform(transform)

        self.body = RigidBody.fromConstructionInfo(motion,              #  MotionState motion
                                                   shape,               #  CollisionShape shape
                                                   0.0,                 #  btScalar mass
                                                   bVector3(0, 0, 0),   #  Vector3 inertia
                                                   transform,           #  Transform worldTransform
                                                   0.0,               	#  btScalar linearDamping
                                                   0.0,               	#  btScalar angularDamping
                                                   0.75,                #  btScalar friction
                                                   0.95,                #  btScalar restitution
                                                   0.0,                 #  btScalar linearSleepingThreshold
                                                   0.0)                 #  btScalar angularSleepingThreshold
        self.motion = motion
        self.body.setContactProcessingThreshold(0)
Beispiel #4
0
 def update_body_position(self):
     '''
     Update body position
     '''
     transform = Transform()
     self.mnumpy = array(self.m, 'f')
     transform.setFromOpenGLMatrix(self.mnumpy)
     self.motion.setWorldTransform(transform)
Beispiel #5
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
Beispiel #6
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
Beispiel #7
0
    def __init__(self, filename, view, cball_radius):
        super(CueStick, self).__init__(filename, view)
        self.half = self.model.dimension / 2.0
        self.tip_radius = self.half.x
        self.pivot = eVector3(0, 0, self.half.z + cball_radius)
        self.org_pivot = eVector3(*self.pivot)
        shape = CapsuleShapeZ(self.tip_radius, self.model.dimension.z - self.tip_radius * 2)
        #shape = CylinderShapeZ(bVector3(*self.half))
        transform = Transform()
        transform.setIdentity()
        self.motion = CSMotionState()
        self.motion.cue = self
        self.delta = 0.
        self.dx = 0.
        self.dy = 0.
        self.motion.setWorldTransform(transform)
        self.body = RigidBody.fromConstructionInfo(self.motion,         #  MotionState motion
                                                   shape,               #  CollisionShape shape
                                                   0.0,                 #  btScalar mass
                                                   bVector3(0, 0, 0),   #  Vector3 inertia
                                                   transform,           #  Transform worldTransform
                                                   0.0,                 #  btScalar linearDamping
                                                   0.0,                 #  btScalar angularDamping
                                                   0.0,                 #  btScalar friction
                                                   0.71,                #  btScalar restitution
                                                   0.0,                 #  btScalar linearSleepingThreshold
                                                   0.0)                 #  btScalar angularSleepingThreshold
        self.body.setContactProcessingThreshold(0)
        self.body.setCcdMotionThreshold(0)
        self.body.setHitFraction(0.1 * SCALE_FACTOR)
        self.body.setGravity(bVector3(0, 0, 0))

        # Make it kinematic body with collision contacts generation but no response impulses
        flags = self.body.getCollisionFlags() | CueStick.CF_KINEMATIC_OBJECT | CueStick.CF_NO_CONTACT_RESPONSE
        self.body.setCollisionFlags(flags)
        self.body.setActivationState(DISABLE_DEACTIVATION)
Beispiel #8
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
Beispiel #9
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
Beispiel #10
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)
Beispiel #11
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
Beispiel #12
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