Ejemplo n.º 1
0
    def __init__(self):
        super(ElasticBody, self).__init__()

        bodies = self.bodies
        for i in range(64):
            bodies.append(None)
        # Bottom static body
        sd = box2d.b2PolygonDef()
        sd.SetAsBox(50.0, 2.0)
        sd.friction = 0.1
        sd.restitution = 0.1
        bd = box2d.b2BodyDef()
        bd.position.Set(-1.0, -7.5)
        self.m_ground = self.world.CreateBody(bd)
        self.m_ground.CreateShape(sd)
        # Upper static body
        sd = box2d.b2PolygonDef()
        sd.SetAsBox(20.0, 0.50, box2d.b2Vec2(0, 0), 0.047 * box2d.b2_pi)
        sd.friction = 0.01
        sd.restitution = 0.001
        bd = box2d.b2BodyDef()
        bd.position.Set(-20, 93.0)
        g = self.world.CreateBody(bd)
        g.CreateShape(sd)
        sd.SetAsBox(15, 0.50, box2d.b2Vec2(-15.0, 12.5), 0.0)
        g.CreateShape(sd)

        sd.SetAsBox(20, 0.5, box2d.b2Vec2(0.0, -25.0), -0.5)
        g.CreateShape(sd)
        # Left channel left wall
        sd = box2d.b2PolygonDef()
        sd.SetAsBox(0.7, 55.0)
        sd.friction = 0.1
        sd.restitution = 0.1
        bd = box2d.b2BodyDef()
        bd.position.Set(-49.3, 50.0)
        g = self.world.CreateBody(bd)
        g.CreateShape(sd)
        # Right wall
        sd = box2d.b2PolygonDef()
        sd.SetAsBox(0.7, 55.0)
        sd.friction = 0.1
        sd.restitution = 0.1
        bd = box2d.b2BodyDef()
        bd.position.Set(45, 50.0)
        g = self.world.CreateBody(bd)
        g.CreateShape(sd)
        # Left channel right upper wall
        sd = box2d.b2PolygonDef()
        sd.SetAsBox(0.5, 20.0)
        sd.friction = 0.05
        sd.restitution = 0.01
        bd = box2d.b2BodyDef()
        bd.position.Set(-42.0, 70.0)
        bd.angle = -0.03 * box2d.b2_pi
        g = self.world.CreateBody(bd)
        g.CreateShape(sd)
        # Left channel right lower wall
        sd = box2d.b2PolygonDef()
        sd.SetAsBox(0.50, 23.0)
        sd.friction = 0.05
        sd.restitution = 0.01
        bd = box2d.b2BodyDef()
        bd.position.Set(-44.0, 27.0)
        g = self.world.CreateBody(bd)
        g.CreateShape(sd)
        # Bottom motors
        cd = box2d.b2CircleDef()
        cd.radius = 3.0
        cd.density = 15.0
        cd.friction = 1
        cd.restitution = 0.2
        # 1.
        bd.position.Set(-40.0, 2.5)
        body = self.world.CreateBody(bd)
        body.CreateShape(cd)
        body.SetMassFromShapes()
        jr = box2d.b2RevoluteJointDef()
        jr.Initialize(g, body, body.GetWorldCenter() + box2d.b2Vec2(0, 1))
        jr.maxMotorTorque = 30000
        jr.enableMotor = True
        jr.motorSpeed = 20
        self.world.CreateJoint(jr).getAsType()
        # 1. left down
        bd.position.Set(-46.0, -2.5)
        cd.radius = 1.5
        jr.motorSpeed = -20
        body = self.world.CreateBody(bd)
        body.CreateShape(cd)
        sd.SetAsBox(2.0, 0.50)
        body.CreateShape(sd)
        body.SetMassFromShapes()
        jr.Initialize(g, body, body.GetWorldCenter())
        self.world.CreateJoint(jr).getAsType()
        # 2.
        cd.radius = 3.0
        jr.motorSpeed = 20
        bd.position.Set(-32.0, 2.5)
        body = self.world.CreateBody(bd)
        body.CreateShape(cd)
        body.SetMassFromShapes()
        jr.Initialize(g, body, body.GetWorldCenter() + box2d.b2Vec2(0, 1))
        self.world.CreateJoint(jr).getAsType()
        # 3.
        jr.motorSpeed = 20
        bd.position.Set(-24.0, 1.5)
        body = self.world.CreateBody(bd)
        body.CreateShape(cd)
        body.SetMassFromShapes()
        jr.Initialize(g, body, body.GetWorldCenter() + box2d.b2Vec2(0, 1))
        self.world.CreateJoint(jr).getAsType()
        # 4.
        bd.position.Set(-16.0, 0.8)
        body = self.world.CreateBody(bd)
        body.CreateShape(cd)
        body.SetMassFromShapes()
        jr.Initialize(g, body, body.GetWorldCenter() + box2d.b2Vec2(0, 1))
        self.world.CreateJoint(jr).getAsType()
        # 5.
        bd.position.Set(-8.0, 0.5)
        body = self.world.CreateBody(bd)
        body.CreateShape(cd)
        body.SetMassFromShapes()
        jr.Initialize(g, body, body.GetWorldCenter() + box2d.b2Vec2(0, 1))
        self.world.CreateJoint(jr).getAsType()
        # 6.
        bd.position.Set(0.0, 0.1)
        body = self.world.CreateBody(bd)
        body.CreateShape(cd)
        body.SetMassFromShapes()
        jr.Initialize(g, body, body.GetWorldCenter() + box2d.b2Vec2(0, 1))
        self.world.CreateJoint(jr).getAsType()
        # 7.
        bd.position.Set(8.0, -0.5)
        body = self.world.CreateBody(bd)
        body.CreateShape(cd)
        sd.SetAsBox(3.7, 0.5)
        body.CreateShape(sd)
        body.SetMassFromShapes()
        jr.Initialize(g, body, body.GetWorldCenter() + box2d.b2Vec2(0, 1))
        self.world.CreateJoint(jr).getAsType()
        # 8. right rotator
        sd.SetAsBox(5, 0.5)
        sd.density = 2.0
        bd.position.Set(18.0, 1)
        rightmotor = self.world.CreateBody(bd)  #
        rightmotor.CreateShape(sd)
        sd.SetAsBox(4.5, 0.5, box2d.b2Vec2(0, 0), box2d.b2_pi / 3)
        rightmotor.CreateShape(sd)
        sd.SetAsBox(4.5, 0.5, box2d.b2Vec2(0, 0), box2d.b2_pi * 2 / 3)
        rightmotor.CreateShape(sd)
        cd.radius = 4.2
        rightmotor.CreateShape(cd)
        rightmotor.SetMassFromShapes()
        jr.Initialize(g, rightmotor, rightmotor.GetWorldCenter())
        jr.maxMotorTorque = 70000
        jr.motorSpeed = -4
        self.world.CreateJoint(jr).getAsType()
        # 9. left rotator
        sd.SetAsBox(8.5, 0.5)
        sd.density = 2.0
        bd.position.Set(-34.0, 17)
        body = self.world.CreateBody(bd)
        body.CreateShape(sd)
        sd.SetAsBox(8.5, 0.5, box2d.b2Vec2(0, 0), box2d.b2_pi * 0.5)
        body.CreateShape(sd)
        cd.radius = 7
        cd.friction = 0.9
        body.CreateShape(cd)
        body.SetMassFromShapes()
        jr.Initialize(g, body, body.GetWorldCenter())
        jr.maxMotorTorque = 100000
        jr.motorSpeed = -5
        self.world.CreateJoint(jr).getAsType()
        # big compressor
        sd.SetAsBox(3.0, 4)
        sd.density = 10.0
        bd.position.Set(-16.0, 17)
        hammerleft = self.world.CreateBody(bd)
        hammerleft.CreateShape(sd)
        hammerleft.SetMassFromShapes()
        jd = box2d.b2DistanceJointDef()
        jd.Initialize(body, hammerleft, body.GetWorldCenter() + box2d.b2Vec2(0, 6), hammerleft.GetWorldCenter())
        self.world.CreateJoint(jd).getAsType()

        bd.position.Set(4.0, 17)
        hammerright = self.world.CreateBody(bd)
        hammerright.CreateShape(sd)
        hammerright.SetMassFromShapes()
        jd.Initialize(body, hammerright, body.GetWorldCenter() - box2d.b2Vec2(0, 6), hammerright.GetWorldCenter())
        self.world.CreateJoint(jd).getAsType()
        # pusher
        sd.SetAsBox(6, 0.75)
        bd.position.Set(-21.0, 9)
        pusher = self.world.CreateBody(bd)  #
        pusher.CreateShape(sd)
        sd.SetAsBox(2, 1.5, box2d.b2Vec2(-5, 0), 0)
        pusher.SetMassFromShapes()
        pusher.CreateShape(sd)
        jd.Initialize(
            rightmotor,
            pusher,
            rightmotor.GetWorldCenter() + box2d.b2Vec2(-8.0, 0),
            pusher.GetWorldCenter() + box2d.b2Vec2(5.0, 0),
        )
        self.world.CreateJoint(jd).getAsType()
        # Static bodies above motors
        sd = box2d.b2PolygonDef()
        cd = box2d.b2CircleDef()
        sd.SetAsBox(9.0, 0.5)
        sd.friction = 0.05
        sd.restitution = 0.01
        bd = box2d.b2BodyDef()
        bd.position.Set(-15.5, 12)
        bd.angle = 0.0
        g = self.world.CreateBody(bd)
        g.CreateShape(sd)

        sd.SetAsBox(8, 0.5, box2d.b2Vec2(23, 0), 0)
        g.CreateShape(sd)
        # compressor statics
        sd.SetAsBox(7.0, 0.5, box2d.b2Vec2(-2, 9), 0)
        g.CreateShape(sd)
        sd.SetAsBox(9.0, 0.5, box2d.b2Vec2(22, 9), 0)
        g.CreateShape(sd)

        sd.SetAsBox(19.0, 0.5, box2d.b2Vec2(-9, 15), -0.05)
        g.CreateShape(sd)
        sd.SetAsBox(4.7, 0.5, box2d.b2Vec2(15, 11.5), -0.5)
        g.CreateShape(sd)
        # below compressor
        sd.SetAsBox(26.0, 0.3, box2d.b2Vec2(17, -4.4), -0.02)
        g.CreateShape(sd)
        cd.radius = 1.0
        cd.friction = 1.0
        cd.localPosition = box2d.b2Vec2(29, -6)
        g.CreateShape(cd)
        cd.radius = 0.7
        cd.localPosition = box2d.b2Vec2(-2, -4.5)
        g.CreateShape(cd)
        # Elevator
        bd = box2d.b2BodyDef()
        cd = box2d.b2CircleDef()
        sd = box2d.b2PolygonDef()

        bd.position.Set(40.0, 4.0)
        self.m_elev = self.world.CreateBody(bd)

        sd.SetAsBox(0.5, 2.5, box2d.b2Vec2(3.0, -3.0), 0)
        sd.density = 1
        sd.friction = 0.01
        self.m_elev.CreateShape(sd)
        sd.SetAsBox(7.0, 0.5, box2d.b2Vec2(-3.5, -5.5), 0)
        self.m_elev.CreateShape(sd)
        sd.SetAsBox(0.5, 2.5, box2d.b2Vec2(-11, -3.5), 0)
        self.m_elev.CreateShape(sd)
        self.m_elev.SetMassFromShapes()

        jp = box2d.b2PrismaticJointDef()
        jp.Initialize(self.m_ground, self.m_elev, bd.position, box2d.b2Vec2(0.0, 1.0))
        jp.lowerTranslation = 0.0
        jp.upperTranslation = 100.0
        jp.enableLimit = True
        jp.enableMotor = True
        jp.maxMotorForce = 10000
        jp.motorSpeed = 0
        self.m_joint_elev = self.world.CreateJoint(jp).getAsType()

        # Korb
        sd.SetAsBox(2.3, 0.5, box2d.b2Vec2(1, 0.0), 0.0)
        sd.density = 0.5
        bd.position.Set(29.0, 6.5)
        body = self.world.CreateBody(bd)  #
        body.CreateShape(sd)
        sd.SetAsBox(2.5, 0.5, box2d.b2Vec2(3.0, -2), box2d.b2_pi / 2)
        body.CreateShape(sd)
        sd.SetAsBox(4.6, 0.5, box2d.b2Vec2(7.8, -4.0), 0)
        body.CreateShape(sd)
        sd.SetAsBox(0.5, 4.5, box2d.b2Vec2(12, 0.0), 0)
        body.CreateShape(sd)

        sd.SetAsBox(0.5, 0.5, box2d.b2Vec2(13, 4.0), 0)
        body.CreateShape(sd)

        cd.radius = 0.7
        cd.density = 1
        cd.friction = 0.01
        cd.localPosition = box2d.b2Vec2(0, 0)
        body.CreateShape(cd)
        body.SetMassFromShapes()

        jr = box2d.b2RevoluteJointDef()
        jr.Initialize(self.m_elev, body, bd.position)
        jr.enableLimit = True
        jr.lowerAngle = -0.2
        jr.upperAngle = box2d.b2_pi * 1.1
        jr.collideConnected = True
        self.world.CreateJoint(jr).getAsType()
        # upper body exit
        sd.SetAsBox(14.0, 0.5, box2d.b2Vec2(-3.5, -10.0), 0.0)
        bd.position.Set(17.5, 96.0)
        body = self.world.CreateBody(bd)
        body.CreateShape(sd)
        # "Elastic body" 64 bodies - something like a lin. elastic compound
        # connected via dynamic forces (springs)
        sd = box2d.b2PolygonDef()
        sd.SetAsBox(0.55, 0.55)
        sd.density = 1.5
        sd.friction = 0.01
        sd.filter.groupIndex = -1
        startpoint = box2d.b2Vec2(30, 20)
        bd = box2d.b2BodyDef()
        bd.isBullet = False
        bd.allowSleep = False
        for i in range(8):
            for j in range(8):
                bd.position.Set(j * 1.02, 2.51 + 1.02 * i)
                bd.position += startpoint
                body = self.world.CreateBody(bd)  #
                bodies[8 * i + j] = body
                body.CreateShape(sd)
                body.SetMassFromShapes()
