コード例 #1
0
ファイル: box_cutter.py プロジェクト: aaronma37/pybox2d
    def __init__(self):
        super(BoxCutter, self).__init__()
        # The ground
        self.ground = self.world.CreateStaticBody(
            userData='ground',
            shapes=[
                b2EdgeShape(vertices=[(-50, 0), (50, 0)]),
                b2EdgeShape(vertices=[(-50, 0), (-50, 10)]),
                b2EdgeShape(vertices=[(50, 0), (50, 10)]),
            ]
        )

        self.laser_body = self.world.CreateDynamicBody(
            userData='laser',
            position=(0, 2),
            fixtures=b2FixtureDef(
                density=4.0,
                shape=b2PolygonShape(box=(LASER_HALF_WIDTH, 1))
            )
        )

        for i in range(2):
            self.world.CreateDynamicBody(
                userData=LASER_SPLIT_TAG,
                position=(3.0 + i * 6, 8),
                fixtures=b2FixtureDef(
                    density=5.0,
                    shape=b2PolygonShape(box=(3, 3))
                )
            )
コード例 #2
0
    def __init__(self):
        super(VerticalStack, self).__init__()

        columns = 5
        rows = 16

        ground = self.world.CreateStaticBody(shapes=[
            b2EdgeShape(vertices=[(-40, 0), (40, 0)]),
            b2EdgeShape(vertices=[(20, 0), (20, 20)]),
        ])

        box = b2FixtureDef(shape=b2PolygonShape(box=(0.5, 0.5)),
                           density=1,
                           friction=0.3)
        circle = b2FixtureDef(shape=b2CircleShape(radius=0.5),
                              density=1,
                              friction=0.3)

        box_start = -10
        box_space = 2.5
        circle_start = 8
        circle_space = 2.5
        for j in range(columns):
            for i in range(rows):
                self.world.CreateDynamicBody(
                    fixtures=box,
                    position=(box_start + box_space * j, 0.752 + 1.54 * i))
                self.world.CreateDynamicBody(
                    fixtures=circle,
                    position=(circle_start + circle_space * j,
                              0.752 + 1.54 * i))
コード例 #3
0
ファイル: bridge.py プロジェクト: aaronma37/pybox2d
    def __init__(self):
        super(Bridge, self).__init__()

        # The ground
        ground = self.world.CreateBody(
            shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)])
        )

        create_bridge(self.world, ground, (1.0, 0.25),
                      (-14.5, 5), self.numPlanks, 0.2, 20)

        fixture = b2FixtureDef(
            shape=b2PolygonShape(vertices=[(-0.5, 0.0),
                                           (0.5, 0.0),
                                           (0.0, 1.5),
                                           ]),
            density=1.0
        )
        for i in range(2):
            self.world.CreateDynamicBody(
                position=(-8 + 8 * i, 12),
                fixtures=fixture,
            )

        fixture = b2FixtureDef(shape=b2CircleShape(radius=0.5), density=1)
        for i in range(3):
            self.world.CreateDynamicBody(
                position=(-6 + 6 * i, 10),
                fixtures=fixture,
            )
コード例 #4
0
    def __init__(self):
        super(OneSidedPlatform, self).__init__()

        # The ground
        ground = self.world.CreateBody(shapes=b2EdgeShape(vertices=[(-20,
                                                                     0), (20,
                                                                          0)]))

        # The platform
        half_height = 0.5
        ypos = 10
        body = self.world.CreateBody(position=(0, ypos),
                                     shapes=b2PolygonShape(box=(3,
                                                                half_height)))
        self.platform = body.fixtures[0]

        # The circular character
        self.character_radius = 0.5
        body = self.world.CreateDynamicBody(
            position=(0, 12),
            fixtures=b2FixtureDef(
                shape=b2CircleShape(radius=self.character_radius),
                density=1.0),
        )

        self.character = body.fixtures[0]
        body.linearVelocity = (0, -50)

        self.bottom = ypos - half_height  # The bottom of the platform
        self.top = ypos + half_height  # The top of the platform
        self.state = 'unknown'
コード例 #5
0
    def __init__(self):
        super(RocketDemo, self).__init__()

        self.settings.drawJoints = False
        self.settings.drawMenu = False
        self.settings.drawStats = False
        self.settings.velocityIterations = 3
        self.settings.positionIterations = 1
        self.settings.hz = 20.0
        self.redrawInterval = 0

        # self.world.gravity = b2Vec2(0.0, 0.0)

        self.rockets = []
        # self.genetic = Genetic(
        #     lambda chromosomes: self.startEpoch(chromosomes),
        #     PControlChromosome)

        self.genetic = IslandGenetic(
            lambda chromosomes: self.startEpoch(chromosomes),
            IslandChromosome) # THIS IS FOR ISLAND GENETIC ALGORITHM

        self.world.CreateBody(
            shapes=b2EdgeShape(vertices=[(-20, -2), (20, -2)]))
        self.target = self.createTarget((0, 60))
コード例 #6
0
ファイル: conveyor_belt.py プロジェクト: aaronma37/pybox2d
    def __init__(self):
        Framework.__init__(self)

        self.using_contacts = True
        ground = self.world.CreateStaticBody(
            shapes=[b2EdgeShape(vertices=[(-20, 0), (20, 0)])],
        )

        # Platform
        self.platform = self.world.CreateStaticBody(
            position=(-5, 5),
            allowSleep=False,
            fixtures=b2FixtureDef(friction=0.8,
                                  shape=b2PolygonShape(box=(10.0, 5.0)),),
        )

        self.platform_fixture = self.platform.fixtures[0]

        # Boxes
        for i in range(5):
            self.platform = self.world.CreateDynamicBody(
                position=(-10.0 + 2.0 * i, 7.0),
                fixtures=b2FixtureDef(density=20.0,
                                      shape=b2PolygonShape(box=(0.5, 0.5)),),
            )
コード例 #7
0
    def __init__(self):
        super(Bridge, self).__init__()

        # The ground
        ground = self.world.CreateBody(shapes=b2EdgeShape(vertices=[(-40,
                                                                     0), (40,
                                                                          0)]))

        create_bridge(self.world, ground, (1.0, 0.25), (-14.5, 5),
                      self.numPlanks, 0.2, 20)

        fixture = b2FixtureDef(shape=b2PolygonShape(vertices=[
            (-0.5, 0.0),
            (0.5, 0.0),
            (0.0, 1.5),
        ]),
                               density=1.0)
        for i in range(2):
            self.world.CreateDynamicBody(
                position=(-8 + 8 * i, 12),
                fixtures=fixture,
            )

        fixture = b2FixtureDef(shape=b2CircleShape(radius=0.5), density=1)
        for i in range(3):
            self.world.CreateDynamicBody(
                position=(-6 + 6 * i, 10),
                fixtures=fixture,
            )
コード例 #8
0
    def __init__(self):
        super(OneSidedPlatform, self).__init__()

        # The ground
        ground = self.world.CreateBody(
            shapes=b2EdgeShape(vertices=[(-20, 0), (20, 0)])
        )

        # The platform
        half_height = 0.5
        ypos = 10
        body = self.world.CreateBody(
            position=(0, ypos),
            shapes=b2PolygonShape(box=(3, half_height))
        )
        self.platform = body.fixtures[0]

        # The circular character
        self.character_radius = 0.5
        body = self.world.CreateDynamicBody(
            position=(0, 12),
            fixtures=b2FixtureDef(shape=b2CircleShape(
                radius=self.character_radius), density=1.0),
        )

        self.character = body.fixtures[0]
        body.linearVelocity = (0, -50)

        self.bottom = ypos - half_height  # The bottom of the platform
        self.top = ypos + half_height    # The top of the platform
        self.state = 'unknown'
コード例 #9
0
    def createWorld(self):
        self._isLiving = True
        self._auto = True
        self._pid_control = PIDControl(1, 0.05, 0.1)  # kp, ki, kd

        self.ground = self.world.CreateBody(
            shapes=b2EdgeShape(vertices=[(-25, 0), (25, 0)])
        )

        self.carBody = self.world.CreateDynamicBody(
            position=(0, 3),
            fixtures=b2FixtureDef(
                shape=b2PolygonShape(box=(5, 1)), density=1)

        )

        self.carLwheel = self.world.CreateDynamicBody(
            position=(-3, 1),
            fixtures=b2FixtureDef(
                shape=b2CircleShape(radius=1), density=2, friction=1)

        )

        self.carRwheel = self.world.CreateDynamicBody(
            position=(3, 1),
            fixtures=b2FixtureDef(
                shape=b2CircleShape(radius=1), density=2, friction=1)

        )

        self.pendulum = self.world.CreateDynamicBody(
            position=(0, 13),
            fixtures=b2FixtureDef(
                shape=b2PolygonShape(box=(0.5, 10)), density=1),

        )

        self.pendelumJoin = self.world.CreateRevoluteJoint(
            bodyA=self.carBody,
            bodyB=self.pendulum,
            anchor=(0, 3),
            maxMotorTorque=1,
            enableMotor=True
        )

        self.pendelumRJoin =self.world.CreateRevoluteJoint(
            bodyA=self.carBody,
            bodyB=self.carRwheel,
            anchor=(3, 1),
            maxMotorTorque=1,
            enableMotor=True,
        )

        self.pendelumLJoin = self.world.CreateRevoluteJoint(
            bodyA=self.carBody,
            bodyB=self.carLwheel,
            anchor=(-3, 1),
            maxMotorTorque=1,
            enableMotor=True,
        )
