Exemple #1
0
    def __init__(self):
        super(ApplyForce, self).__init__()
        self.world.SetGravity(box2d.b2Vec2(0.0, 0.0))

        k_restitution = 0.4

        bd=box2d.b2BodyDef() 
        bd.position.Set(0.0, 20.0)
        ground = self.world.CreateBody(bd) 

        sd=box2d.b2PolygonDef() 
        sd.density = 0.0
        sd.restitution = k_restitution

        sd.SetAsBox(0.2, 20.0, box2d.b2Vec2(-20.0, 0.0), 0.0)
        ground.CreateShape(sd)

        sd.SetAsBox(0.2, 20.0, box2d.b2Vec2(20.0, 0.0), 0.0)
        ground.CreateShape(sd)

        sd.SetAsBox(0.2, 20.0, box2d.b2Vec2(0.0, -20.0), 0.5 * box2d.b2_pi)
        ground.CreateShape(sd)

        sd.SetAsBox(0.2, 20.0, box2d.b2Vec2(0.0, 20.0), -0.5 * box2d.b2_pi)
        ground.CreateShape(sd)

        xf1 = box2d.b2XForm ()
        xf1.R.Set(0.3524 * box2d.b2_pi)
        xf1.position = box2d.b2Mul(xf1.R, box2d.b2Vec2(1.0, 0.0))

        sd1=box2d.b2PolygonDef() 
        sd1.vertexCount = 3
        sd1.setVertex(0, box2d.b2Mul(xf1, box2d.b2Vec2(-1.0, 0.0)))
        sd1.setVertex(1, box2d.b2Mul(xf1, box2d.b2Vec2(1.0, 0.0)))
        sd1.setVertex(2, box2d.b2Mul(xf1, box2d.b2Vec2(0.0, 0.5)))
        sd1.density = 2.0

        xf2 = box2d.b2XForm ()
        xf2.R.Set(-0.3524 * box2d.b2_pi)
        xf2.position = box2d.b2Mul(xf2.R, box2d.b2Vec2(-1.0, 0.0))

        sd2=box2d.b2PolygonDef() 
        sd2.vertexCount = 3
        sd2.setVertex(0, box2d.b2Mul(xf2, box2d.b2Vec2(-1.0, 0.0)))
        sd2.setVertex(1, box2d.b2Mul(xf2, box2d.b2Vec2(1.0, 0.0)))
        sd2.setVertex(2, box2d.b2Mul(xf2, box2d.b2Vec2(0.0, 0.5)))
        sd2.density = 2.0

        bd=box2d.b2BodyDef() 
        bd.angularDamping = 2.0
        bd.linearDamping = 0.1

        bd.position.Set(0.0, 1.05)
        bd.angle = box2d.b2_pi
        self.m_body = self.world.CreateBody(bd)
        self.m_body.CreateShape(sd1)
        self.m_body.CreateShape(sd2)
        self.m_body.SetMassFromShapes()
    def Step(self, settings) :
        sweep1=box2d.b2Sweep()
        sweep1.c0.Set(0.0, 20.0)
        sweep1.a0 = 0.0
        sweep1.c = sweep1.c0
        sweep1.a = sweep1.a0
        sweep1.t0 = 0.0
        sweep1.localCenter = self.m_body1.GetLocalCenter()

        sweep2=box2d.b2Sweep()
        sweep2.c0.Set(9.6363468, 28.050615)
        sweep2.a0 = 1.6408679
        sweep2.c = sweep2.c0 + box2d.b2Vec2(-0.075121880, 0.27358246)
        sweep2.a = sweep2.a0 - 10.434675
        sweep2.t0 = 0.0
        sweep2.localCenter = self.m_body2.GetLocalCenter()

        toi = box2d.b2TimeOfImpact(self.m_shape1, sweep1, self.m_shape2, sweep2)

        self.DrawString(5, self.textLine, "toi = %g" % (toi))
        self.textLine += 15

        xf2=box2d.b2XForm ()
        sweep2.GetXForm(xf2, toi)
        vertexCount = self.m_shape2.GetVertexCount()
        vertices = []

        localVertices = self.m_shape2.getVertices_b2Vec2()
        for vertex in localVertices:
            vertices.append( box2d.b2Mul(xf2, vertex).tuple() )

        self.debugDraw.DrawPolygon(vertices, vertexCount, box2d.b2Color(0.5, 0.7, 0.9))

        localVertices = self.m_shape2.getCoreVertices_b2Vec2()
        for vertex in localVertices:
            vertices.append( box2d.b2Mul(xf2, vertex).tuple() )
        self.debugDraw.DrawPolygon(vertices, vertexCount, box2d.b2Color(0.5, 0.7, 0.9))
     
        settings.pause = True
        super(TimeOfImpact, self).Step(settings)
        settings.pause = False
    def __init__(self):
        super(CompoundShapes, self).__init__()
        bd=box2d.b2BodyDef() 
        bd.position.Set(0.0, -10.0)
        body = self.world.CreateBody(bd) 

        sd=box2d.b2PolygonDef() 
        sd.SetAsBox(50.0, 10.0)
        body.CreateShape(sd)

        sd1=box2d.b2CircleDef() 
        sd1.radius = 0.5
        sd1.localPosition.Set(-0.5, 0.5)
        sd1.density = 2.0

        sd2=box2d.b2CircleDef() 
        sd2.radius = 0.5
        sd2.localPosition.Set(0.5, 0.5)
        sd2.density = 0.0 

        for i in range(10):
            x = box2d.b2Random(-0.1, 0.1)
            bd=box2d.b2BodyDef() 
            bd.position.Set(x + 5.0, 1.05 + 2.5 * i)
            bd.angle = box2d.b2Random(-box2d.b2_pi, box2d.b2_pi)
            body = self.world.CreateBody(bd) 
            body.CreateShape(sd1)
            body.CreateShape(sd2)
            body.SetMassFromShapes()

        sd1=box2d.b2PolygonDef() 
        sd1.SetAsBox(0.25, 0.5)
        sd1.density = 2.0

        sd2=box2d.b2PolygonDef() 
        sd2.SetAsBox(0.25, 0.5, box2d.b2Vec2(0.0, -0.5), 0.5 * box2d.b2_pi)
        sd2.density = 2.0

        for i in range(10):
            x = box2d.b2Random(-0.1, 0.1)
            bd=box2d.b2BodyDef() 
            bd.position.Set(x - 5.0, 1.05 + 2.5 * i)
            bd.angle = box2d.b2Random(-box2d.b2_pi, box2d.b2_pi)
            body = self.world.CreateBody(bd) 
            body.CreateShape(sd1)
            body.CreateShape(sd2)
            body.SetMassFromShapes()

        xf1 = box2d.b2XForm()
        xf1.R.Set(0.3524 * box2d.b2_pi)
        xf1.position = box2d.b2Mul(xf1.R, box2d.b2Vec2(1.0, 0.0))

        sd1=box2d.b2PolygonDef() 
        sd1.vertexCount = 3
        sd1.setVertex(0, box2d.b2Mul(xf1, box2d.b2Vec2(-1.0, 0.0)))
        sd1.setVertex(1, box2d.b2Mul(xf1, box2d.b2Vec2(1.0, 0.0)))
        sd1.setVertex(2, box2d.b2Mul(xf1, box2d.b2Vec2(0.0, 0.5)))
        sd1.density = 2.0

        xf2 = box2d.b2XForm()
        xf2.R.Set(-0.3524 * box2d.b2_pi)
        xf2.position = box2d.b2Mul(xf2.R, box2d.b2Vec2(-1.0, 0.0))

        sd2=box2d.b2PolygonDef() 
        sd2.vertexCount = 3
        sd2.setVertex(0, box2d.b2Mul(xf2, box2d.b2Vec2(-1.0, 0.0)))
        sd2.setVertex(1, box2d.b2Mul(xf2, box2d.b2Vec2(1.0, 0.0)))
        sd2.setVertex(2, box2d.b2Mul(xf2, box2d.b2Vec2(0.0, 0.5)))
        sd2.density = 2.0

        for i in range(10):
            x = box2d.b2Random(-0.1, 0.1)
            bd=box2d.b2BodyDef() 
            bd.position.Set(x, 2.05 + 2.5 * i)
            bd.angle = 0.0
            body = self.world.CreateBody(bd) 
            body.CreateShape(sd1)
            body.CreateShape(sd2)
            body.SetMassFromShapes()

        sd_bottom=box2d.b2PolygonDef() 
        sd_bottom.SetAsBox( 1.5, 0.15 )
        sd_bottom.density = 4.0

        sd_left=box2d.b2PolygonDef() 
        sd_left.SetAsBox(0.15, 2.7, box2d.b2Vec2(-1.45, 2.35), 0.2)
        sd_left.density = 4.0

        sd_right=box2d.b2PolygonDef() 
        sd_right.SetAsBox(0.15, 2.7, box2d.b2Vec2(1.45, 2.35), -0.2)
        sd_right.density = 4.0

        bd=box2d.b2BodyDef() 
        bd.position.Set( 0.0, 2.0 )
        body = self.world.CreateBody(bd) 
        body.CreateShape(sd_bottom)
        body.CreateShape(sd_left)
        body.CreateShape(sd_right)
        body.SetMassFromShapes()