Ejemplo n.º 2
0
    def CreateLeg(self, s, wheelAnchor) :
        p1=box2d.b2Vec2(5.4 * s, -6.1)
        p2=box2d.b2Vec2(7.2 * s, -1.2)
        p3=box2d.b2Vec2(4.3 * s, -1.9)
        p4=box2d.b2Vec2(3.1 * s, 0.8)
        p5=box2d.b2Vec2(6.0 * s, 1.5)
        p6=box2d.b2Vec2(2.5 * s, 3.7)
        
        sd1=box2d.b2PolygonDef()
        sd2=box2d.b2PolygonDef()
        sd1.vertexCount = 3
        sd2.vertexCount = 3
        sd1.filter.groupIndex = -1
        sd2.filter.groupIndex = -1
        sd1.density = 1.0
        sd2.density = 1.0
        
        if s > 0.0:
            sd1.setVertex(0, p1)
            sd1.setVertex(1, p2)
            sd1.setVertex(2, p3)
            
            sd2.setVertex(0, box2d.b2Vec2_zero)
            sd2.setVertex(1, p5 - p4)
            sd2.setVertex(2, p6 - p4)
        else:
            sd1.setVertex(0, p1)
            sd1.setVertex(1, p3)
            sd1.setVertex(2, p2)
            
            sd2.setVertex(0, box2d.b2Vec2_zero)
            sd2.setVertex(1, p6 - p4)
            sd2.setVertex(2, p5 - p4)

        bd1=box2d.b2BodyDef()
        bd2=box2d.b2BodyDef()
        bd1.position = self.m_offset
        bd2.position = p4 + self.m_offset
        
        bd1.angularDamping = 10.0
        bd2.angularDamping = 10.0
        
        body1 = self.world.CreateBody(bd1) 
        body2 = self.world.CreateBody(bd2) 
        
        body1.CreateShape(sd1)
        body2.CreateShape(sd2)
        
        body1.SetMassFromShapes()
        body2.SetMassFromShapes()
        
        djd=box2d.b2DistanceJointDef()
        
        # Using a soft distance constraint can reduce some jitter.
        # It also makes the structure seem a bit more fluid by
        # acting like a suspension system.
        #djd.dampingRatio = 0.5
        #djd.frequencyHz = 10.0
        # svn r149, not usable yet
        
        djd.Initialize(body1, body2, p2 + self.m_offset, p5 + self.m_offset)
        self.world.CreateJoint(djd).getAsType() 
        
        djd.Initialize(body1, body2, p3 + self.m_offset, p4 + self.m_offset)
        self.world.CreateJoint(djd).getAsType() 
        
        djd.Initialize(body1, self.m_wheel, p3 + self.m_offset, wheelAnchor + self.m_offset)
        self.world.CreateJoint(djd).getAsType() 
        
        djd.Initialize(body2, self.m_wheel, p6 + self.m_offset, wheelAnchor + self.m_offset)
        self.world.CreateJoint(djd).getAsType() 
        
        rjd=box2d.b2RevoluteJointDef() 
        
        rjd.Initialize(body2, self.m_chassis, p4 + self.m_offset)
        self.world.CreateJoint(rjd).getAsType() 