コード例 #10
0
    def __init__(self):
        Framework.__init__(self)

        self.using_contacts = True
        ground = self.world.CreateStaticBody(
            shapes=[b2EdgeShape(vertices=[(-20, 0), (20, 0)])], )

        # Platform
        self.platform = self.world.CreateStaticBody(
            position=(-5, 5),
            allowSleep=False,
            fixtures=b2FixtureDef(
                friction=0.8,
                shape=b2PolygonShape(box=(10.0, 5.0)),
            ),
        )

        self.platform_fixture = self.platform.fixtures[0]

        # Boxes
        for i in range(5):
            self.platform = self.world.CreateDynamicBody(
                position=(-10.0 + 2.0 * i, 7.0),
                fixtures=b2FixtureDef(
                    density=20.0,
                    shape=b2PolygonShape(box=(0.5, 0.5)),
                ),
            )
コード例 #11
0
ファイル: raycast.py プロジェクト: dementrock/pybox2d
    def __init__(self):
        super(Raycast, self).__init__()

        self.world.gravity = (0, 0)
        # The ground
        ground = self.world.CreateBody(shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)]))

        # The various shapes
        w = 1.0
        b = w / (2.0 + sqrt(2.0))
        s = sqrt(2.0) * b

        self.shapes = [
            b2PolygonShape(vertices=[(-0.5, 0), (0.5, 0), (0, 1.5)]),
            b2PolygonShape(vertices=[(-0.1, 0), (0.1, 0), (0, 1.5)]),
            b2PolygonShape(
                vertices=[
                    (0.5 * s, 0),
                    (0.5 * w, b),
                    (0.5 * w, b + s),
                    (0.5 * s, w),
                    (-0.5 * s, w),
                    (-0.5 * w, b + s),
                    (-0.5 * w, b),
                    (-0.5 * s, 0.0),
                ]
            ),
            b2PolygonShape(box=(0.5, 0.5)),
            b2CircleShape(radius=0.5),
        ]
        self.angle = 0

        self.callbacks = [RayCastClosestCallback, RayCastAnyCallback, RayCastMultipleCallback]
        self.callback_class = self.callbacks[0]
コード例 #12
0
    def __init__(self):
        super(Pyramid, self).__init__()
        # The ground
        ground = self.world.CreateStaticBody(shapes=b2EdgeShape(
            vertices=[(-40, 0), (40, 0)]))

        box_half_size = (0.5, 0.5)
        box_density = 5.0
        box_rows = 20

        x = b2Vec2(-7, 0.75)
        deltaX = (0.5625, 1.25)
        deltaY = (1.125, 0)

        for i in range(box_rows):
            y = x.copy()

            for j in range(i, box_rows):
                self.world.CreateDynamicBody(
                    position=y,
                    fixtures=b2FixtureDef(
                        shape=b2PolygonShape(box=box_half_size),
                        density=box_density))

                y += deltaY

            x += deltaX
コード例 #13
0
ファイル: pyramid.py プロジェクト: aaronma37/pybox2d
    def __init__(self):
        super(Pyramid, self).__init__()
        # The ground
        ground = self.world.CreateStaticBody(
            shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)])
        )

        box_half_size = (0.5, 0.5)
        box_density = 5.0
        box_rows = 20

        x = b2Vec2(-7, 0.75)
        deltaX = (0.5625, 1.25)
        deltaY = (1.125, 0)

        for i in range(box_rows):
            y = x.copy()

            for j in range(i, box_rows):
                self.world.CreateDynamicBody(
                    position=y,
                    fixtures=b2FixtureDef(
                        shape=b2PolygonShape(box=box_half_size),
                        density=box_density)
                )

                y += deltaY

            x += deltaX
コード例 #14
0
    def __init__(self):
        super(Spring, self).__init__()

        ground = self.world.CreateStaticBody(
            position=(0, 0),
            shapes=[b2EdgeShape(vertices=[(-50, 0), (50, 0)])],
        )

        self.bodyA = self.world.CreateDynamicBody(
            position=(0, 10),
            fixtures=b2FixtureDef(
                shape=b2PolygonShape(box=(1, 1)), density=1.0)
        )

        self.bodyB = self.world.CreateDynamicBody(
            position=(0, 5),
            fixtures=b2FixtureDef(
                shape=b2CircleShape(radius = 0.5), density=1.0, restitution=0)
        )

        dfn = b2DistanceJointDef(
            frequencyHz=2.5,
            dampingRatio=0,
            bodyA=self.bodyA,
            bodyB=self.bodyB,
            anchorA=(0, 10),
            anchorB=(0, 5),
            collideConnected=True
        )

        self.world.CreateJoint(dfn)
コード例 #15
0
    def __init__(self):
        super(BodyTypes, self).__init__()

        # The ground
        ground = self.world.CreateBody(
            shapes=b2EdgeShape(vertices=[(-20, 0), (20, 0)])
        )

        # The attachment
        self.attachment = self.world.CreateDynamicBody(
            position=(0, 3),
            fixtures=b2FixtureDef(
                shape=b2PolygonShape(box=(0.5, 2)), density=2.0),
        )

        # The platform
        fixture = b2FixtureDef(
            shape=b2PolygonShape(box=(4, 0.5)),
            density=2,
            friction=0.6,
        )

        self.platform = self.world.CreateDynamicBody(
            position=(0, 5),
            fixtures=fixture,
        )

        # The joints joining the attachment/platform and ground/platform
        self.world.CreateRevoluteJoint(
            bodyA=self.attachment,
            bodyB=self.platform,
            anchor=(0, 5),
            maxMotorTorque=50,
            enableMotor=True
        )

        self.world.CreatePrismaticJoint(
            bodyA=ground,
            bodyB=self.platform,
            anchor=(0, 5),
            axis=(1, 0),
            maxMotorForce=1000,
            enableMotor=True,
            lowerTranslation=-10,
            upperTranslation=10,
            enableLimit=True
        )

        # And the payload that initially sits upon the platform
        # Reusing the fixture we previously defined above
        fixture.shape.box = (0.75, 0.75)
        self.payload = self.world.CreateDynamicBody(
            position=(0, 8),
            fixtures=fixture,
        )
コード例 #16
0
ファイル: rope.py プロジェクト: aaronma37/pybox2d
    def __init__(self):
        super(Rope, self).__init__()

        # The ground
        ground = self.world.CreateBody(
            shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)]))

        shape = b2PolygonShape(box=(0.5, 0.125))
        fd = b2FixtureDef(
            shape=shape,
            friction=0.2,
            density=20,
            categoryBits=0x0001,
            maskBits=(0xFFFF & ~0x0002),
        )

        N = 10
        y = 15.0

        prevBody = ground
        for i in range(N):
            if i < N - 1:
                body = self.world.CreateDynamicBody(
                    position=(0.5 + i, y),
                    fixtures=fd,
                )
            else:
                shape.box = (1.5, 1.5)
                fd.density = 100
                fd.categoryBits = 0x0002
                body = self.world.CreateDynamicBody(
                    position=(i, y),
                    fixtures=fd,
                    angularDamping=0.4,
                )

            self.world.CreateRevoluteJoint(
                bodyA=prevBody,
                bodyB=body,
                anchor=(i, y),
                collideConnected=False,
            )

            prevBody = body

        extraLength = 0.01
        self.rd = rd = b2RopeJointDef(
            bodyA=ground,
            bodyB=body,
            maxLength=N - 1.0 + extraLength,
            localAnchorA=(0, y),
            localAnchorB=(0, 0)
        )
        self.rope = self.world.CreateJoint(rd)
コード例 #17
0
    def __init__(self):
        super(Tower, self).__init__()

        #Ground
        ground = self.world.CreateStaticBody(
            position=(0, 0),
            shapes=[b2EdgeShape(vertices=[(-30, 0), (30, 0)])]  #,
            ##            b2PolygonShape(box=(0.2, 5, (0.5, 5),0))]
        )

        ##        self.world.CreateStaticBody(
        ##            shapes=[b2PolygonShape(box=[1, 1, (5, 15), 0]),
        ##                    b2PolygonShape(box=[1, 1, (-2, 12), 0]),
        ##                    ]
        ##        )

        self._x = 0

        ##        self.body = self.world.CreateDynamicBody(
        ##            position=(0.5, 10),
        ##            fixtures=b2FixtureDef(
        ##                shape=b2PolygonShape(box=(4, 0.1)), density=1.0),
        ##        )

        # Small circle
        #circle = b2FixtureDef(
        #shape=b2CircleShape(radius=0.25),
        #density=100.0,
        #)

        # Generate bullet (circle)
        #self.bullet = self.world.CreateBody(
        #position=(self._x, 30),
        #bullet=True,
        #type=b2_dynamicBody,
        #fixtures=circle,
        #linearVelocity=(0, -50)
        #)

        self.stack1 = self.world.CreateDynamicBody(
            position=(self._x, 0),
            ##            bullet=True,
            type=b2_dynamicBody,
            fixtures=b2FixtureDef(shape=b2PolygonShape(box=(5, 7)),
                                  density=100.0),
            linearVelocity=(0, -50))

        self.stack2 = self.world.CreateDynamicBody(
            position=(self._x, 0),
            ##            bullet=True,
            type=b2_dynamicBody,
            fixtures=b2FixtureDef(shape=b2PolygonShape(box=(4, 4)),
                                  density=100.0),
            linearVelocity=(0, -50))
