def optimized_rear_sensor(self, car, act_mask, display_obstacle_on_sensor=False):
        """
        rear visual sensor
        :param car:
        :param object_mask:
        :param act_mask:
        :param display_obstacle_on_sensor:
        :return:
        """
        # act_mask is a separate image where you can only see what the sensor sees
        position = self.global_cars_positions[car.car_tag]
        center = Collision.calculate_center_for_car(car, position)
        if car.car_tag == self.current_car:
            center = Collision.center_rect(self.screen_width, self.screen_height)
        mid_of_rear_axle = Collision.point_rotation(car, 65, 16, center)

        arc_points = get_arc_points(mid_of_rear_axle, 150, radians(-90 + car.angle), radians(90 + car.angle),
                                    self.sensor_size)

        draw_center = Collision.center_rect(self.screen_width, self.screen_height)
        draw_mid_rear_axle = Collision.point_rotation(car, 65, 16, draw_center)
        draw_arc_points = get_arc_points(draw_mid_rear_axle, 150, radians(-90 + car.angle), radians(90 + car.angle),
                                         self.sensor_size)

        offroad_edge_points = []
        draw_offroad_edge_points = []

        for end_point, draw_end_point in zip(arc_points, draw_arc_points):
            points_to_be_checked = list(get_equidistant_points(mid_of_rear_axle, end_point, 25))
            draw_points_to_be_checked = list(get_equidistant_points(draw_mid_rear_axle, draw_end_point, 25))

            check = False

            for line_point, draw_line_point in zip(points_to_be_checked, draw_points_to_be_checked):
                try:
                    if np.array_equal(self.object_mask.get_at((int(line_point[0]), int(line_point[1]))), self.bkd_color):
                        check = True
                        break
                except:
                    check = True
                    break

            if check is False:
                offroad_edge_points.append(end_point)
                draw_offroad_edge_points.append(draw_end_point)
            else:
                offroad_edge_points.append(line_point)
                draw_offroad_edge_points.append(draw_line_point)

        for index in range(0, len(arc_points)):
            if offroad_edge_points[index] == arc_points[index]:
                pygame.draw.line(self.screen, (0, 255, 0), mid_of_rear_axle, arc_points[index], True)
                pygame.draw.line(act_mask, (0, 255, 0), draw_mid_rear_axle, draw_arc_points[index], True)
            else:
                pygame.draw.line(self.screen, (0, 255, 0), mid_of_rear_axle, offroad_edge_points[index], True)
                pygame.draw.line(act_mask, (0, 255, 0), draw_mid_rear_axle, draw_offroad_edge_points[index], True)
                if display_obstacle_on_sensor is True:
                    pygame.draw.line(self.screen, (255, 0, 0), offroad_edge_points[index], arc_points[index], True)
                    pygame.draw.line(act_mask, (255, 0, 0), draw_offroad_edge_points[index], draw_arc_points[index], True)
    def optimized_front_sensor(self):
        # act_mask is a separate image where you can only see what the sensor sees
        center_rect = Collision.center_rect(self.screen_width,
                                            self.screen_height)
        mid_of_front_axle = Collision.point_rotation(self.car, -1, 16,
                                                     center_rect)

        arc_points = get_arc_points(mid_of_front_axle, 150,
                                    radians(90 + self.car.angle),
                                    radians(270 + self.car.angle), 80)

        offroad_edge_points = []

        for end_point in arc_points:
            points_to_be_checked = list(
                get_equidistant_points(mid_of_front_axle, end_point, 25))

            check = False

            for line_point in points_to_be_checked:
                if np.array_equal(
                        self.screen.get_at(
                            (int(line_point[0]), int(line_point[1]))),
                        self.obstacle_color):
                    check = True
                    break

            if check is False:
                offroad_edge_points.append(end_point)
            else:
                offroad_edge_points.append(line_point)

        return mid_of_front_axle, arc_points, offroad_edge_points
Exemple #3
0
    def optimized_front_sensor(self,
                               act_mask,
                               display_obstacle_on_sensor=False):
        """
        front visual sensor
        :param act_mask:
        :param display_obstacle_on_sensor:
        :return:
        """
        # act_mask is a separate image where you can only see what the sensor sees
        center_rect = Collision.center_rect(self.screen_width,
                                            self.screen_height)
        mid_of_front_axle = Collision.point_rotation(self.car, -1, 16,
                                                     center_rect)

        arc_points = get_arc_points(mid_of_front_axle, 150,
                                    radians(90 + self.car.angle),
                                    radians(270 + self.car.angle),
                                    self.sensor_size)

        offroad_edge_points = []

        for end_point in arc_points:
            points_to_be_checked = list(
                get_equidistant_points(mid_of_front_axle, end_point, 25))

            check = False

            for line_point in points_to_be_checked:
                if np.array_equal(
                        self.object_mask.get_at(
                            (int(line_point[0]), int(line_point[1]))),
                        self.bkd_color):
                    check = True
                    break

            if check is False:
                offroad_edge_points.append(end_point)
            else:
                offroad_edge_points.append(line_point)

        for index in range(0, len(arc_points)):
            if offroad_edge_points[index] == arc_points[index]:
                pygame.draw.line(self.screen, (0, 255, 0), mid_of_front_axle,
                                 arc_points[index], True)
                pygame.draw.line(act_mask, (0, 255, 0), mid_of_front_axle,
                                 arc_points[index], True)
            else:
                pygame.draw.line(self.screen, (0, 255, 0), mid_of_front_axle,
                                 offroad_edge_points[index], True)
                pygame.draw.line(act_mask, (0, 255, 0), mid_of_front_axle,
                                 offroad_edge_points[index], True)
                if display_obstacle_on_sensor is True:
                    pygame.draw.line(self.screen, (255, 0, 0),
                                     offroad_edge_points[index],
                                     arc_points[index], True)
                    pygame.draw.line(act_mask, (255, 0, 0),
                                     offroad_edge_points[index],
                                     arc_points[index], True)
Exemple #4
0
 def enable_sensor(self, car, draw_screen, rays_nr):
     """
     distance sensor
     :param car:
     :param data_screen:
     :param draw_screen:
     :param rays_nr:
     :return:
     """
     center_rect = Collision.center_rect(self.screen_width,
                                         self.screen_height)
     mid_of_front_axle = Collision.point_rotation(car, 0, 16, center_rect)
     distance = np.array([])
     for angle_index in range(120, 240, int(round(120 / rays_nr))):
         distance = np.append(
             distance,
             self.compute_sensor_distance(car, mid_of_front_axle, 200,
                                          angle_index, self.object_mask,
                                          draw_screen))
     return distance
    def enable_rear_sensor(self, car, draw_screen, rays_nr):
        """
        rear distance sensor
        :param car:
        :param data_screen:
        :param draw_screen:
        :param rays_nr:
        :return:
        """

        position = self.global_cars_positions[car.car_tag]
        center = Collision.calculate_center_for_car(car, position)
        if car.car_tag == self.current_car:
            center = Collision.center_rect(self.screen_width, self.screen_height)
        mid_of_rear_axle = Collision.point_rotation(car, 65, 16, center)

        distance = np.array([])
        for angle_index in range(120, 240, int(round(120/rays_nr))):
            distance = np.append(distance,
                                 self.compute_sensor_distance(car, mid_of_rear_axle, 200, angle_index, self.object_mask,
                                                              draw_screen, side='rear'))
        return distance