Beispiel #1
0
    def _createJoints(self):
        """Internal method to create joints.
        """

        front_wheel_joint = wheelJointDef(
            bodyA=self.chassis,
            bodyB=self.front_wheel,
            localAnchorA=(1.096710562705994, -0.4867535233497620),
            localAnchorB=(-0.001052246429026127, 0.0007888938998803496),
            localAxisA=(-0.4521348178386688, 0.8919496536254883),
            frequencyHz=10.0,
            enableMotor=True,
            maxMotorTorque=20.0,
            dampingRatio=0.8)

        swingarm_revolute = revoluteJointDef(
            bodyA=self.chassis,
            bodyB=self.swingArm,
            localAnchorA=(-0.1697492003440857, -0.2360641807317734),
            localAnchorB=(0.3914504945278168, 0.1011910215020180),
            enableMotor=False,
            enableLimit=True,
            maxMotorTorque=9999999999.0,
            motorSpeed=0,
            lowerAngle=0.1,
            upperAngle=0.45)

        # distancejoint4
        swingarm_distance = distanceJointDef(
            bodyA=self.chassis,
            bodyB=self.swingArm,
            localAnchorA=(-0.8274764418601990, 0.3798926770687103),
            localAnchorB=(-0.2273961603641510, -0.04680897668004036),
            length=0.8700000047683716,
            frequencyHz=8.0,
            dampingRatio=0.8999999761581421)

        # drivejoint
        swingarm_drive = revoluteJointDef(
            bodyA=self.swingArm,
            bodyB=self.rear_wheel,
            localAnchorA=(-0.3686238229274750, -0.07278718799352646),
            localAnchorB=(0, 0),
            enableMotor=False,
            enableLimit=False,
            maxMotorTorque=8.0,
            motorSpeed=0,
            lowerAngle=0,
            upperAngle=0,
        )

        self.front_wheel_joint = self.world.CreateJoint(front_wheel_joint)
        self.swingarm_revolute = self.world.CreateJoint(swingarm_revolute)
        self.swingarm_distance = self.world.CreateJoint(swingarm_distance)
        self.swingarm_drive = self.world.CreateJoint(swingarm_drive)