コード例 #18
0
    def __init__(self):
        super(Rope, self).__init__()

        # The ground
        ground = self.world.CreateBody(shapes=b2EdgeShape(vertices=[(-40,
                                                                     0), (40,
                                                                          0)]))

        shape = b2PolygonShape(box=(0.5, 0.125))
        fd = b2FixtureDef(
            shape=shape,
            friction=0.2,
            density=20,
            categoryBits=0x0001,
            maskBits=(0xFFFF & ~0x0002),
        )

        N = 10
        y = 15.0

        prevBody = ground
        for i in range(N):
            if i < N - 1:
                body = self.world.CreateDynamicBody(
                    position=(0.5 + i, y),
                    fixtures=fd,
                )
            else:
                shape.box = (1.5, 1.5)
                fd.density = 100
                fd.categoryBits = 0x0002
                body = self.world.CreateDynamicBody(
                    position=(i, y),
                    fixtures=fd,
                    angularDamping=0.4,
                )

            self.world.CreateRevoluteJoint(
                bodyA=prevBody,
                bodyB=body,
                anchor=(i, y),
                collideConnected=False,
            )

            prevBody = body

        extraLength = 0.01
        self.rd = rd = b2RopeJointDef(bodyA=ground,
                                      bodyB=body,
                                      maxLength=N - 1.0 + extraLength,
                                      localAnchorA=(0, y),
                                      localAnchorB=(0, 0))
        self.rope = self.world.CreateJoint(rd)
コード例 #19
0
def gen_terrain():
    old_height = 0
    shapes, terrain_spec = [], []
    for i in range(0, 800, 10):
        height = random() * i / 20
        terrain_spec.append((-10 + i, old_height))
        terrain_spec.append((i, height))
        (-10 + i, old_height), (i, height)
        shapes.append(
            b2EdgeShape(vertices=[(-10 + i, old_height), (i, height)]))
        old_height = height
    return shapes, terrain_spec
コード例 #20
0
ファイル: edge_test.py プロジェクト: aaronma37/pybox2d
    def __init__(self):
        super(EdgeTest, self).__init__()

        v1 = (-10.0, 0.0)
        v2 = (-7.0, -1.0)
        v3 = (-4.0, 0.0)
        v4 = (0.0, 0.0)
        v5 = (4.0, 0.0)
        v6 = (7.0, 1.0)
        v7 = (10.0, 0.0)

        shapes = [b2EdgeShape(vertices=[None, v1, v2, v3]),
                  b2EdgeShape(vertices=[v1, v2, v3, v4]),
                  b2EdgeShape(vertices=[v2, v3, v4, v5]),
                  b2EdgeShape(vertices=[v3, v4, v5, v6]),
                  b2EdgeShape(vertices=[v4, v5, v6, v7]),
                  b2EdgeShape(vertices=[v5, v6, v7])
                  ]
        ground = self.world.CreateStaticBody(shapes=shapes)

        box = self.world.CreateDynamicBody(
            position=(0.5, 0.6),
            allowSleep=False,
            shapes=b2PolygonShape(box=(0.5, 0.5))
        )
コード例 #21
0
    def __init__(self):
        super(EdgeTest, self).__init__()

        v1 = (-10.0, 0.0)
        v2 = (-7.0, -1.0)
        v3 = (-4.0, 0.0)
        v4 = (0.0, 0.0)
        v5 = (4.0, 0.0)
        v6 = (7.0, 1.0)
        v7 = (10.0, 0.0)

        shapes = [b2EdgeShape(vertices=[None, v1, v2, v3]),
                  b2EdgeShape(vertices=[v1, v2, v3, v4]),
                  b2EdgeShape(vertices=[v2, v3, v4, v5]),
                  b2EdgeShape(vertices=[v3, v4, v5, v6]),
                  b2EdgeShape(vertices=[v4, v5, v6, v7]),
                  b2EdgeShape(vertices=[v5, v6, v7])
                  ]
        ground = self.world.CreateStaticBody(shapes=shapes)

        box = self.world.CreateDynamicBody(
            position=(0.5, 0.6),
            allowSleep=False,
            shapes=b2PolygonShape(box=(0.5, 0.5))
        )
コード例 #22
0
    def __init__(self):
        super(Confined, self).__init__()

        # The ground
        ground = self.world.CreateStaticBody(shapes=[
            b2EdgeShape(vertices=[(-10, 0), (10, 0)]),
            b2EdgeShape(vertices=[(-10, 0), (-10, 20)]),
            b2EdgeShape(vertices=[(10, 0), (10, 20)]),
            b2EdgeShape(vertices=[(-10, 20), (10, 20)]),
        ])

        # The bodies
        self.radius = radius = 0.5
        columnCount = 5
        rowCount = 5

        for j in range(columnCount):
            for i in range(rowCount):
                self.CreateCircle((-10 + (2.1 * j + 1 + 0.01 * i) * radius,
                                   (2 * i + 1) * radius))

        self.world.gravity = (0, 0)
コード例 #23
0
ファイル: confined.py プロジェクト: aaronma37/pybox2d
    def __init__(self):
        super(Confined, self).__init__()

        # The ground
        ground = self.world.CreateStaticBody(
            shapes=[b2EdgeShape(vertices=[(-10, 0), (10, 0)]),
                    b2EdgeShape(vertices=[(-10, 0), (-10, 20)]),
                    b2EdgeShape(vertices=[(10, 0), (10, 20)]),
                    b2EdgeShape(vertices=[(-10, 20), (10, 20)]),
                    ])

        # The bodies
        self.radius = radius = 0.5
        columnCount = 5
        rowCount = 5

        for j in range(columnCount):
            for i in range(rowCount):
                self.CreateCircle((-10 + (2.1 * j + 1 + 0.01 * i) * radius,
                                  (2 * i + 1) * radius))

        self.world.gravity = (0, 0)
コード例 #24
0
    def __init__(self):
        super(fallingObjects, self).__init__()

        #Groud
        ground = self.world.CreateStaticBody(
            position=(0, 0),
            shapes=[
                b2EdgeShape(vertices=[(-30, 0), (30, 0)]),
                b2PolygonShape(box=(0.2, 5, (0.5, 5), 0))
            ])

        self.world.CreateStaticBody(shapes=[
            b2PolygonShape(box=[1, 1, (5, 15), 0]),
            b2PolygonShape(box=[1, 1, (-2, 12), 0]),
        ])

        self._x = 0.20352793
        self.body = self.world.CreateDynamicBody(
            position=(0.5, 10),
            fixtures=b2FixtureDef(shape=b2PolygonShape(box=(4, 0.1)),
                                  density=1.0),
        )

        # Small circle
        #circle = b2FixtureDef(
        #shape=b2CircleShape(radius=0.25),
        #density=100.0,
        #)

        # Generate bullet (circle)
        #self.bullet = self.world.CreateBody(
        #position=(self._x, 30),
        #bullet=True,
        #type=b2_dynamicBody,
        #fixtures=circle,
        #linearVelocity=(0, -50)
        #)

        self.bullet = self.world.CreateDynamicBody(
            position=(self._x, 30),
            bullet=True,
            fixtures=b2FixtureDef(shape=b2PolygonShape(box=(0.25, 0.25)),
                                  density=100.0),
            linearVelocity=(0, -50))

        self.bullet1 = self.world.CreateDynamicBody(
            position=(self._x, 30),
            bullet=True,
            fixtures=b2FixtureDef(shape=b2PolygonShape(box=(0.25, 0.25)),
                                  density=100.0),
            linearVelocity=(0, -50))
コード例 #25
0
ファイル: vertical_stack.py プロジェクト: dementrock/pybox2d
    def __init__(self):
        super(VerticalStack, self).__init__()

        columns = 5
        rows = 16

        ground = self.world.CreateStaticBody(
            shapes=[b2EdgeShape(vertices=[(-40, 0), (40, 0)]), b2EdgeShape(vertices=[(20, 0), (20, 20)])]
        )

        box = b2FixtureDef(shape=b2PolygonShape(box=(0.5, 0.5)), density=1, friction=0.3)
        circle = b2FixtureDef(shape=b2CircleShape(radius=0.5), density=1, friction=0.3)

        box_start = -10
        box_space = 2.5
        circle_start = 8
        circle_space = 2.5
        for j in range(columns):
            for i in range(rows):
                self.world.CreateDynamicBody(fixtures=box, position=(box_start + box_space * j, 0.752 + 1.54 * i))
                self.world.CreateDynamicBody(
                    fixtures=circle, position=(circle_start + circle_space * j, 0.752 + 1.54 * i)
                )
コード例 #26
0
ファイル: mobile.py プロジェクト: aaronma37/pybox2d
    def __init__(self):
        Framework.__init__(self)

        ground = self.world.CreateStaticBody(
            position=(0, 20),
            shapes=[b2EdgeShape(vertices=[(-20, 0), (20, 0)])],
        )

        a = 0.5
        depth = 0
        self.root = self.add_node(ground, (0, 0), depth, 3.0, a)

        self.world.CreateRevoluteJoint(bodyA=ground, bodyB=self.root,
                                       localAnchorA=(0, 0), localAnchorB=(0, a))
