def createWorld(self, world):
        world.setGravity(Vector2(0, 0))

        k_restitution = 0.4

        bd = BodyDef()
        bd.position.set(0, 20)
        ground = world.createBody(bd)

        shape = EdgeShape()

        sd = FixtureDef()
        sd.shape = shape
        sd.density = 0
        sd.restitution = k_restitution

        shape.set(Vector2(-20, -20), Vector2(-20, 20))
        ground.createFixture(sd)

        shape.set(Vector2(20, -20), Vector2(20, 20))
        ground.createFixture(sd)

        shape.set(Vector2(-20, 20), Vector2(20, 20))
        ground.createFixture(sd)

        shape.set(Vector2(-20, -20), Vector2(20, -20))
        ground.createFixture(sd)

        shape.dispose()

        xf1 = Transform(Vector2(), 0.3524 * Math.PI)
        xf1.setPosition(xf1.mul(Vector2(1, 0)))

        vertices = [None, None, None]
        vertices[0] = xf1.mul(Vector2(-1, 0))
        vertices[1] = xf1.mul(Vector2(1, 0))
        vertices[2] = xf1.mul(Vector2(0, 0.5))

        poly1 = PolygonShape()
        poly1.set(vertices)

        sd1 = FixtureDef()
        sd1.shape = poly1
        sd1.density = 4.0

        xf2 = Transform(Vector2(), -0.3524 * Math.PI)
        xf2.setPosition(xf2.mul(Vector2(-1, 0)))

        vertices[0] = xf2.mul(Vector2(-1, 0))
        vertices[1] = xf2.mul(Vector2(1, 0))
        vertices[2] = xf2.mul(Vector2(0, 0.5))

        poly2 = PolygonShape()
        poly2.set(vertices)

        sd2 = FixtureDef()
        sd2.shape = poly2
        sd2.density = 2.0

        bd = BodyDef()
        bd.type = BodyType.DynamicBody
        bd.angularDamping = 5.0
        bd.linearDamping = 0.1

        bd.position.set(0, 2)
        bd.angle = Math.PI
        bd.allowSleep = False
        self.m_body = world.createBody(bd)
        self.m_body.createFixture(sd1)
        self.m_body.createFixture(sd2)
        poly1.dispose()
        poly2.dispose()

        shape = PolygonShape()
        shape.setAsBox(0.5, 0.5)

        fd = FixtureDef()
        fd.shape = shape
        fd.density = 1.0
        fd.friction = 0.3

        for i in range(10):
            bd = BodyDef()
            bd.type = BodyType.DynamicBody

            bd.position.set(0, 5 + 1.54 * i)
            body = world.createBody(bd)

            body.createFixture(fd)

            gravity = 10.0
            I = body.getInertia()
            mass = body.getMass()

            radius = Math.sqrt(2 * I / mass)

            jd = FrictionJointDef()
            jd.localAnchorA.set(0, 0)
            jd.localAnchorB.set(0, 0)
            jd.bodyA = ground
            jd.bodyB = body
            jd.collideConnected = True
            jd.maxForce = mass * gravity
            jd.maxTorque = mass * radius * gravity

            world.createJoint(jd)

        shape.dispose()
Exemplo n.º 2
0
    def createWorld(self, world):
        shape = EdgeShape()
        shape.set(Vector2(-40.0, 0), Vector2(40, 0))

        fd = FixtureDef()
        fd.shape = shape
        fd.friction = 0.3

        bd = BodyDef()
        ground = world.createBody(bd)
        ground.createFixture(fd)
        shape.dispose()

        vertices = []
        vertices.append(Vector2(-1, 0))
        vertices.append(Vector2(1, 0))
        vertices.append(Vector2(0, 2))
        polygon = PolygonShape()
        polygon.set(vertices)

        triangleShapeDef = FixtureDef()
        triangleShapeDef.shape = polygon
        triangleShapeDef.density = 1.0

        triangleShapeDef.filter.groupIndex = self.k_smallGroup
        triangleShapeDef.filter.categoryBits = self.k_triangleCategory
        triangleShapeDef.filter.maskBits = self.k_triangleMask

        triangleBodyDef = BodyDef()
        triangleBodyDef.type = BodyType.DynamicBody
        triangleBodyDef.position.set(-5, 2)

        body1 = world.createBody(triangleBodyDef)
        body1.createFixture(triangleShapeDef)

        vertices[0].scl(2)
        vertices[1].scl(2)
        vertices[2].scl(2)

        polygon.set(vertices)
        triangleShapeDef.filter.groupIndex = self.k_largeGroup
        triangleBodyDef.position.set(-5, 6)
        triangleBodyDef.fixedRotation = True

        body2 = world.createBody(triangleBodyDef)
        body2.createFixture(triangleShapeDef)

        bd = BodyDef()
        bd.type = BodyType.DynamicBody
        bd.position.set(-5, 10)
        body = world.createBody(bd)

        p = PolygonShape()
        p.setAsBox(0.5, 1.0)
        body.createFixture(p, 1)

        jd = PrismaticJointDef()
        jd.bodyA = body2
        jd.bodyB = body
        jd.enableLimit = True
        jd.localAnchorA.set(0, 4)
        jd.localAnchorB.set(0, 0)
        jd.localAxisA.set(0, 1)
        jd.lowerTranslation = -1
        jd.upperTranslation = 1

        world.createJoint(jd)

        p.dispose()

        polygon.setAsBox(1, 0.5)
        boxShapeDef = FixtureDef()
        boxShapeDef.shape = polygon
        boxShapeDef.density = 1
        boxShapeDef.restitution = 0.1

        boxShapeDef.filter.groupIndex = self.k_smallGroup
        boxShapeDef.filter.categoryBits = self.k_boxCategory
        boxShapeDef.filter.maskBits = self.k_boxMask

        boxBodyDef = BodyDef()
        boxBodyDef.type = BodyType.DynamicBody
        boxBodyDef.position.set(0, 2)

        body3 = world.createBody(boxBodyDef)
        body3.createFixture(boxShapeDef)

        polygon.setAsBox(2, 1)
        boxShapeDef.filter.groupIndex = self.k_largeGroup
        boxBodyDef.position.set(0, 6)

        body4 = world.createBody(boxBodyDef)
        body4.createFixture(boxShapeDef)

        circle = CircleShape()
        circle.setRadius(1)

        circleShapeDef = FixtureDef()
        circleShapeDef.shape = circle
        circleShapeDef.density = 1.0

        circleShapeDef.filter.groupIndex = self.k_smallGroup
        circleShapeDef.filter.categoryBits = self.k_circleCategory
        circleShapeDef.filter.maskBits = self.k_circleMask

        circleBodyDef = BodyDef()
        circleBodyDef.type = BodyType.DynamicBody
        circleBodyDef.position.set(5, 2)

        body5 = world.createBody(circleBodyDef)
        body5.createFixture(circleShapeDef)

        circle.setRadius(2)
        circleShapeDef.filter.groupIndex = self.k_largeGroup
        circleBodyDef.position.set(5, 6)

        body6 = world.createBody(circleBodyDef)
        body6.createFixture(circleShapeDef)