Ejemplo n.º 3
0
    def __init__(self):
        super(Dominos, self).__init__()
        sd=box2d.b2PolygonDef()
        sd.SetAsBox(50.0, 10.0)

        bd=box2d.b2BodyDef()
        bd.position.Set(0.0, -10.0)
        b1 = self.world.CreateBody(bd)
        b1.CreateShape(sd)

        sd=box2d.b2PolygonDef()
        sd.SetAsBox(6.0, 0.25)

        bd=box2d.b2BodyDef()
        bd.position.Set(-1.5, 10.0)
        ground = self.world.CreateBody(bd) 
        ground.CreateShape(sd)

        sd=box2d.b2PolygonDef() 
        sd.SetAsBox(0.1, 1.0)
        sd.density = 20.0
        sd.friction = 0.1

        
        for i in range(10):
            bd=box2d.b2BodyDef() 
            bd.position.Set(-6.0 + 1.0 * i, 11.25)
            body = self.world.CreateBody(bd) 
            body.CreateShape(sd)
            body.SetMassFromShapes()

        sd=box2d.b2PolygonDef() 
        sd.SetAsBox(7.0, 0.25, box2d.b2Vec2_zero, 0.3)

        bd=box2d.b2BodyDef() 
        bd.position.Set(1.0, 6.0)
        ground = self.world.CreateBody(bd) 
        ground.CreateShape(sd)

        
        sd=box2d.b2PolygonDef() 
        sd.SetAsBox(0.25, 1.5)

        bd=box2d.b2BodyDef() 
        bd.position.Set(-7.0, 4.0)
        b2 = self.world.CreateBody(bd)
        b2.CreateShape(sd)

        
        sd=box2d.b2PolygonDef() 
        sd.SetAsBox(6.0, 0.125)
        sd.density = 10.0

        bd=box2d.b2BodyDef() 
        bd.position.Set(-0.9, 1.0)
        bd.angle = -0.15

        b3 = self.world.CreateBody(bd)
        b3.CreateShape(sd)
        b3.SetMassFromShapes()

        jd=box2d.b2RevoluteJointDef() 
        anchor=box2d.b2Vec2() 

        anchor.Set(-2.0, 1.0)
        jd.Initialize(b1, b3, anchor)
        jd.collideConnected = True
        self.world.CreateJoint(jd).getAsType() 

        
        sd=box2d.b2PolygonDef() 
        sd.SetAsBox(0.25, 0.25)
        sd.density = 10.0

        bd=box2d.b2BodyDef() 
        bd.position.Set(-10.0, 15.0)
        b4 = self.world.CreateBody(bd)
        b4.CreateShape(sd)
        b4.SetMassFromShapes()

        anchor.Set(-7.0, 15.0)
        jd.Initialize(b2, b4, anchor)
        self.world.CreateJoint(jd).getAsType() 

        
        bd=box2d.b2BodyDef() 
        bd.position.Set(6.5, 3.0)
        b5 = self.world.CreateBody(bd)

        sd=box2d.b2PolygonDef() 
        sd.density = 10.0
        sd.friction = 0.1

        sd.SetAsBox(1.0, 0.1, box2d.b2Vec2(0.0, -0.9), 0.0)
        b5.CreateShape(sd)

        sd.SetAsBox(0.1, 1.0, box2d.b2Vec2(-0.9, 0.0), 0.0)
        b5.CreateShape(sd)

        sd.SetAsBox(0.1, 1.0, box2d.b2Vec2(0.9, 0.0), 0.0)
        b5.CreateShape(sd)

        b5.SetMassFromShapes()

        anchor.Set(6.0, 2.0)
        jd.Initialize(b1, b5, anchor)
        self.world.CreateJoint(jd).getAsType() 

        
        sd=box2d.b2PolygonDef() 
        sd.SetAsBox(1.0, 0.1)
        sd.density = 30.0
        sd.friction = 0.2

        bd=box2d.b2BodyDef() 
        bd.position.Set(6.5, 4.1)
        b6 = self.world.CreateBody(bd)
        b6.CreateShape(sd)
        b6.SetMassFromShapes()

        anchor.Set(7.5, 4.0)
        jd.Initialize(b5, b6, anchor)
        self.world.CreateJoint(jd).getAsType() 

        
        sd=box2d.b2PolygonDef() 
        sd.SetAsBox(0.1, 1.0)
        sd.density = 10.0

        bd=box2d.b2BodyDef() 
        bd.position.Set(7.4, 1.0)

        b7 = self.world.CreateBody(bd)
        b7.CreateShape(sd)
        b7.SetMassFromShapes()

        djd=box2d.b2DistanceJointDef() 
        djd.body1 = b3
        djd.body2 = b7
        djd.localAnchor1.Set(6.0, 0.0)
        djd.localAnchor2.Set(0.0, -1.0)
        d = djd.body2.GetWorldPoint(djd.localAnchor2) - djd.body1.GetWorldPoint(djd.localAnchor1)
        djd.length = d.Length()
        self.world.CreateJoint(djd).getAsType() 

        sd=box2d.b2CircleDef() 
        sd.radius = 0.2
        sd.density = 10.0

        
        for i in range(4):
            bd=box2d.b2BodyDef() 
            bd.position.Set(5.9 + 2.0 * sd.radius * i, 2.4)
            body = self.world.CreateBody(bd) 
            body.CreateShape(sd)
            body.SetMassFromShapes()