コード例 #27
0
    def __init__(self):
        super(BoxCutter, self).__init__()
        # The ground
        self.ground = self.world.CreateStaticBody(
            userData='ground',
            shapes=[
                b2EdgeShape(vertices=[(-50, 0), (50, 0)]),
                b2EdgeShape(vertices=[(-50, 0), (-50, 10)]),
                b2EdgeShape(vertices=[(50, 0), (50, 10)]),
            ])

        self.laser_body = self.world.CreateDynamicBody(
            userData='laser',
            position=(0, 2),
            fixtures=b2FixtureDef(density=4.0,
                                  shape=b2PolygonShape(box=(LASER_HALF_WIDTH,
                                                            1))))

        for i in range(2):
            self.world.CreateDynamicBody(userData=LASER_SPLIT_TAG,
                                         position=(3.0 + i * 6, 8),
                                         fixtures=b2FixtureDef(
                                             density=5.0,
                                             shape=b2PolygonShape(box=(3, 3))))
コード例 #28
0
    def __init__(self):
        super(EdgeShapes, self).__init__()
        self.ground = self.world.CreateStaticBody(
            shapes=[b2EdgeShape(vertices=v)
                    for v in get_sinusoid_vertices(-20.0, VERTEX_COUNT)])

        self.shapes = [
            b2PolygonShape(vertices=[(-0.5, 0), (0.5, 0), (0, 1.5)]),
            b2PolygonShape(vertices=[(-0.1, 0), (0.1, 0), (0, 1.5)]),
            b2PolygonShape(vertices=get_octagon_vertices(1.0)),
            b2PolygonShape(box=(0.5, 0.5)),
            b2CircleShape(radius=0.5),
        ]

        self.angle = 0
        self.callback = RayCastCallback()
コード例 #29
0
    def __init__(self):
        Framework.__init__(self)

        ground = self.world.CreateStaticBody(
            position=(0, 20),
            shapes=[b2EdgeShape(vertices=[(-20, 0), (20, 0)])],
        )

        a = 0.5
        depth = 0
        self.root = self.add_node(ground, (0, 0), depth, 3.0, a)

        self.world.CreateRevoluteJoint(bodyA=ground,
                                       bodyB=self.root,
                                       localAnchorA=(0, 0),
                                       localAnchorB=(0, a))
コード例 #30
0
    def __init__(self, num_goals):
        super(SimpleHA2D, self).__init__()
        self.world.gravity = (
            0, 0)  #So that this is a flat 2D world with no grvity.
        self.num_goals = num_goals
        self.autonomy_goals = []
        self.human_goals = []
        self.autonomy_velocity = b2Vec2(0.0, 0.0)
        self.human_velocity = b2Vec2(0.0, 0.0)

        ground = self.world.CreateStaticBody(
            position=(0, 0),
            shapes=[b2EdgeShape(vertices=[(0, 35), (0, -35)])])

        self.autonomy_robot = Robot(self.world, position=(5, 9), radius=1)
        self.human_robot = Robot(self.world, position=(-5, 9), radius=1)
コード例 #31
0
    def __init__(self):
        super(Restitution, self).__init__()

        # The ground
        ground = self.world.CreateStaticBody(shapes=b2EdgeShape(
            vertices=[(-20, 0), (20, 0)]))

        radius = 1.0
        density = 1.0
        # The bodies
        for i, restitution in enumerate([0.0, 0.1, 0.3, 0.5, 0.75, 0.9, 1.0]):
            self.world.CreateDynamicBody(
                position=(-10 + 3.0 * i, 20),
                fixtures=b2FixtureDef(shape=b2CircleShape(radius=radius),
                                      density=density,
                                      restitution=restitution))
コード例 #32
0
def create_track(world):
    ground = world.CreateStaticBody(
        shapes=b2EdgeShape(vertices=[(-20, 1000), (-19, 0)]), userData="track")
    x, y1, dx = -19, 0, 5
    vertices = [
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2.5, 1.5, 0, 0, 2, 5, 9, 7, 5,
        0, -3, -2, 0, 3, 6, -10, 0, 0, 0, 1, 2, 3, 1, 0, 0, 1000
    ]
    for y2 in vertices:  # iterate through vertices twice
        ground.CreateEdgeFixture(
            vertices=[(x, y1), (x + dx, y2)],
            density=0,
            friction=100,
        )
        y1 = y2
        x += dx
コード例 #33
0
ファイル: edge_shapes.py プロジェクト: dementrock/pybox2d
    def __init__(self):
        super(EdgeShapes, self).__init__()
        self.ground = self.world.CreateStaticBody(
            shapes=[b2EdgeShape(vertices=v) for v in get_sinusoid_vertices(-20.0, VERTEX_COUNT)]
        )

        self.shapes = [
            b2PolygonShape(vertices=[(-0.5, 0), (0.5, 0), (0, 1.5)]),
            b2PolygonShape(vertices=[(-0.1, 0), (0.1, 0), (0, 1.5)]),
            b2PolygonShape(vertices=get_octagon_vertices(1.0)),
            b2PolygonShape(box=(0.5, 0.5)),
            b2CircleShape(radius=0.5),
        ]

        self.angle = 0
        self.callback = RayCastCallback()
コード例 #34
0
    def __init__(self):
        super(Breakable, self).__init__()

        # The ground
        self.world.CreateBody(shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)]))

        # The breakable body
        self.shapes = (b2PolygonShape(box=(0.5, 0.5, (-0.5, 0), 0)),
                       b2PolygonShape(box=(0.5, 0.5, (0.5, 0), 0)))
        self.body = self.world.CreateDynamicBody(
            position=(0, 40),
            angle=0.25 * b2_pi,
            shapes=self.shapes,
            shapeFixture=b2FixtureDef(density=1),
        )

        self.fixtures = self.body.fixtures
コード例 #35
0
ファイル: web.py プロジェクト: liyaxi1996/PyBox2D-RLP
    def __init__(self):
        super(Web, self).__init__()
        self.Print("hashdfasj", (127, 127, 255))
        # The ground
        ground = self.world.CreateBody(shapes=b2EdgeShape(vertices=[(-40,
                                                                     0), (40,
                                                                          0)]))

        fixture = b2FixtureDef(shape=b2PolygonShape(box=(0.5, 0.5)),
                               density=5,
                               friction=0.2)

        self.bodies = [
            self.world.CreateDynamicBody(position=pos, fixtures=fixture)
            for pos in ((-5, 5), (5, 5), (5, 15), (-5, 15))
        ]

        bodies = self.bodies

        # Create the joints between each of the bodies and also the ground
        #         bodyA      bodyB   localAnchorA localAnchorB
        sets = [
            (ground, bodies[0], (-10, 0), (-0.5, -0.5)),
            (ground, bodies[1], (10, 0), (0.5, -0.5)),
            (ground, bodies[2], (10, 20), (0.5, 0.5)),
            (ground, bodies[3], (-10, 20), (-0.5, 0.5)),
            (bodies[0], bodies[1], (0.5, 0), (-0.5, 0)),
            (bodies[1], bodies[2], (0, 0.5), (0, -0.5)),
            (bodies[2], bodies[3], (-0.5, 0), (0.5, 0)),
            (bodies[3], bodies[0], (0, -0.5), (0, 0.5)),
        ]

        # We will define the positions in the local body coordinates, the length
        # will automatically be set by the __init__ of the b2DistanceJointDef
        self.joints = []
        for bodyA, bodyB, localAnchorA, localAnchorB in sets:
            dfn = b2DistanceJointDef(
                frequencyHz=4.0,
                dampingRatio=0.5,
                bodyA=bodyA,
                bodyB=bodyB,
                localAnchorA=localAnchorA,
                localAnchorB=localAnchorB,
            )
            self.joints.append(self.world.CreateJoint(dfn))
コード例 #36
0
    def __init__(self, name, creatures):
        Environment.name = name
        self.env = super(Environment, self).__init__()
        self.settings.drawJoints = False
        Environment.step_limit = STEP_LIMIT

        _ = self.world.CreateBody(shapes=b2EdgeShape(vertices=[(-1000,
                                                                -1), (1000,
                                                                      -1)]))
        Environment.description = []
        Environment.creature_bodies = {}
        for creature in creatures:
            body = {}

            for edge in creature.edges:
                vertex = creature.vertices[edge[0]], creature.vertices[edge[1]]
                point = line_to_rectangle(*vertex, THICKNESS)

                fixture = b2FixtureDef(
                    shape=b2PolygonShape(vertices=point),
                    density=DENSITY,
                    friction=FRICTION,
                )
                fixture.filter.groupIndex = -1
                body[tuple(edge)] = self.world.CreateDynamicBody(
                    fixtures=fixture, )

            for edge in creature.edges:
                adjacent = find_adjacent_edges(edge, creature.edges)
                for a_edge in adjacent:
                    anchor = int(creature.vertices[edge[0]][0]), int(
                        creature.vertices[edge[0]][1])
                    self.world.CreateRevoluteJoint(
                        bodyA=body[tuple(edge)],
                        bodyB=body[tuple(a_edge)],
                        anchor=anchor,
                        collideConnected=True,
                        motorSpeed=MOTOR_SPEED,
                        maxMotorTorque=MAX_MOTOR_TORQUE,
                        enableMotor=True,
                    )
            Environment.creature_bodies[creature.identity] = body[tuple(edge)]
        if len(creatures) == 1:
            Environment.description.append(
                f"Creature #{creatures[0].identity}")
