예제 #1
0
    def __init__(self):
        super(Pulley, self).__init__()
        y, L, a, b = 16.0, 12.0, 1.0, 2.0
        # The ground
        ground = self.world.create_static_body(
            shapes=[edgeShape(vertices=[(-40, 0), (40, 0)]),
                    circleShape(radius=2, pos=(-10.0, y + b + L)),
                    circleShape(radius=2, pos=(10.0, y + b + L))]
        )

        body_a = self.world.create_dynamic_body(
            position=(-10, y),
            fixtures=fixtureDef(shape=polygonShape(box=(a, b)), density=5.0),
        )
        body_b = self.world.create_dynamic_body(
            position=(10, y),
            fixtures=fixtureDef(shape=polygonShape(box=(a, b)), density=5.0),
        )

        self.pulley = self.world.CreatePulleyJoint(
            body_a=body_a,
            body_b=body_b,
            anchorA=(-10.0, y + b),
            anchorB=(10.0, y + b),
            groundAnchorA=(-10.0, y + b + L),
            groundAnchorB=(10.0, y + b + L),
            ratio=1.5,
        )
예제 #2
0
파일: chain.py 프로젝트: aaronma37/pybox2d
    def __init__(self):
        super(Chain, self).__init__()

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

        plank = fixtureDef(
            shape=polygonShape(box=(0.6, 0.125)),
            density=20,
            friction=0.2,
        )

        # Create one Chain (Only the left end is fixed)
        prevBody = ground
        y = 25
        numPlanks = 30
        for i in range(numPlanks):
            body = self.world.CreateDynamicBody(
                position=(0.5 + i, y),
                fixtures=plank,
            )

            # You can try a WeldJoint for a slightly different effect.
            # self.world.CreateWeldJoint(
            self.world.CreateRevoluteJoint(
                bodyA=prevBody,
                bodyB=body,
                anchor=(i, y),
            )

            prevBody = body
예제 #3
0
    def __init__(self):
        self.seed()
        self.viewer = None

        self.world = Box2D.b2World()
        self.terrain = None
        self.hull = None

        self.prev_shaping = None

        self.fd_polygon = fixtureDef(
                        shape = polygonShape(vertices=
                        [(0, 0),
                         (1, 0),
                         (1, -1),
                         (0, -1)]),
                        friction = FRICTION)

        self.fd_edge = fixtureDef(
                    shape = edgeShape(vertices=
                    [(0, 0),
                     (1, 1)]),
                    friction = FRICTION,
                    categoryBits=0x0001,
                )

        self.reset()

        high = np.array([np.inf]*24)
        self.action_space = spaces.Box(np.array([-1,-1,-1,-1]), np.array([+1,+1,+1,+1]))
        self.observation_space = spaces.Box(-high, high)
예제 #4
0
    def __init__(self, vertices, inner_corners, color, position, world):
        # use box2d edge
        self.uuid = uuid1()
        self.vertices = vertices
        self.color = color
        self.position = position
        self.world = world
        self.edge_fixtures = []
        self.edge_bodies = []
        for index, corner in enumerate(inner_corners):
            next_corner = inner_corners[(index + 1) % len(inner_corners)]
            fixture = edgeShape(vertices=[corner, next_corner*2-corner])

            self.edge_fixtures.append(fixture)
            body = world.CreateStaticBody(shapes=fixture, position=corner)
            body.userData = self
            self.edge_bodies.append(body)