Beispiel #2
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
        self.throttle = 0
        self.gimbal = 0.0
        self.landed_ticks = 0
        self.stepnumber = 0
        self.smoke = []

        # self.terrainheigth = self.np_random.uniform(H / 20, H / 10)
        self.terrainheigth = H / 20
        self.shipheight = self.terrainheigth + SHIP_HEIGHT
        # ship_pos = self.np_random.uniform(0, SHIP_WIDTH / SCALE) + SHIP_WIDTH / SCALE
        ship_pos = W / 2
        self.helipad_x1 = ship_pos - SHIP_WIDTH / 2
        self.helipad_x2 = self.helipad_x1 + SHIP_WIDTH
        self.helipad_y = self.terrainheigth + SHIP_HEIGHT

        self.water = self.world.CreateStaticBody(
            fixtures=fixtureDef(shape=polygonShape(
                vertices=((0, 0), (W, 0), (W, self.terrainheigth),
                          (0, self.terrainheigth))),
                                friction=0.1,
                                restitution=0.0))
        self.water.color1 = rgb(70, 96, 176)

        self.ship = self.world.CreateStaticBody(
            fixtures=fixtureDef(shape=polygonShape(
                vertices=((self.helipad_x1, self.terrainheigth),
                          (self.helipad_x2, self.terrainheigth),
                          (self.helipad_x2, self.terrainheigth + SHIP_HEIGHT),
                          (self.helipad_x1,
                           self.terrainheigth + SHIP_HEIGHT))),
                                friction=0.5,
                                restitution=0.0))

        self.containers = []
        for side in [-1, 1]:
            self.containers.append(
                self.world.CreateStaticBody(
                    fixtures=fixtureDef(shape=polygonShape(
                        vertices=((ship_pos + side * 0.95 * SHIP_WIDTH / 2,
                                   self.helipad_y),
                                  (ship_pos + side * 0.95 * SHIP_WIDTH / 2,
                                   self.helipad_y + SHIP_HEIGHT),
                                  (ship_pos + side * 0.95 * SHIP_WIDTH / 2 -
                                   side * SHIP_HEIGHT,
                                   self.helipad_y + SHIP_HEIGHT),
                                  (ship_pos + side * 0.95 * SHIP_WIDTH / 2 -
                                   side * SHIP_HEIGHT, self.helipad_y))),
                                        friction=0.2,
                                        restitution=0.0)))
            self.containers[-1].color1 = rgb(206, 206, 2)

        self.ship.color1 = (0.2, 0.2, 0.2)

        initial_x = W / 2 + W * np.random.uniform(-0.3, 0.3)
        initial_y = H * 0.95
        self.lander = self.world.CreateDynamicBody(
            position=(initial_x, initial_y),
            angle=0.0,
            fixtures=fixtureDef(shape=polygonShape(
                vertices=((-ROCKET_WIDTH / 2, 0), (+ROCKET_WIDTH / 2, 0),
                          (ROCKET_WIDTH / 2, +ROCKET_HEIGHT),
                          (-ROCKET_WIDTH / 2, +ROCKET_HEIGHT))),
                                density=1.0,
                                friction=0.5,
                                categoryBits=0x0010,
                                maskBits=0x001,
                                restitution=0.0))

        self.lander.color1 = rgb(230, 230, 230)

        for i in [-1, +1]:
            leg = self.world.CreateDynamicBody(
                position=(initial_x - i * LEG_AWAY,
                          initial_y + ROCKET_WIDTH * 0.2),
                angle=(i * BASE_ANGLE),
                fixtures=fixtureDef(shape=polygonShape(
                    vertices=((0, 0), (0, LEG_LENGTH / 25),
                              (i * LEG_LENGTH, 0), (i * LEG_LENGTH,
                                                    -LEG_LENGTH / 20),
                              (i * LEG_LENGTH / 3, -LEG_LENGTH / 7))),
                                    density=1,
                                    restitution=0.0,
                                    friction=0.2,
                                    categoryBits=0x0020,
                                    maskBits=0x001))
            leg.ground_contact = False
            leg.color1 = (0.25, 0.25, 0.25)
            rjd = revoluteJointDef(bodyA=self.lander,
                                   bodyB=leg,
                                   localAnchorA=(i * LEG_AWAY,
                                                 ROCKET_WIDTH * 0.2),
                                   localAnchorB=(0, 0),
                                   enableLimit=True,
                                   maxMotorTorque=2500.0,
                                   motorSpeed=-0.05 * i,
                                   enableMotor=True)
            djd = distanceJointDef(bodyA=self.lander,
                                   bodyB=leg,
                                   anchorA=(i * LEG_AWAY, ROCKET_HEIGHT / 8),
                                   anchorB=leg.fixtures[0].body.transform *
                                   (i * LEG_LENGTH, 0),
                                   collideConnected=False,
                                   frequencyHz=0.01,
                                   dampingRatio=0.9)
            if i == 1:
                rjd.lowerAngle = -SPRING_ANGLE
                rjd.upperAngle = 0
            else:
                rjd.lowerAngle = 0
                rjd.upperAngle = +SPRING_ANGLE
            leg.joint = self.world.CreateJoint(rjd)
            leg.joint2 = self.world.CreateJoint(djd)

            self.legs.append(leg)

        self.lander.linearVelocity = (
            -self.np_random.uniform(0, INITIAL_RANDOM) * START_SPEED *
            (initial_x - W / 2) / W, -START_SPEED)

        self.lander.angularVelocity = (1 + INITIAL_RANDOM) * np.random.uniform(
            -1, 1)

        self.drawlist = self.legs + [self.water] + [
            self.ship
        ] + self.containers + [self.lander]

        if CONTINUOUS:
            return self.step([0, 0, 0])[0]
        else:
            return self.step(6)[0]
    def reset(self):
        self._destroy()
        self.world.contactListener_bug_workaround = ContactDetector(self)
        self.world.contactListener = self.world.contactListener_bug_workaround
        self.game_over = False
        self.prev_shaping = None
        self.scroll = 0.0
        self.lidar_render = 0

        W = VIEWPORT_W/SCALE
        H = VIEWPORT_H/SCALE

        self._generate_terrain(self.hardcore)
        self._generate_clouds()

        init_x = TERRAIN_STEP*TERRAIN_STARTPAD/2
        init_y = TERRAIN_HEIGHT+2*LEG_H
        '''
        self.hull = self.world.CreateDynamicBody(
            position = (init_x, init_y),
            fixtures = HULL_FD
                )
        self.hull.ground_contact = False
        self.hull.color1 = (0.5,0.4,0.9)
        self.hull.color2 = (0.3,0.3,0.5)
        self.hull.ApplyForceToCenter((self.np_random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM), 0), True)
        self.hull.connected_body = [1, 3]
        self.hull.connected_joints = [0, 2]
        #'''

        center = (init_x, init_y)
        radius = 0.5
        circle_radius = 0.25
        shape_num = 14
        angularDamping = 0.5
        linearDamping = 0.5
        friction = 0.5
        density = 5.0

        def get_pos(angle):
            return (np.cos(angle * np.pi / 180.0) * radius + center[0],
                    np.sin(angle * np.pi / 180.0) * radius + center[1])
        
        circle = circleShape(radius=circle_radius)
        fixture = fixtureDef(shape=circle, friction=friction, density=density,
                               restitution=0.0)
        
        self.bodies = [
            self.world.CreateDynamicBody(position=get_pos(i), fixtures=fixture) for i in range(0, 360, int(360 / shape_num))]
        self.joints = []
        self.motors = []
        
        prev_body = self.bodies[-1]
        for i, body in enumerate(self.bodies):
            if i % 2 == 0:
                joint = self.world.CreateJoint(distanceJointDef(
                    bodyA=prev_body,
                    bodyB=body,
                    anchorA=prev_body.position,
                    anchorB=body.position,
                    dampingRatio=10.0,
                    frequencyHz=5.0,
                ))
                self.joints.append(joint)
            else:
                joint = self.world.CreateJoint(revoluteJointDef(
                    bodyA=prev_body,
                    bodyB=body,
                    #anchorA=prev_body.position,
                    #anchorB=body.position,
                    localAnchorA=(body.position-prev_body.position)/2.,
                    localAnchorB=(prev_body.position-body.position)/2.,
                    enableMotor=True,
                    enableLimit=False,
                    maxMotorTorque=MOTORS_TORQUE,
                    motorSpeed = 1.,
                ))
                self.motors.append(joint)
                self.joints.append(joint)

            body.color1 = (120/255.,140/255.,175/255.)
            body.color2 = (61/255., 72/255., 90/255.)
            prev_body = body
        self.hull = self.bodies[0]

        self.drawlist = self.terrain + self.bodies

        class LidarCallback(Box2D.b2.rayCastCallback):
            def ReportFixture(self, fixture, point, normal, fraction):
                if (fixture.filterData.categoryBits & 1) == 0:
                    return 1
                self.p2 = point
                self.fraction = fraction
                return 0
        self.lidar = [LidarCallback() for _ in range(10)]

        ob, _, _, info = self.step(np.zeros(self.action_space.shape))
        return ob, info