コード例 #37
0
ファイル: breakable.py プロジェクト: aaronma37/pybox2d
    def __init__(self):
        super(Breakable, self).__init__()

        # The ground
        self.world.CreateBody(shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)]))

        # The breakable body
        self.shapes = (b2PolygonShape(box=(0.5, 0.5, (-0.5, 0), 0)),
                       b2PolygonShape(box=(0.5, 0.5, (0.5, 0), 0))
                       )
        self.body = self.world.CreateDynamicBody(
            position=(0, 40),
            angle=0.25 * b2_pi,
            shapes=self.shapes,
            shapeFixture=b2FixtureDef(density=1),
        )

        self.fixtures = self.body.fixtures
コード例 #38
0
ファイル: restitution.py プロジェクト: aaronma37/pybox2d
    def __init__(self):
        super(Restitution, self).__init__()

        # The ground
        ground = self.world.CreateStaticBody(
            shapes=b2EdgeShape(vertices=[(-20, 0), (20, 0)])
        )

        radius = 1.0
        density = 1.0
        # The bodies
        for i, restitution in enumerate([0.0, 0.1, 0.3, 0.5, 0.75, 0.9, 1.0]):
            self.world.CreateDynamicBody(
                position=(-10 + 3.0 * i, 20),
                fixtures=b2FixtureDef(
                    shape=b2CircleShape(radius=radius),
                    density=density, restitution=restitution)
            )
コード例 #39
0
    def __init__(self):
        super(randomBox, self).__init__()

        ground = self.world.CreateStaticBody(
            position=(0, 0),
            shapes=[b2EdgeShape(vertices=[(-50, 0), (50, 0)])])

        rect_density = 30.0
        struct_length = random.randint(1, 4)
        struct_height = random.randint(4, 8)

        for i in range(struct_length):
            for j in range(struct_height):
                self.world.CreateDynamicBody(
                    position=position_vectors(),
                    fixtures=b2FixtureDef(
                        shape=b2PolygonShape(box=box_sizes()),
                        density=rect_density))
コード例 #40
0
ファイル: web.py プロジェクト: aaronma37/pybox2d
    def __init__(self):
        super(Web, self).__init__()

        # The ground
        ground = self.world.CreateBody(
            shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)])
        )

        fixture = b2FixtureDef(shape=b2PolygonShape(box=(0.5, 0.5)),
                               density=5, friction=0.2)

        self.bodies = [self.world.CreateDynamicBody(
            position=pos,
            fixtures=fixture
        ) for pos in ((-5, 5), (5, 5), (5, 15), (-5, 15))]

        bodies = self.bodies

        # Create the joints between each of the bodies and also the ground
        #         bodyA      bodyB   localAnchorA localAnchorB
        sets = [(ground,    bodies[0], (-10, 0), (-0.5, -0.5)),
                (ground,    bodies[1], (10, 0),  (0.5, -0.5)),
                (ground,    bodies[2], (10, 20), (0.5, 0.5)),
                (ground,    bodies[3], (-10, 20), (-0.5, 0.5)),
                (bodies[0], bodies[1], (0.5, 0), (-0.5, 0)),
                (bodies[1], bodies[2], (0, 0.5), (0, -0.5)),
                (bodies[2], bodies[3], (-0.5, 0), (0.5, 0)),
                (bodies[3], bodies[0], (0, -0.5), (0, 0.5)),
                ]

        # We will define the positions in the local body coordinates, the length
        # will automatically be set by the __init__ of the b2DistanceJointDef
        self.joints = []
        for bodyA, bodyB, localAnchorA, localAnchorB in sets:
            dfn = b2DistanceJointDef(
                frequencyHz=4.0,
                dampingRatio=0.5,
                bodyA=bodyA,
                bodyB=bodyB,
                localAnchorA=localAnchorA,
                localAnchorB=localAnchorB,
            )
            self.joints.append(self.world.CreateJoint(dfn))
コード例 #41
0
ファイル: motor_joint.py プロジェクト: aaronma37/pybox2d
    def __init__(self):
        Framework.__init__(self)

        ground = self.world.CreateStaticBody(
            shapes=[b2EdgeShape(vertices=[(-20, 0), (20, 0)])],
        )

        # Define motorized body
        body = self.world.CreateDynamicBody(
            position=(0, 8),
            allowSleep=False,
            fixtures=b2FixtureDef(density=2.0, friction=0.6,
                                  shape=b2PolygonShape(box=(2.0, 0.5)),),
        )

        self.joint = self.world.CreateMotorJoint(bodyA=ground, bodyB=body,
                                                 maxForce=1000, maxTorque=1000)

        self.go = False
        self.time = 0.0
コード例 #42
0
    def __init__(self):
        Framework.__init__(self)

        ground = self.world.CreateStaticBody(
            shapes=[b2EdgeShape(vertices=[(-20, 0), (20, 0)])],
        )

        # Define motorized body
        body = self.world.CreateDynamicBody(
            position=(0, 8),
            allowSleep=False,
            fixtures=b2FixtureDef(density=2.0, friction=0.6,
                                  shape=b2PolygonShape(box=(2.0, 0.5)),),
        )

        self.joint = self.world.CreateMotorJoint(bodyA=ground, bodyB=body,
                                                 maxForce=1000, maxTorque=1000)

        self.go = False
        self.time = 0.0
コード例 #43
0
 def __createCollisionBody(self, pos, size, shape, ud = None):
     #collisionbody       
     body = self.mWorld.CreateDynamicBody(position = pos)
     
     if shape == EnemyShape.CIRCLE:
         shape = b2CircleShape(radius=size.x/2)
     elif shape == EnemyShape.POLYGON:
         shape = b2PolygonShape()
         shape.SetAsBox(size.x/2, size.y/2)
     elif shape == EnemyShape.LINE:
         shape = b2EdgeShape()
         shape.vertices = b2Vec2(-size.x / 2.0, -size.y / 2.0), b2Vec2(size.x / 2.0, size.y / 2.0)        
     
     fd = b2FixtureDef()
     fd.shape = shape
     fd.isSensor = True
     body.CreateFixture(fd)
     body.isbullet = False
     body.fixedRotation = True
     body.userData = self if ud == None else ud
     
     return body
コード例 #44
0
ファイル: bullet.py プロジェクト: aaronma37/pybox2d
    def __init__(self):
        super(Bullet, self).__init__()

        ground = self.world.CreateStaticBody(
            position=(0, 0),
            shapes=[b2EdgeShape(vertices=[(-10, 0), (10, 0)]),
                    b2PolygonShape(box=(0.2, 1, (0.5, 1), 0))]
        )

        self._x = 0.20352793
        self.body = self.world.CreateDynamicBody(
            position=(0, 4),
            fixtures=b2FixtureDef(
                shape=b2PolygonShape(box=(2, 0.1)), density=1.0),
        )

        self.bullet = self.world.CreateDynamicBody(
            position=(self._x, 10),
            bullet=True,
            fixtures=b2FixtureDef(shape=b2PolygonShape(
                box=(0.25, 0.25)), density=100.0),
            linearVelocity=(0, -50)
        )
コード例 #45
0
    def __init__(self):
        super(Bullet, self).__init__()

        ground = self.world.CreateStaticBody(
            position=(0, 0),
            shapes=[
                b2EdgeShape(vertices=[(-10, 0), (10, 0)]),
                b2PolygonShape(box=(0.2, 1, (0.5, 1), 0))
            ])

        self._x = 0.20352793
        self.body = self.world.CreateDynamicBody(
            position=(0, 4),
            fixtures=b2FixtureDef(shape=b2PolygonShape(box=(2, 0.1)),
                                  density=1.0),
        )

        self.bullet = self.world.CreateDynamicBody(
            position=(self._x, 10),
            bullet=True,
            fixtures=b2FixtureDef(shape=b2PolygonShape(box=(0.25, 0.25)),
                                  density=100.0),
            linearVelocity=(0, -50))
コード例 #46
0
ファイル: body_types.py プロジェクト: dementrock/pybox2d
    def __init__(self):
        super(BodyTypes, self).__init__()

        # The ground
        ground = self.world.CreateBody(shapes=b2EdgeShape(vertices=[(-20, 0), (20, 0)]))

        # The attachment
        self.attachment = self.world.CreateDynamicBody(
            position=(0, 3), fixtures=b2FixtureDef(shape=b2PolygonShape(box=(0.5, 2)), density=2.0)
        )

        # The platform
        fixture = b2FixtureDef(shape=b2PolygonShape(box=(4, 0.5)), density=2, friction=0.6)

        self.platform = self.world.CreateDynamicBody(position=(0, 5), fixtures=fixture)

        # The joints joining the attachment/platform and ground/platform
        self.world.CreateRevoluteJoint(
            bodyA=self.attachment, bodyB=self.platform, anchor=(0, 5), maxMotorTorque=50, enableMotor=True
        )

        self.world.CreatePrismaticJoint(
            bodyA=ground,
            bodyB=self.platform,
            anchor=(0, 5),
            axis=(1, 0),
            maxMotorForce=1000,
            enableMotor=True,
            lowerTranslation=-10,
            upperTranslation=10,
            enableLimit=True,
        )

        # And the payload that initially sits upon the platform
        # Reusing the fixture we previously defined above
        fixture.shape.box = (0.75, 0.75)
        self.payload = self.world.CreateDynamicBody(position=(0, 8), fixtures=fixture)