예제 #5
0
    def reset(self):
        self._destroy()
        self.world.contactListener_keepref = ContactDetector(self)
        self.world.contactListener = self.world.contactListener_keepref
        self.game_over = False

        W = VIEWPORT_W/SCALE
        H = VIEWPORT_H/SCALE

        # terrain
        CHUNKS = 11
        height = self.np_random.uniform(0, H/2, size=(CHUNKS+1,) )
        chunk_x  = [W/(CHUNKS-1)*i for i in range(CHUNKS)]
        self.helipad_x1 = chunk_x[CHUNKS//2-1]
        self.helipad_x2 = chunk_x[CHUNKS//2+1]
        self.helipad_y  = H/4
        height[CHUNKS//2-2] = self.helipad_y
        height[CHUNKS//2-1] = self.helipad_y
        height[CHUNKS//2+0] = self.helipad_y
        height[CHUNKS//2+1] = self.helipad_y
        height[CHUNKS//2+2] = self.helipad_y
        smooth_y = [0.33*(height[i-1] + height[i+0] + height[i+1]) for i in range(CHUNKS)]

        self.moon = self.world.CreateStaticBody( shapes=edgeShape(vertices=[(0, 0), (W, 0)]) )
        self.sky_polys = []
        for i in range(CHUNKS-1):
            p1 = (chunk_x[i],   smooth_y[i])
            p2 = (chunk_x[i+1], smooth_y[i+1])
            self.moon.CreateEdgeFixture(
                vertices=[p1,p2],
                density=0,
                friction=0.1)
            self.sky_polys.append( [p1, p2, (p2[0],H), (p1[0],H)] )

        self.moon.color1 = (0.0,0.0,0.0)
        self.moon.color2 = (0.0,0.0,0.0)

        for lander in self.landers:
            self._create_lander(lander, len(self.landers))

        self.drawlist = (
            [lander.body for lander in self.landers]
            + [leg for lander in self.landers for leg in lander.legs])

        return self.step()
예제 #6
0
    def _generate_terrain(self, hardcore):
        GRASS, STUMP, STAIRS, PIT, _STATES_ = range(5)
        state    = GRASS
        velocity = 0.0
        y        = TERRAIN_HEIGHT
        counter  = TERRAIN_STARTPAD
        oneshot  = False
        self.terrain   = []
        self.terrain_x = []
        self.terrain_y = []
        for i in range(TERRAIN_LENGTH):
            x = i*TERRAIN_STEP
            self.terrain_x.append(x)

            if state==GRASS and not oneshot:
                velocity = 0.8*velocity + 0.01*np.sign(TERRAIN_HEIGHT - y)
                if i > TERRAIN_STARTPAD: velocity += np.random.uniform(-1, 1)/SCALE   #1
                y += velocity

            elif state==PIT and oneshot:
                counter = np.random.randint(3, 5)
                poly = [
                    (x,              y),
                    (x+TERRAIN_STEP, y),
                    (x+TERRAIN_STEP, y-4*TERRAIN_STEP),
                    (x,              y-4*TERRAIN_STEP),
                    ]
                t = self.world.CreateStaticBody(
                    fixtures = fixtureDef(
                        shape=polygonShape(vertices=poly),
                        friction = FRICTION
                    ))
                t.color1, t.color2 = (1,1,1), (0.6,0.6,0.6)
                self.terrain.append(t)
                t = self.world.CreateStaticBody(
                    fixtures = fixtureDef(
                        shape=polygonShape(vertices=[(p[0]+TERRAIN_STEP*counter,p[1]) for p in poly]),
                        friction = FRICTION
                    ))
                t.color1, t.color2 = (1,1,1), (0.6,0.6,0.6)
                self.terrain.append(t)
                counter += 2
                original_y = y

            elif state==PIT and not oneshot:
                y = original_y
                if counter > 1:
                    y -= 4*TERRAIN_STEP

            elif state==STUMP and oneshot:
                counter = np.random.randint(1, 3)
                poly = [
                    (x,                      y),
                    (x+counter*TERRAIN_STEP, y),
                    (x+counter*TERRAIN_STEP, y+counter*TERRAIN_STEP),
                    (x,                      y+counter*TERRAIN_STEP),
                    ]
                t = self.world.CreateStaticBody(
                    fixtures = fixtureDef(
                        shape=polygonShape(vertices=poly),
                        friction = FRICTION
                    ))
                t.color1, t.color2 = (1,1,1), (0.6,0.6,0.6)
                self.terrain.append(t)

            elif state==STAIRS and oneshot:
                stair_height = +1 if np.random.ranf() > 0.5 else -1
                stair_width = np.random.randint(4, 5)
                stair_steps = np.random.randint(3, 5)
                original_y = y
                for s in range(stair_steps):
                    poly = [
                        (x+(    s*stair_width)*TERRAIN_STEP, y+(   s*stair_height)*TERRAIN_STEP),
                        (x+((1+s)*stair_width)*TERRAIN_STEP, y+(   s*stair_height)*TERRAIN_STEP),
                        (x+((1+s)*stair_width)*TERRAIN_STEP, y+(-1+s*stair_height)*TERRAIN_STEP),
                        (x+(    s*stair_width)*TERRAIN_STEP, y+(-1+s*stair_height)*TERRAIN_STEP),
                        ]
                    t = self.world.CreateStaticBody(
                        fixtures = fixtureDef(
                            shape=polygonShape(vertices=poly),
                            friction = FRICTION
                        ))
                    t.color1, t.color2 = (1,1,1), (0.6,0.6,0.6)
                    self.terrain.append(t)
                counter = stair_steps*stair_width

            elif state==STAIRS and not oneshot:
                s = stair_steps*stair_width - counter - stair_height
                n = s/stair_width
                y = original_y + (n*stair_height)*TERRAIN_STEP

            oneshot = False
            self.terrain_y.append(y)
            counter -= 1
            if counter==0:
                counter = np.random.randint(TERRAIN_GRASS/2, TERRAIN_GRASS)
                if state==GRASS and hardcore:
                    state = np.random.randint(1, _STATES_)
                    oneshot = True
                else:
                    state = GRASS
                    oneshot = True

        self.terrain_poly = []
        for i in range(TERRAIN_LENGTH-1):
            poly = [
                (self.terrain_x[i],   self.terrain_y[i]),
                (self.terrain_x[i+1], self.terrain_y[i+1])
                ]
            t = self.world.CreateStaticBody(
                fixtures = fixtureDef(
                    shape=edgeShape(vertices=poly),
                    friction = FRICTION,
                    categoryBits=0x0001,
                ))
            color = (0.3, 1.0 if i%2==0 else 0.8, 0.3)
            t.color1 = color
            t.color2 = color
            self.terrain.append(t)
            color = (0.4, 0.6, 0.3)
            poly += [ (poly[1][0], 0), (poly[0][0], 0) ]
            self.terrain_poly.append( (poly, color) )
        self.terrain.reverse()
예제 #7
0
    def _reset(self):
        self._destroy()
        self.game_over = False
        self.prev_shaping = None

        W = VIEWPORT_W/SCALE
        H = VIEWPORT_H/SCALE

        # terrain
        CHUNKS = 11
        height = np.random.uniform(0, H/2, size=(CHUNKS+1,) )
        chunk_x  = [W/(CHUNKS-1)*i for i in xrange(CHUNKS)]
        self.helipad_x1 = chunk_x[CHUNKS//2-1]
        self.helipad_x2 = chunk_x[CHUNKS//2+1]
        self.helipad_y  = H/4
        height[CHUNKS//2-2] = self.helipad_y
        height[CHUNKS//2-1] = self.helipad_y
        height[CHUNKS//2+0] = self.helipad_y
        height[CHUNKS//2+1] = self.helipad_y
        height[CHUNKS//2+2] = self.helipad_y
        smooth_y = [0.33*(height[i-1] + height[i+0] + height[i+1]) for i in xrange(CHUNKS)]

        self.moon = self.world.CreateStaticBody( shapes=edgeShape(vertices=[(0, 0), (W, 0)]) )
        self.sky_polys = []
        for i in xrange(CHUNKS-1):
            p1 = (chunk_x[i],   smooth_y[i])
            p2 = (chunk_x[i+1], smooth_y[i+1])
            self.moon.CreateEdgeFixture(
                vertices=[p1,p2],
                density=0,
                friction=0.1)
            self.sky_polys.append( [p1, p2, (p2[0],H), (p1[0],H)] )

        self.moon.color1 = (0.0,0.0,0.0)
        self.moon.color2 = (0.0,0.0,0.0)

        initial_y = VIEWPORT_H/SCALE
        self.lander = self.world.CreateDynamicBody(
            position = (VIEWPORT_W/SCALE/2, initial_y),
            angle=0.0,
            fixtures = fixtureDef(
                shape=polygonShape(vertices=[ (x/SCALE,y/SCALE) for x,y in LANDER_POLY ]),
                density=5.0,
                friction=0.1,
                categoryBits=0x0010,
                maskBits=0x001,  # collide only with ground
                restitution=0.0) # 0.99 bouncy
                )
        self.lander.color1 = (0.5,0.4,0.9)
        self.lander.color2 = (0.3,0.3,0.5)
        self.lander.ApplyForceToCenter( (
            np.random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM),
            np.random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM)
            ), True)

        self.legs = []
        for i in [-1,+1]:
            leg = self.world.CreateDynamicBody(
                position = (VIEWPORT_W/SCALE/2 - i*LEG_AWAY/SCALE, initial_y),
                angle = (i*0.05),
                fixtures = fixtureDef(
                    shape=polygonShape(box=(LEG_W/SCALE, LEG_H/SCALE)),
                    density=1.0,
                    restitution=0.0,
                    categoryBits=0x0020,
                    maskBits=0x001)
                )
            leg.color1 = (0.5,0.4,0.9)
            leg.color2 = (0.3,0.3,0.5)
            rjd = revoluteJointDef(
                bodyA=self.lander,
                bodyB=leg,
                localAnchorA=(0, 0),
                localAnchorB=(i*LEG_AWAY/SCALE, LEG_DOWN/SCALE),
                enableMotor=True,
                enableLimit=True,
                maxMotorTorque=LEG_SPRING_TORQUE,
                motorSpeed=+0.3*i  # low enough not to jump back into the sky
                )
            if i==-1:
                rjd.lowerAngle = +0.9 - 0.5  # Yes, the most esoteric numbers here, angles legs have freedom to travel within
                rjd.upperAngle = +0.9
            else:
                rjd.lowerAngle = -0.9
                rjd.upperAngle = -0.9 + 0.5
            leg.joint = self.world.CreateJoint(rjd)
            self.legs.append(leg)

        self.drawlist = [self.lander] + self.legs

        return self._step(0)[0]
예제 #8
0
    def _generate_terrain(self, hardcore):
        GRASS, STUMP, STAIRS, PIT, _STATES_ = range(5)
        state = GRASS
        velocity = 0.0
        y = TERRAIN_HEIGHT
        counter = TERRAIN_STARTPAD
        oneshot = False
        self.terrain = []
        self.terrain_x = []
        self.terrain_y = []
        for i in range(self.terrain_length):
            x = i * TERRAIN_STEP
            self.terrain_x.append(x)

            if state == GRASS and not oneshot:
                velocity = 0.8 * velocity + 0.01 * np.sign(TERRAIN_HEIGHT - y)
                if i > TERRAIN_STARTPAD:
                    velocity += self.np_random.uniform(-1, 1) / SCALE  # 1
                y += velocity

            elif state == PIT and oneshot:
                counter = self.np_random.randint(3, 5)
                poly = [
                    (x, y),
                    (x + TERRAIN_STEP, y),
                    (x + TERRAIN_STEP, y - 4 * TERRAIN_STEP),
                    (x, y - 4 * TERRAIN_STEP),
                ]
                t = self.world.CreateStaticBody(fixtures=fixtureDef(
                    shape=polygonShape(vertices=poly), friction=FRICTION))
                t.color1, t.color2 = (1, 1, 1), (0.6, 0.6, 0.6)
                self.terrain.append(t)
                t = self.world.CreateStaticBody(
                    fixtures=fixtureDef(shape=polygonShape(
                        vertices=[(p[0] + TERRAIN_STEP * counter, p[1])
                                  for p in poly]),
                                        friction=FRICTION))
                t.color1, t.color2 = (1, 1, 1), (0.6, 0.6, 0.6)
                self.terrain.append(t)
                counter += 2
                original_y = y

            elif state == PIT and not oneshot:
                y = original_y
                if counter > 1:
                    y -= 4 * TERRAIN_STEP

            elif state == STUMP and oneshot:
                counter = self.np_random.randint(1, 3)
                poly = [
                    (x, y),
                    (x + counter * TERRAIN_STEP, y),
                    (x + counter * TERRAIN_STEP, y + counter * TERRAIN_STEP),
                    (x, y + counter * TERRAIN_STEP),
                ]
                t = self.world.CreateStaticBody(fixtures=fixtureDef(
                    shape=polygonShape(vertices=poly), friction=FRICTION))
                t.color1, t.color2 = (1, 1, 1), (0.6, 0.6, 0.6)
                self.terrain.append(t)

            elif state == STAIRS and oneshot:
                stair_height = +1 if self.np_random.rand() > 0.5 else -1
                stair_width = self.np_random.randint(4, 5)
                stair_steps = self.np_random.randint(3, 5)
                original_y = y
                for s in range(stair_steps):
                    poly = [
                        (x + (s * stair_width) * TERRAIN_STEP,
                         y + (s * stair_height) * TERRAIN_STEP),
                        (x + ((1 + s) * stair_width) * TERRAIN_STEP,
                         y + (s * stair_height) * TERRAIN_STEP),
                        (x + ((1 + s) * stair_width) * TERRAIN_STEP,
                         y + (-1 + s * stair_height) * TERRAIN_STEP),
                        (x + (s * stair_width) * TERRAIN_STEP,
                         y + (-1 + s * stair_height) * TERRAIN_STEP),
                    ]
                    t = self.world.CreateStaticBody(fixtures=fixtureDef(
                        shape=polygonShape(vertices=poly), friction=FRICTION))
                    t.color1, t.color2 = (1, 1, 1), (0.6, 0.6, 0.6)
                    self.terrain.append(t)
                counter = stair_steps * stair_width

            elif state == STAIRS and not oneshot:
                s = stair_steps * stair_width - counter - stair_height
                n = s / stair_width
                y = original_y + (n * stair_height) * TERRAIN_STEP

            oneshot = False
            self.terrain_y.append(y)
            counter -= 1
            if counter == 0:
                counter = self.np_random.randint(TERRAIN_GRASS / 2,
                                                 TERRAIN_GRASS)
                if state == GRASS and hardcore:
                    state = self.np_random.randint(1, _STATES_)
                    oneshot = True
                else:
                    state = GRASS
                    oneshot = True

        self.terrain_poly = []
        for i in range(self.terrain_length - 1):
            poly = [(self.terrain_x[i], self.terrain_y[i]),
                    (self.terrain_x[i + 1], self.terrain_y[i + 1])]
            t = self.world.CreateStaticBody(fixtures=fixtureDef(
                shape=edgeShape(vertices=poly),
                friction=FRICTION,
                categoryBits=0x0001,
            ))
            color = (0.3, 1.0 if i % 2 == 0 else 0.8, 0.3)
            t.color1 = color
            t.color2 = color
            self.terrain.append(t)
            color = (0.4, 0.6, 0.3)
            poly += [(poly[1][0], 0), (poly[0][0], 0)]
            self.terrain_poly.append((poly, color))
        self.terrain.reverse()
예제 #9
0
    def _reset(self):
        self._destroy()
        self.world.contactListener_keepref = ContactDetector(self)
        self.world.contactListener = self.world.contactListener_keepref
        self.game_over = False
        self.prev_shaping = None

        W = VIEWPORT_W / SCALE
        H = VIEWPORT_H / SCALE

        # terrain
        CHUNKS = 11
        height = self.np_random.uniform(0, H / 2, size=(CHUNKS + 1, ))
        chunk_x = [W / (CHUNKS - 1) * i for i in range(CHUNKS)]
        self.helipad_x1 = chunk_x[CHUNKS // 2 - 1]
        self.helipad_x2 = chunk_x[CHUNKS // 2 + 1]
        self.helipad_y = H / 4
        height[CHUNKS // 2 - 2] = self.helipad_y
        height[CHUNKS // 2 - 1] = self.helipad_y
        height[CHUNKS // 2 + 0] = self.helipad_y
        height[CHUNKS // 2 + 1] = self.helipad_y
        height[CHUNKS // 2 + 2] = self.helipad_y
        smooth_y = [
            0.33 * (height[i - 1] + height[i + 0] + height[i + 1])
            for i in range(CHUNKS)
        ]

        self.moon = self.world.CreateStaticBody(shapes=edgeShape(
            vertices=[(0, 0), (W, 0)]))
        self.sky_polys = []
        for i in range(CHUNKS - 1):
            p1 = (chunk_x[i], smooth_y[i])
            p2 = (chunk_x[i + 1], smooth_y[i + 1])
            self.moon.CreateEdgeFixture(vertices=[p1, p2],
                                        density=0,
                                        friction=0.1)
            self.sky_polys.append([p1, p2, (p2[0], H), (p1[0], H)])

        self.moon.color1 = (0.0, 0.0, 0.0)
        self.moon.color2 = (0.0, 0.0, 0.0)

        initial_y = VIEWPORT_H / SCALE
        self.lander = self.world.CreateDynamicBody(
            position=(VIEWPORT_W / SCALE / 2, initial_y),
            angle=0.0,
            fixtures=fixtureDef(
                shape=polygonShape(vertices=[(x / SCALE, y / SCALE)
                                             for x, y in LANDER_POLY]),
                density=5.0,
                friction=0.1,
                categoryBits=0x0010,
                maskBits=0x001,  # collide only with ground
                restitution=0.0)  # 0.99 bouncy
        )
        self.lander.color1 = (0.5, 0.4, 0.9)
        self.lander.color2 = (0.3, 0.3, 0.5)
        self.lander.ApplyForceToCenter(
            (self.np_random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM),
             self.np_random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM)), True)

        self.legs = []
        for i in [-1, +1]:
            leg = self.world.CreateDynamicBody(
                position=(VIEWPORT_W / SCALE / 2 - i * LEG_AWAY / SCALE,
                          initial_y),
                angle=(i * 0.05),
                fixtures=fixtureDef(shape=polygonShape(box=(LEG_W / SCALE,
                                                            LEG_H / SCALE)),
                                    density=1.0,
                                    restitution=0.0,
                                    categoryBits=0x0020,
                                    maskBits=0x001))
            leg.ground_contact = False
            leg.color1 = (0.5, 0.4, 0.9)
            leg.color2 = (0.3, 0.3, 0.5)
            rjd = revoluteJointDef(
                bodyA=self.lander,
                bodyB=leg,
                localAnchorA=(0, 0),
                localAnchorB=(i * LEG_AWAY / SCALE, LEG_DOWN / SCALE),
                enableMotor=True,
                enableLimit=True,
                maxMotorTorque=LEG_SPRING_TORQUE,
                motorSpeed=+0.3 * i  # low enough not to jump back into the sky
            )
            if i == -1:
                rjd.lowerAngle = +0.9 - 0.5  # Yes, the most esoteric numbers here, angles legs have freedom to travel within
                rjd.upperAngle = +0.9
            else:
                rjd.lowerAngle = -0.9
                rjd.upperAngle = -0.9 + 0.5
            leg.joint = self.world.CreateJoint(rjd)
            self.legs.append(leg)

        self.drawlist = [self.lander] + self.legs

        return self._step(np.array([0, 0]) if self.continuous else 0)[0]
예제 #10
0
    def __init__(self, max_steps=1500, leg_length=34, terrain_length_scale=2,
                 fall_penalty = -50):
        print(leg_length)
        self.max_steps = max_steps
        self.terrain_length_scale=terrain_length_scale
        self.fall_penalty = fall_penalty
        self.torque_penalty = 0.00035
        self.head_balance_penalty = 5

        self.LEG_W, self.LEG_H = 8 / SCALE, leg_length / SCALE
        self.HULL_FD = fixtureDef(
            shape=polygonShape(vertices=[(x / SCALE, y / SCALE) for x, y in HULL_POLY]),
            density=5.0,
            friction=0.1,
            categoryBits=0x0020,
            maskBits=0x001,  # collide only with ground
            restitution=0.0)  # 0.99 bouncy

        self.LEG_FD = fixtureDef(
            shape=polygonShape(box=(self.LEG_W / 2, self.LEG_H / 2)),
            density=1.0,
            restitution=0.0,
            categoryBits=0x0020,
            maskBits=0x001)

        self.LOWER_FD = fixtureDef(
            shape=polygonShape(box=(0.8 * self.LEG_W / 2, self.LEG_H / 2)),
            density=1.0,
            restitution=0.0,
            categoryBits=0x0020,
            maskBits=0x001)

        EzPickle.__init__(self)
        self.seed()
        self.viewer = None

        self.world = Box2D.b2World()
        self.terrain = None
        self.hull = None

        self.prev_shaping = None

        self.fd_polygon = fixtureDef(
                        shape = polygonShape(vertices=
                        [(0, 0),
                         (1, 0),
                         (1, -1),
                         (0, -1)]),
                        friction = FRICTION)

        self.fd_edge = fixtureDef(
                    shape = edgeShape(vertices=
                    [(0, 0),
                     (1, 1)]),
                    friction = FRICTION,
                    categoryBits=0x0001,
                )

        self.reset()

        high = np.array([np.inf] * 24)
        self.action_space = spaces.Box(np.array([-1, -1, -1, -1]), np.array([1, 1, 1, 1]), dtype=np.float32)
        self.observation_space = spaces.Box(-high, high, dtype=np.float32)
예제 #11
0
    def reset(self):
        self._destroy()
        self.prev_shaping = None

        self.startpos = None

        W = self.VIEWPORT_W / self.SCALE
        H = self.VIEWPORT_H / self.SCALE

        # terrain
        height = self.np_random.uniform(0,
                                        H / 2,
                                        size=(self.TERRAIN_CHUNKS + 1, ))
        chunk_x = [
            W / (self.TERRAIN_CHUNKS - 1) * i
            for i in range(self.TERRAIN_CHUNKS)
        ]
        self.helipad_x1 = chunk_x[self.TERRAIN_CHUNKS // 2 - 1]
        self.helipad_x2 = chunk_x[self.TERRAIN_CHUNKS // 2 + 1]
        self.helipad_y = H / 4
        height[self.TERRAIN_CHUNKS // 2 - 2] = self.helipad_y
        height[self.TERRAIN_CHUNKS // 2 - 1] = self.helipad_y
        height[self.TERRAIN_CHUNKS // 2 + 0] = self.helipad_y
        height[self.TERRAIN_CHUNKS // 2 + 1] = self.helipad_y
        height[self.TERRAIN_CHUNKS // 2 + 2] = self.helipad_y
        smooth_y = [
            0.33 * (height[i - 1] + height[i + 0] + height[i + 1])
            for i in range(self.TERRAIN_CHUNKS)
        ]

        self.ground = self.world.CreateStaticBody(shapes=edgeShape(
            vertices=[(0, 0), (W, 0)]))
        self.sky_polys = []
        for i in range(self.TERRAIN_CHUNKS - 1):
            p1 = (chunk_x[i], smooth_y[i])
            p2 = (chunk_x[i + 1], smooth_y[i + 1])
            self.ground.CreateEdgeFixture(vertices=[p1, p2],
                                          density=0,
                                          friction=0.1)
            self.sky_polys.append([p1, p2, (p2[0], H), (p1[0], H)])

        self.startpos = self.VIEWPORT_W / self.SCALE / 2, self.VIEWPORT_H / self.SCALE

        self.lander = self.world.CreateDynamicBody(
            position=self.startpos,
            angle=0.0,
            fixtures=[
                fixtureDef(shape=polygonShape(vertices=[(x / self.SCALE,
                                                         y / self.SCALE)
                                                        for x, y in poly]),
                           density=0.0)
                for poly in [
                    self.HULL_POLY, self.LEG1_POLY, self.LEG2_POLY,
                    self.MOTOR1_POLY, self.MOTOR2_POLY, self.BLADE1L_POLY,
                    self.BLADE1R_POLY, self.BLADE2L_POLY, self.BLADE2R_POLY
                ]
            ])

        # By showing props periodically, we can emulate prop rotation
        self.props_visible = 0

        # Create cusom dynamics model
        self.dynamics = DJIPhantomDynamics()

        # Initialize custom dynamics with slight velocity perturbation
        state = np.zeros(12)
        d = self.dynamics
        state[d.STATE_Y] = self.startpos[
            0]  # 3D copter Y comes from 2D copter X
        state[d.STATE_Z] = -self.startpos[
            1]  # 3D copter Z comes from 2D copter Y, negated for NED
        state[d.STATE_Y_DOT] = self.INITIAL_RANDOM_VELOCITY * np.random.randn()
        state[d.STATE_Z_DOT] = self.INITIAL_RANDOM_VELOCITY * np.random.randn()
        self.dynamics.setState(state)

        return self.step(np.array([0, 0]))[0]
예제 #12
0
    def _reset(self, xoff=0, yoff=0):
        self._destroy()
        self.world.contactListener_keepref = ContactDetector(self)
        self.world.contactListener = self.world.contactListener_keepref
        self.landed = False
        self.prev_shaping = None
        self.rendering = False

        W = self.VIEWPORT_W / self.SCALE
        H = self.VIEWPORT_H / self.SCALE

        # Turn off gravity so we can run our own dynamics
        self.world.gravity = 0, 0

        # terrain
        CHUNKS = 11
        height = self.np_random.uniform(0, H / 2, size=(CHUNKS + 1, ))
        chunk_x = [W / (CHUNKS - 1) * i for i in range(CHUNKS)]
        self.helipad_x1 = chunk_x[CHUNKS // 2 - 1]
        self.helipad_x2 = chunk_x[CHUNKS // 2 + 1]
        self.helipad_y = H / 4
        height[CHUNKS // 2 - 2] = self.helipad_y
        height[CHUNKS // 2 - 1] = self.helipad_y
        height[CHUNKS // 2 + 0] = self.helipad_y
        height[CHUNKS // 2 + 1] = self.helipad_y
        height[CHUNKS // 2 + 2] = self.helipad_y
        smooth_y = [
            0.33 * (height[i - 1] + height[i + 0] + height[i + 1])
            for i in range(CHUNKS)
        ]

        self.ground = self.world.CreateStaticBody(shapes=edgeShape(
            vertices=[(0, 0), (W, 0)]))
        self.sky_polys = []
        for i in range(CHUNKS - 1):
            p1 = (chunk_x[i], smooth_y[i])
            p2 = (chunk_x[i + 1], smooth_y[i + 1])
            self.ground.CreateEdgeFixture(vertices=[p1, p2],
                                          density=0,
                                          friction=0.1)
            self.sky_polys.append([p1, p2, (p2[0], H), (p1[0], H)])

        initial_y = self.VIEWPORT_H / self.SCALE

        self.lander = self.world.CreateDynamicBody(
            position=(self.VIEWPORT_W / self.SCALE / 2, initial_y),
            angle=0.0,
            fixtures=[
                fixtureDef(shape=polygonShape(vertices=[(x / self.SCALE,
                                                         y / self.SCALE)
                                                        for x, y in poly]),
                           density=1.0)
                for poly in [
                    self.HULL_POLY, self.LEG1_POLY, self.LEG2_POLY,
                    self.MOTOR1_POLY, self.MOTOR2_POLY, self.BLADE1L_POLY,
                    self.BLADE1R_POLY, self.BLADE2L_POLY, self.BLADE2R_POLY
                ]
            ])

        self.dynamics = DJIPhantomDynamics()

        # Start at top center, plus optional offset
        state = np.zeros(12)
        state[
            self.dynamics.
            STATE_Y] = self.START_X + xoff  # 3D copter Y comes from 2D copter X
        state[self.dynamics.STATE_Z] = -(
            self.START_Y + yoff)  # 3D copter Z comes from 2D copter Y

        self.dynamics.setState(state)

        # By showing props periodically, we can emulate prop rotation
        self.show_props = 0

        # Support showing vehicle while on ground
        self.ground_count = 0

        return self.step(np.array([0, 0]))[0]
예제 #13
0
def get_transformed_edge(edge, body):
    new_vertices = [tuple(body.transform * v) for v in edge.vertices]
    return edgeShape(vertices=new_vertices)
예제 #14
0
    def reset(self):
        self._destroy()
        self.world.contactListener_keepref = ContactDetector(self)
        self.world.contactListener = self.world.contactListener_keepref
        self.game_over = False
        self.prev_shaping = None

        W = VIEWPORT_W / SCALE  # window_width / scale = 600 / 30 = 20
        H = VIEWPORT_H / SCALE  # window_height / scale = 400 /30 = 13

        # terrain
        CHUNKS = 15  # the number of vertex on surface
        height = self.np_random.uniform(
            0, H / 3,
            size=(CHUNKS +
                  1, ))  # vertex's y-axis position (randomly generate)
        chunk_x = [W / (CHUNKS - 1) * i
                   for i in range(CHUNKS)]  # vertex's x-axis position

        # interval length
        self.interval_length = chunk_x[4] - chunk_x[3]

        self.helipad_x1 = chunk_x[CHUNKS // 2 -
                                  0]  # define the x-position of left flag
        self.helipad_x2 = chunk_x[CHUNKS // 2 +
                                  0]  # define the y-position of right flag
        self.helipad_y = H / 4  # define the y-position of flag-bottom

        smooth_y = [
            0.33 * (height[i - 1] + height[i + 0] + height[i + 1])
            for i in range(CHUNKS)
        ]
        """########################### change the position of flag ##############################"""
        index = np.random.randint(1, CHUNKS - 2)
        self.helipad_one_x = chunk_x[index] + (chunk_x[index + 1] -
                                               chunk_x[index]) / 2
        self.helipad_one_y = smooth_y[index]
        smooth_y[index + 1] = self.helipad_one_y
        """########################### change the position of flag ##############################"""

        self.moon = self.world.CreateStaticBody(fixtures=fixtureDef(
            shape=edgeShape(vertices=[(0, 0), (W, 0)])))  # ???
        self.moon_polys = []
        self.sky_polys = []
        for i in range(CHUNKS - 1):
            p1 = (chunk_x[i], smooth_y[i]
                  )  # use chunk_x and smooth_y to generate points
            p2 = (chunk_x[i + 1], smooth_y[i + 1])
            self.moon.CreateEdgeFixture(
                vertices=[p1,
                          p2],  # use 2-neighbor points to create ground ???
                density=0,  # density of the ground
                friction=0.1)  # friction of the ground
            self.moon_polys.append([p1, p2, (p2[0], 0), (p1[0], 0)])
            self.sky_polys.append([p1, p2, (p2[0], H),
                                   (p1[0], H)])  # sky-poly shape, 4 points

        initial_y = VIEWPORT_H / SCALE  # starting altitude of the lander
        """########################### Create Earth, Stars, Smoke ##############################"""
        self._create_stars_large()
        self._create_stars_small()
        """########################### Create Earth, Stars ##############################"""

        self.lander = self.world.CreateDynamicBody(
            position=(VIEWPORT_W / SCALE / 2,
                      initial_y),  # starting point: (middle, top)
            angle=0.0,  # angle: no idea
            fixtures=fixtureDef(
                shape=polygonShape(vertices=[(x / SCALE, y / SCALE)
                                             for x, y in LANDER_POLY]),
                density=5.0,
                friction=0.1,
                categoryBits=0x0010,  # the category the lander belongs to
                maskBits=0x001,  # the category the lander with collide with
                restitution=0.99)  # 0.99 bouncy
        )
        self.lander.color1 = (160 / 255, 160 / 255, 160 / 255
                              )  # filling color for lander
        self.lander.color2 = (160 / 255, 160 / 255, 160 / 255
                              )  # border color for lander
        self.lander.ApplyForceToCenter(
            (  # not understood
                self.np_random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM),
                self.np_random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM)),
            True)

        self.legs = []
        for i in [-1, +1]:
            leg = self.world.CreateDynamicBody(
                position=(VIEWPORT_W / SCALE / 2 - i * LEG_AWAY / SCALE,
                          initial_y),  # x <-> ; y <-> lander's "middle"
                angle=(i * 0.20),  # control the angle between legs and body
                fixtures=fixtureDef(
                    shape=polygonShape(box=(LEG_W / SCALE, LEG_H / SCALE)),
                    density=1.0,
                    restitution=0.0,
                    categoryBits=0x0020,  # the category the legs belongs to
                    maskBits=0x001)  # the category the legs with collide with
            )
            leg.ground_contact = False
            leg.color1 = (1, 0, 0)  # filling color for legs
            leg.color2 = (1, 0, 0)  # border color for legs
            rjd = revoluteJointDef(  # para about joints between lander and legs
                bodyA=self.lander,
                bodyB=leg,
                localAnchorA=(0, 0),
                localAnchorB=(i * LEG_AWAY / SCALE, LEG_DOWN / SCALE),
                enableMotor=True,
                enableLimit=True,
                maxMotorTorque=LEG_SPRING_TORQUE,
                motorSpeed=+0.3 * i  # low enough not to jump back into the sky
            )
            if i == -1:
                rjd.lowerAngle = +0.9 - 0.6  # Yes, the most esoteric numbers here,
                rjd.upperAngle = +0.36  # angles legs have freedom to travel within
            else:
                rjd.lowerAngle = -0.36  # angle between legs and body
                rjd.upperAngle = -0.9 + 0.6  # angle between legs and body
            leg.joint = self.world.CreateJoint(rjd)
            self.legs.append(leg)

        self.drawlist = [self.lander] + self.legs

        return self.step(np.array([0, 0]) if self.continuous else 0)[
            0]  # not understood
    def _reset(self):
        self._destroy()
        self.world.contactListener_keepref = ContactDetector(self)
        self.world.contactListener = self.world.contactListener_keepref
        self.terminated = False
        self.prev_shaping = None
        self.human_render = False

        # Each episode we choose a new wind speed and the direction on the wind (from to the right and vise versa)
        self.wind_speed = random.choice(WIND_SPEED_RANGE)
        self.wind_direction = random.choice(
            (-1, 1))  # -1 means from right to left; 1 means from left to right
        # Le't monitor the fuel capacity
        self.fuel_capacity = FULL_TANK

        self.total_angle = 0
        self.prev_angle = 0

        # Relief
        BENDS = 15
        bends_y = np.random.uniform(0, H / 2, size=(BENDS + 1, ))
        hill_width = W / (BENDS - 1)
        bends_x = [
            hill_width * i +
            np.random.uniform(-0.5 * hill_width, 0.5 * hill_width) *
            int(n != 0 and n != BENDS - 1) for n, i in enumerate(range(BENDS))
        ]
        self.plateau_x1 = bends_x[BENDS // 2 - 1]
        self.plateau_x2 = bends_x[BENDS // 2 + 1]
        self.plateau_center = (self.plateau_x2 + self.plateau_x1) / 2
        self.plateau_y = np.random.uniform(H / 8, H / 2)
        bends_y[BENDS // 2 - 2:BENDS // 2 + 3] = self.plateau_y
        smooth_y = [
            np.mean([bends_y[i - 1], bends_y[i + 0], bends_y[i + 1]])
            for i in range(BENDS)
        ]

        self.mars = self.world.CreateStaticBody(shapes=edgeShape(
            vertices=[(0, 0), (W, 0)]))
        self.relief_coords = []
        for i in range(BENDS - 1):
            p1 = (bends_x[i], smooth_y[i])
            p2 = (bends_x[i + 1], smooth_y[i + 1])
            self.mars.CreateEdgeFixture(vertices=[p1, p2],
                                        density=0,
                                        friction=0.1)
            self.relief_coords.append([p1, p2, (p2[0], 0), (p1[0], 0)])

        # Lander
        # Body
        initial_y = H
        initial_x = np.random.uniform(0.1 * W, 0.9 * W)
        self.lander = self.world.CreateDynamicBody(
            position=(initial_x, initial_y),
            angle=0.0,
            fixtures=fixtureDef(
                shape=polygonShape(vertices=[(x / SCALE, y / SCALE)
                                             for x, y in LANDER_POLY]),
                density=5.0,
                friction=0.1,
                categoryBits=0x0010,
                maskBits=0x001,  # collide only with ground
                restitution=0.0)  # 0.99 bouncy
        )
        self.lander.color1 = LANDER_COLOR
        self.lander.color2 = LANDER_COLOR_BORDER
        self.lander.ApplyForceToCenter(
            (np.random.uniform(-INITIAL_RANDOM_FORCE, INITIAL_RANDOM_FORCE),
             np.random.uniform(-INITIAL_RANDOM_FORCE, INITIAL_RANDOM_FORCE)),
            True)

        # Legs
        self.legs = []
        for i in [-1, +1]:
            leg = self.world.CreateDynamicBody(
                position=(initial_x - i * LEG_AWAY / SCALE, initial_y),
                angle=(i * 0.05),
                fixtures=fixtureDef(shape=polygonShape(box=(LEG_W / SCALE,
                                                            LEG_H / SCALE)),
                                    density=1.0,
                                    restitution=0.0,
                                    categoryBits=0x0020,
                                    maskBits=0x001))
            leg.ground_contact = False
            leg.color1 = LEGS_COLOR
            leg.color2 = LEGS_COLOR_BORDER
            rjd = revoluteJointDef(
                bodyA=self.lander,
                bodyB=leg,
                localAnchorA=(0, 0),
                localAnchorB=(i * LEG_AWAY / SCALE, LEG_DOWN / SCALE),
                enableMotor=True,
                enableLimit=True,
                maxMotorTorque=LEG_SPRING_TORQUE,
                motorSpeed=+0.3 * i  # low enough not to jump back into the sky
            )
            if i == -1:
                rjd.lowerAngle = +0.9 - 0.5  # Yes, the most esoteric numbers here, angles legs have freedom to travel within
                rjd.upperAngle = +0.9
            else:
                rjd.lowerAngle = -0.9
                rjd.upperAngle = -0.9 + 0.5
            leg.joint = self.world.CreateJoint(rjd)
            self.legs.append(leg)

        self.drawlist = [self.lander] + self.legs
        return self._step(np.array([0, 0]) if self.continuous else 0)[0]