コード例 #47
0
    def __init__(self):
        super(Raycast, self).__init__()

        self.world.gravity = (0, 0)
        # The ground
        ground = self.world.CreateBody(shapes=b2EdgeShape(vertices=[(-40,
                                                                     0), (40,
                                                                          0)]))

        # The various shapes
        w = 1.0
        b = w / (2.0 + sqrt(2.0))
        s = sqrt(2.0) * b

        self.shapes = [
            b2PolygonShape(vertices=[(-0.5, 0), (0.5, 0), (0, 1.5)]),
            b2PolygonShape(vertices=[(-0.1, 0), (0.1, 0), (0, 1.5)]),
            b2PolygonShape(
                vertices=[(0.5 * s,
                           0), (0.5 * w,
                                b), (0.5 * w,
                                     b + s), (0.5 * s,
                                              w), (-0.5 * s,
                                                   w), (-0.5 * w, b +
                                                        s), (-0.5 * w,
                                                             b), (-0.5 * s,
                                                                  0.0)]),
            b2PolygonShape(box=(0.5, 0.5)),
            b2CircleShape(radius=0.5),
        ]
        self.angle = 0

        self.callbacks = [
            RayCastClosestCallback, RayCastAnyCallback, RayCastMultipleCallback
        ]
        self.callback_class = self.callbacks[0]
コード例 #48
0
    def __init__(self, args=None):
        """
        Initialize all of your objects here.
        Be sure to call the Framework's initializer first.
        """
        if args == None:
            args = {
                "start_y": 10,
                "len_torso": 6,
                "len_crank_arm": 2,
                "len_leg": 14,
                "motor_torque": 400,
                "motor_speed": 5
            }
        super(Robot, self).__init__()
        self.count = 0
        self.points = []
        self.group = -1
        self.start_y = args["start_y"]
        self.len_torso = args["len_torso"]
        self.len_crank_arm = args["len_crank_arm"]
        self.len_leg = args["len_leg"]
        self.motor_torque = args["motor_torque"]
        self.motor_speed = args["motor_speed"]
        #Non controlled constants
        torso_width = .001
        other_width = .001
        leg_angle = numpy.arctan(self.len_torso / self.len_crank_arm)
        ground = self.world.CreateStaticBody(
            position=(0, 0),
            shapes=[b2EdgeShape(vertices=[(-1000, 0), (1000, 0)])])

        self.torso = self.world.CreateDynamicBody(
            position=(0, self.start_y),
            fixedRotation=False,
            fixtures=b2FixtureDef(
                shape=b2PolygonShape(box=(torso_width, self.len_torso / 2)),
                density=1.3,
                filter=b2Filter(groupIndex=self.group, )),
        )
        tail_angle = -numpy.pi / 3
        len_tail = .035
        self.tail = self.world.CreateDynamicBody(
            position=(len_tail * numpy.sin(tail_angle),
                      self.start_y - len_tail * numpy.cos(tail_angle)),
            fixtures=b2FixtureDef(shape=b2PolygonShape(box=(torso_width,
                                                            len_tail)),
                                  density=1,
                                  filter=b2Filter(groupIndex=self.group, )),
            angle=tail_angle,
        )

        self.crank_arm = self.world.CreateDynamicBody(
            position=(0, self.start_y + self.len_torso / 2),
            fixtures=b2FixtureDef(shape=b2PolygonShape(box=(self.len_crank_arm,
                                                            other_width)),
                                  density=.1,
                                  filter=b2Filter(groupIndex=self.group, )),
        )
        #Creating the legs
        l = numpy.sqrt(self.len_torso**2 + self.len_crank_arm**2)
        x = (self.len_leg - 2 * l) * numpy.cos(leg_angle) / 2
        y = (self.len_leg - 2 * l) * numpy.sin(leg_angle) / 2
        self.right_leg = self.world.CreateDynamicBody(
            position=(x, self.start_y - y - self.len_torso / 2),
            angle=(-1 * leg_angle + numpy.pi / 2),
            fixtures=b2FixtureDef(shape=b2PolygonShape(box=(other_width,
                                                            self.len_leg / 2)),
                                  density=.50,
                                  filter=b2Filter(groupIndex=self.group),
                                  friction=1,
                                  restitution=0),
        )
        self.left_leg = self.world.CreateDynamicBody(
            position=(-x, self.start_y - y - self.len_torso / 2),
            angle=(leg_angle + numpy.pi / 2),
            fixtures=b2FixtureDef(shape=b2PolygonShape(box=(other_width,
                                                            self.len_leg / 2)),
                                  density=1.0,
                                  filter=b2Filter(groupIndex=self.group),
                                  friction=1,
                                  restitution=0),
        )
        self.right_foot = self.world.CreateDynamicBody(
            position=(self.len_leg / 2 * numpy.cos(leg_angle),
                      self.len_leg * numpy.sin(leg_angle)),
            fixtures=b2FixtureDef(shape=b2CircleShape(radius=.003),
                                  density=0.1,
                                  filter=b2Filter(groupIndex=self.group, )),
        )
        self.left_foot = self.world.CreateDynamicBody(
            position=(-1 * self.len_leg / 2 * numpy.cos(leg_angle),
                      self.len_leg * numpy.sin(leg_angle)),
            fixtures=b2FixtureDef(shape=b2CircleShape(radius=.003),
                                  density=0.1,
                                  filter=b2Filter(groupIndex=self.group, )),
        )
        slot_joint_right = self.world.CreateDynamicBody(
            position=(0, self.torso.worldCenter[1] - self.len_torso / 2),
            angle=(-1 * leg_angle + numpy.pi / 2),
            fixtures=b2FixtureDef(shape=b2CircleShape(radius=.0005),
                                  density=1.0,
                                  filter=b2Filter(groupIndex=self.group, )),
        )
        slot_joint_left = self.world.CreateDynamicBody(
            position=(0, self.torso.worldCenter[1] - self.len_torso / 2),
            angle=(leg_angle + numpy.pi / 2),
            fixtures=b2FixtureDef(shape=b2CircleShape(radius=.0005),
                                  density=1.0,
                                  filter=b2Filter(groupIndex=self.group, )),
        )
        #Attach Feat to legs
        self.right_foot_weld = self.world.CreateWeldJoint(
            bodyA=self.right_foot,
            bodyB=self.right_leg,
            anchor=(self.torso.worldCenter[0] +
                    self.len_leg / 2 * numpy.cos(leg_angle),
                    self.torso.worldCenter[1] +
                    self.len_leg * numpy.sin(leg_angle)),
        )
        self.left_foot_weld = self.world.CreateWeldJoint(
            bodyA=self.left_foot,
            bodyB=self.left_leg,
            anchor=(self.torso.worldCenter[0] -
                    self.len_leg / 2 * numpy.cos(leg_angle),
                    self.torso.worldCenter[1] +
                    self.len_leg * numpy.sin(leg_angle)),
        )
        #Motor Joint
        self.motor = self.world.CreateRevoluteJoint(
            bodyA=self.torso,
            bodyB=self.crank_arm,
            anchor=(self.torso.worldCenter[0],
                    self.torso.worldCenter[1] + self.len_torso / 2),
            motorSpeed=self.motor_speed,
            maxMotorTorque=self.motor_torque,
            enableMotor=True,
        )
        #Joints at end of pivot
        self.right_joint = self.world.CreateRevoluteJoint(
            bodyA=self.crank_arm,
            bodyB=self.right_leg,
            anchor=(self.crank_arm.worldCenter[0] - self.len_crank_arm,
                    self.crank_arm.worldCenter[1]),
        )
        self.left_joint = self.world.CreateRevoluteJoint(
            bodyA=self.crank_arm,
            bodyB=self.left_leg,
            anchor=(self.crank_arm.worldCenter[0] + self.len_crank_arm,
                    self.crank_arm.worldCenter[1]),
        )
        #Making the slot joint composed of rev joint and prismatic joints
        self.left_joint_rev = self.world.CreateRevoluteJoint(
            bodyA=slot_joint_right,
            bodyB=self.torso,
            anchor=(0, self.start_y - self.len_torso / 2),
        )
        self.right_joint_rev = self.world.CreateRevoluteJoint(
            bodyA=slot_joint_left,
            bodyB=self.torso,
            anchor=(0, self.start_y - self.len_torso / 2),
        )
        self.right_slide_pris = self.world.CreatePrismaticJoint(
            bodyA=slot_joint_right,
            bodyB=self.right_leg,
            anchor=(self.torso.worldCenter[0],
                    self.torso.worldCenter[0] - self.len_torso / 2),
            localAxisA=(0, 1),
        )
        self.left_slide_pris = self.world.CreatePrismaticJoint(
            bodyA=slot_joint_left,
            bodyB=self.left_leg,
            anchor=(self.torso.worldCenter[0],
                    self.torso.worldCenter[0] - self.len_torso / 2),
            localAxisA=(0, 1),
        )
        self.tail_joint = self.world.CreateWeldJoint(
            bodyA=self.tail,
            bodyB=self.torso,
            anchor=(0, self.start_y - self.len_torso / 2),
        )
コード例 #49
0
    def __init__(self):
        super(Car, self).__init__()

        # The ground -- create some terrain
        ground = self.world.CreateStaticBody(shapes=b2EdgeShape(
            vertices=[(-20, 0), (20, 0)]))

        x, y1, dx = 20, 0, 5
        vertices = [0.25, 1, 4, 0, 0, -1, -2, -2, -1.25, 0]
        for y2 in vertices * 2:  # iterate through vertices twice
            ground.CreateEdgeFixture(
                vertices=[(x, y1), (x + dx, y2)],
                density=0,
                friction=0.6,
            )
            y1 = y2
            x += dx

        x_offsets = [0, 80, 40, 20, 40]
        x_lengths = [40, 40, 10, 40, 0]
        y2s = [0, 0, 5, 0, 20]

        for x_offset, x_length, y2 in zip(x_offsets, x_lengths, y2s):
            x += x_offset
            ground.CreateEdgeFixture(
                vertices=[(x, 0), (x + x_length, y2)],
                density=0,
                friction=0.6,
            )

        # Teeter
        body = self.world.CreateDynamicBody(
            position=(140, 0.90),
            fixtures=b2FixtureDef(
                shape=b2PolygonShape(box=(10, 0.25)),
                density=1.0,
            ))

        self.world.CreateRevoluteJoint(
            bodyA=ground,
            bodyB=body,
            anchor=body.position,
            lowerAngle=-8.0 * b2_pi / 180.0,
            upperAngle=8.0 * b2_pi / 180.0,
            enableLimit=True,
        )

        # Bridge
        create_bridge(self.world, ground, (2.0, 0.25), (161.0, -0.125),
                      self.bridgePlanks)

        # Boxes
        for y_pos in [0.5, 1.5, 2.5, 3.5, 4.5]:
            self.world.CreateDynamicBody(position=(230, y_pos),
                                         fixtures=b2FixtureDef(
                                             shape=b2PolygonShape(box=(0.5,
                                                                       0.5)),
                                             density=0.5,
                                         ))

        car, wheels, springs = create_car(self.world,
                                          offset=(0.0, 1.0),
                                          wheel_radius=0.4,
                                          wheel_separation=2.0,
                                          scale=(1, 1))
        self.car = car
        self.wheels = wheels
        self.springs = springs
コード例 #50
0
    def __init__(self):
        super(CollisionFiltering, self).__init__()
        # Ground body
        world = self.world
        ground = world.CreateBody(
            shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)])
        )

        # Define the groups that fixtures can fall into
        # Note that negative groups never collide with other negative ones.
        smallGroup = 1
        largeGroup = -1

        # And the categories
        # Note that these are bit-locations, and as such are written in
        # hexadecimal.
        # defaultCategory = 0x0001
        triangleCategory = 0x0002
        boxCategory = 0x0004
        circleCategory = 0x0008

        # And the masks that define which can hit one another
        # A mask of 0xFFFF means that it will collide with everything else in
        # its group. The box mask below uses an exclusive OR (XOR) which in
        # effect toggles the triangleCategory bit, making boxMask = 0xFFFD.
        # Such a mask means that boxes never collide with triangles.  (if
        # you're still confused, see the implementation details below)

        triangleMask = 0xFFFF
        boxMask = 0xFFFF ^ triangleCategory
        circleMask = 0xFFFF

        # The actual implementation determining whether or not two objects
        # collide is defined in the C++ source code, but it can be overridden
        # in Python (with b2ContactFilter).
        # The default behavior goes like this:
        #   if (filterA.groupIndex == filterB.groupIndex and filterA.groupIndex != 0):
        #       collide if filterA.groupIndex is greater than zero (negative groups never collide)
        #   else:
        #       collide if (filterA.maskBits & filterB.categoryBits) != 0 and (filterA.categoryBits & filterB.maskBits) != 0
        #
        # So, if they have the same group index (and that index isn't the
        # default 0), then they collide if the group index is > 0 (since
        # negative groups never collide)
        # (Note that a body with the default filter settings will always
        # collide with everything else.)
        # If their group indices differ, then only if their bitwise-ANDed
        # category and mask bits match up do they collide.
        #
        # For more help, some basics of bit masks might help:
        # -> http://en.wikipedia.org/wiki/Mask_%28computing%29

        # Small triangle
        triangle = b2FixtureDef(
            shape=b2PolygonShape(vertices=[(-1, 0), (1, 0), (0, 2)]),
            density=1,
            filter=b2Filter(
                groupIndex=smallGroup,
                categoryBits=triangleCategory,
                maskBits=triangleMask,
            )
        )

        world.CreateDynamicBody(
            position=(-5, 2),
            fixtures=triangle,
        )

        # Large triangle (recycle definitions)
        triangle.shape.vertices = [
            2.0 * b2Vec2(v) for v in triangle.shape.vertices]
        triangle.filter.groupIndex = largeGroup

        trianglebody = world.CreateDynamicBody(
            position=(-5, 6),
            fixtures=triangle,
            fixedRotation=True,  # <--
        )
        # note that the large triangle will not rotate

        # Small box
        box = b2FixtureDef(
            shape=b2PolygonShape(box=(1, 0.5)),
            density=1,
            restitution=0.1,
            filter = b2Filter(
                groupIndex=smallGroup,
                categoryBits=boxCategory,
                maskBits=boxMask,
            )
        )

        world.CreateDynamicBody(
            position=(0, 2),
            fixtures=box,
        )

        # Large box
        box.shape.box = (2, 1)
        box.filter.groupIndex = largeGroup
        world.CreateDynamicBody(
            position=(0, 6),
            fixtures=box,
        )

        # Small circle
        circle = b2FixtureDef(
            shape=b2CircleShape(radius=1),
            density=1,
            filter=b2Filter(
                groupIndex=smallGroup,
                categoryBits=circleCategory,
                maskBits=circleMask,
            )
        )

        world.CreateDynamicBody(
            position=(5, 2),
            fixtures=circle,
        )

        # Large circle
        circle.shape.radius *= 2
        circle.filter.groupIndex = largeGroup
        world.CreateDynamicBody(
            position=(5, 6),
            fixtures=circle,
        )

        # Create a joint for fun on the big triangle
        # Note that it does not inherit or have anything to do with the
        # filter settings of the attached triangle.
        box = b2FixtureDef(shape=b2PolygonShape(box=(0.5, 1)), density=1)

        testbody = world.CreateDynamicBody(
            position=(-5, 10),
            fixtures=box,
        )
        world.CreatePrismaticJoint(
            bodyA=trianglebody,
            bodyB=testbody,
            enableLimit=True,
            localAnchorA=(0, 4),
            localAnchorB=(0, 0),
            localAxisA=(0, 1),
            lowerTranslation=-1,
            upperTranslation=1,
        )
コード例 #51
0
    def __init__(self):
        super(CharacterCollision, self).__init__()

        ground = self.world.CreateStaticBody(
            position=(0, 0),
            shapes=b2EdgeShape(vertices=[(-20, 0), (20, 0)])
        )

        # Collinear edges
        self.world.CreateStaticBody(
            shapes=[b2EdgeShape(vertices=[(-8, 1), (-6, 1)]),
                    b2EdgeShape(vertices=[(-6, 1), (-4, 1)]),
                    b2EdgeShape(vertices=[(-4, 1), (-2, 1)]),
                    ]
        )

        # Square tiles
        self.world.CreateStaticBody(
            shapes=[b2PolygonShape(box=[1, 1, (4, 3), 0]),
                    b2PolygonShape(box=[1, 1, (6, 3), 0]),
                    b2PolygonShape(box=[1, 1, (8, 3), 0]),
                    ]
        )

        # Square made from an edge loop. Collision should be smooth.
        body = self.world.CreateStaticBody()
        body.CreateLoopFixture(vertices=[(-1, 3), (1, 3), (1, 5), (-1, 5)])

        # Edge loop.
        body = self.world.CreateStaticBody(position=(-10, 4))
        body.CreateLoopFixture(vertices=[
            (0.0, 0.0), (6.0, 0.0),
            (6.0, 2.0), (4.0, 1.0),
            (2.0, 2.0), (0.0, 2.0),
            (-2.0, 2.0), (-4.0, 3.0),
            (-6.0, 2.0), (-6.0, 0.0), ]
        )

        # Square character 1
        self.world.CreateDynamicBody(
            position=(-3, 8),
            fixedRotation=True,
            allowSleep=False,
            fixtures=b2FixtureDef(shape=b2PolygonShape(
                box=(0.5, 0.5)), density=20.0),
        )

        # Square character 2
        body = self.world.CreateDynamicBody(
            position=(-5, 5),
            fixedRotation=True,
            allowSleep=False,
        )

        body.CreatePolygonFixture(box=(0.25, 0.25), density=20.0)

        # Hexagon character
        a = b2_pi / 3.0
        self.world.CreateDynamicBody(
            position=(-5, 8),
            fixedRotation=True,
            allowSleep=False,
            fixtures=b2FixtureDef(
                shape=b2PolygonShape(
                    vertices=[(0.5 * cos(i * a), 0.5 * sin(i * a))
                              for i in range(6)]),
                density=20.0
            ),
        )

        # Circle character
        self.world.CreateDynamicBody(
            position=(3, 5),
            fixedRotation=True,
            allowSleep=False,
            fixtures=b2FixtureDef(
                shape=b2CircleShape(radius=0.5),
                density=20.0
            ),
        )
コード例 #52
0
ファイル: theo_jansen.py プロジェクト: aaronma37/pybox2d
    def __init__(self):
        super(TheoJansen, self).__init__()

        #
        ball_count = 40
        pivot = b2Vec2(0, 0.8)

        # The ground
        ground = self.world.CreateStaticBody(
            shapes=[
                b2EdgeShape(vertices=[(-50, 0), (50, 0)]),
                b2EdgeShape(vertices=[(-50, 0), (-50, 10)]),
                b2EdgeShape(vertices=[(50, 0), (50, 10)]),
            ]
        )

        box = b2FixtureDef(
            shape=b2PolygonShape(box=(0.5, 0.5)),
            density=1,
            friction=0.3)
        circle = b2FixtureDef(
            shape=b2CircleShape(radius=0.25),
            density=1)

        # Create the balls on the ground
        for i in range(ball_count):
            self.world.CreateDynamicBody(
                fixtures=circle,
                position=(-40 + 2.0 * i, 0.5),
            )

        # The chassis
        chassis_fixture = b2FixtureDef(
            shape=b2PolygonShape(box=(2.5, 1)),
            density=1,
            friction=0.3,
            groupIndex=-1)

        self.chassis = self.world.CreateDynamicBody(
            fixtures=chassis_fixture,
            position=pivot + self.offset)

        # Chassis wheel
        wheel_fixture = b2FixtureDef(
            shape=b2CircleShape(radius=1.6),
            density=1,
            friction=0.3,
            groupIndex=-1)

        self.wheel = self.world.CreateDynamicBody(
            fixtures=wheel_fixture,
            position=pivot + self.offset)

        # Add a joint between the chassis wheel and the chassis itself
        self.motorJoint = self.world.CreateRevoluteJoint(
            bodyA=self.wheel,
            bodyB=self.chassis,
            anchor=pivot + self.offset,
            collideConnected=False,
            motorSpeed=self.motorSpeed,
            maxMotorTorque=400,
            enableMotor=self.motorOn)

        wheelAnchor = pivot + (0, -0.8)
        self.CreateLeg(-1, wheelAnchor)
        self.CreateLeg(1, wheelAnchor)

        self.wheel.transform = (self.wheel.position, 120.0 * b2_pi / 180)
        self.CreateLeg(-1, wheelAnchor)
        self.CreateLeg(1, wheelAnchor)

        self.wheel.transform = (self.wheel.position, -120.0 * b2_pi / 180)
        self.CreateLeg(-1, wheelAnchor)
        self.CreateLeg(1, wheelAnchor)
コード例 #53
0
ファイル: car.py プロジェクト: aaronma37/pybox2d
    def __init__(self):
        super(Car, self).__init__()

        # The ground -- create some terrain
        ground = self.world.CreateStaticBody(
            shapes=b2EdgeShape(vertices=[(-20, 0), (20, 0)])
        )

        x, y1, dx = 20, 0, 5
        vertices = [0.25, 1, 4, 0, 0, -1, -2, -2, -1.25, 0]
        for y2 in vertices * 2:  # iterate through vertices twice
            ground.CreateEdgeFixture(
                vertices=[(x, y1), (x + dx, y2)],
                density=0,
                friction=0.6,
            )
            y1 = y2
            x += dx

        x_offsets = [0, 80, 40, 20, 40]
        x_lengths = [40, 40, 10, 40, 0]
        y2s = [0, 0, 5, 0, 20]

        for x_offset, x_length, y2 in zip(x_offsets, x_lengths, y2s):
            x += x_offset
            ground.CreateEdgeFixture(
                vertices=[(x, 0), (x + x_length, y2)],
                density=0,
                friction=0.6,
            )

        # Teeter
        body = self.world.CreateDynamicBody(
            position=(140, 0.90),
            fixtures=b2FixtureDef(
                shape=b2PolygonShape(box=(10, 0.25)),
                density=1.0,
            )
        )

        self.world.CreateRevoluteJoint(
            bodyA=ground,
            bodyB=body,
            anchor=body.position,
            lowerAngle=-8.0 * b2_pi / 180.0,
            upperAngle=8.0 * b2_pi / 180.0,
            enableLimit=True,
        )

        # Bridge
        create_bridge(self.world, ground, (2.0, 0.25),
                      (161.0, -0.125), self.bridgePlanks)

        # Boxes
        for y_pos in [0.5, 1.5, 2.5, 3.5, 4.5]:
            self.world.CreateDynamicBody(
                position=(230, y_pos),
                fixtures=b2FixtureDef(
                    shape=b2PolygonShape(box=(0.5, 0.5)),
                    density=0.5,
                )
            )

        car, wheels, springs = create_car(self.world, offset=(
            0.0, 1.0), wheel_radius=0.4, wheel_separation=2.0, scale=(1, 1))
        self.car = car
        self.wheels = wheels
        self.springs = springs
コード例 #54
0
    def __init__(self):
        super(CollisionProcessing, self).__init__()

        # Tell the framework we're going to use contacts, so keep track of them
        # every Step.
        self.using_contacts = True

        # Ground body
        world = self.world
        ground = world.CreateBody(
            shapes=b2EdgeShape(vertices=[(-50, 0), (50, 0)],)
        )

        xlow, xhi = -5, 5
        ylow, yhi = 2, 35
        random_vector = lambda: b2Vec2(
            b2Random(xlow, xhi), b2Random(ylow, yhi))

        # Small triangle
        triangle = b2FixtureDef(
            shape=b2PolygonShape(vertices=[(-1, 0), (1, 0), (0, 2)]),
            density=1,
        )

        world.CreateBody(
            type=b2_dynamicBody,
            position=random_vector(),
            fixtures=triangle,
        )

        # Large triangle (recycle definitions)
        triangle.shape.vertices = [
            2.0 * b2Vec2(v) for v in triangle.shape.vertices]

        tri_body = world.CreateBody(type=b2_dynamicBody,
                                    position=random_vector(),
                                    fixtures=triangle,
                                    fixedRotation=True,  # <--
                                    )
        # note that the large triangle will not rotate

        # Small box
        box = b2FixtureDef(
            shape=b2PolygonShape(box=(1, 0.5)),
            density=1,
            restitution=0.1,
        )

        world.CreateBody(
            type=b2_dynamicBody,
            position=random_vector(),
            fixtures=box,
        )

        # Large box
        box.shape.box = (2, 1)
        world.CreateBody(
            type=b2_dynamicBody,
            position=random_vector(),
            fixtures=box,
        )

        # Small circle
        circle = b2FixtureDef(
            shape=b2CircleShape(radius=1),
            density=1,
        )

        world.CreateBody(
            type=b2_dynamicBody,
            position=random_vector(),
            fixtures=circle,
        )

        # Large circle
        circle.shape.radius *= 2
        world.CreateBody(
            type=b2_dynamicBody,
            position=random_vector(),
            fixtures=circle,
        )
コード例 #55
0
ファイル: cantilever.py プロジェクト: aaronma37/pybox2d
    def __init__(self):
        super(Cantilever, self).__init__()

        # The ground
        ground = self.world.CreateBody(
            shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)]))

        plank = b2FixtureDef(
            shape=b2PolygonShape(box=(0.5, 0.125)),
            friction=0.2,
            density=20
        )

        # Create one cantilever (Only the left end is fixed)
        prevBody = ground
        for i in range(self.numPlanks):
            body = self.world.CreateDynamicBody(
                position=(-14.5 + i, 5),
                fixtures=plank,
            )

            self.world.CreateWeldJoint(
                bodyA=prevBody,
                bodyB=body,
                anchor=(-15 + i, 5),
            )

            prevBody = body

        # Create another higher up
        prevBody = ground
        for i in range(self.numPlanks):
            body = self.world.CreateDynamicBody(
                position=(-14.5 + i, 15),
                fixtures=plank,
            )

            self.world.CreateWeldJoint(
                bodyA=prevBody,
                bodyB=body,
                anchor=(-15 + i, 15),
            )

            prevBody = body

        # And the left-most unconnected one (technically not a cantilever)
        prevBody = ground
        for i in range(self.numPlanks):
            body = self.world.CreateDynamicBody(
                position=(-4.5 + i, 5),
                fixtures=plank,
            )

            if i > 0:  # skip the joint on the first one
                self.world.CreateWeldJoint(
                    bodyA=prevBody,
                    bodyB=body,
                    anchor=(-5 + i, 5),
                )

            prevBody = body

        # And the right-most unconnected one, using joint damping
        prevBody = ground
        for i in range(self.numPlanks):
            body = self.world.CreateDynamicBody(
                position=(5.5 + i, 10),
                fixtures=plank,
            )

            if i > 0:  # skip the joint on the first one
                self.world.CreateWeldJoint(
                    bodyA=prevBody,
                    bodyB=body,
                    anchor=(5 + i, 10),
                    frequencyHz=8.0,
                    dampingRatio=0.7,
                )

            prevBody = body

        # And a few random shapes to play with
        # First a set of triangles,
        fixture = b2FixtureDef(shape=b2PolygonShape(vertices=[(-0.5, 0.0),
                                                              (0.5, 0.0),
                                                              (0.0, 1.5),
                                                              ]),
                               density=1.0)
        for i in range(2):
            self.world.CreateDynamicBody(
                position=(-8 + 8 * i, 12),
                fixtures=fixture,
            )

        # And then a few circles
        fixture = b2FixtureDef(shape=b2CircleShape(radius=0.5), density=1)
        for i in range(3):
            self.world.CreateDynamicBody(
                position=(-6 + 6 * i, 10),
                fixtures=fixture,
            )