Ejemplo n.º 1
0
    def _generate_path_with_ball_position(self, packet: GameTickPacket,
                                          ball: Vector3) -> List[Vector3]:
        car = Vector3(packet.game_cars[self.agent.index].physics.location)

        if pathing.in_shooting_cone(car, ball,
                                    -1 * calculations.sign(self.agent.team)):
            # Car is in the shooting cone, so make a straight path
            self.path = pathing.linear_bezier(car, ball)
        else:
            # If the direction of the car and the desired ball direction are pointing to different sides of the line
            # between the car and the ball, use a quadratic bezier. Else use a cubic bezier.

            yaw: float = packet.game_cars[
                self.agent.index].physics.rotation.yaw
            car_dir: Vector3 = Vector3(math.cos(yaw), math.sin(yaw), 0)
            desired_ball_dir: Vector3 = Vector3(
                self.agent.game_info.their_goal.center).modified(
                    z=ball.z) - ball
            car_to_ball: Vector3 = ball - car

            car_dir_right_of_line: bool = Vector3.dot_product(
                car_to_ball, car_dir.modified(y=-car_dir.y)) > 0
            ball_dir_right_of_line: bool = Vector3.dot_product(
                car_to_ball,
                desired_ball_dir.modified(y=-desired_ball_dir.y)) > 0

            if car_dir_right_of_line != ball_dir_right_of_line:
                # Quadratic bezier curve
                # Find intersection of the car direction and the desired ball direction for intermediate point of bezier curve.
                intermediate: Vector3 = calculations.line_line_intersection(
                    car, car + car_dir, ball, desired_ball_dir)
                self.path = pathing.quadratic_bezier(car, intermediate, ball)

                color = self.agent.renderer.red(
                ) if self.agent.team else self.agent.renderer.cyan()
                self.agent.renderer.draw_line_3d(car, intermediate, color)
                self.agent.renderer.draw_line_3d(intermediate, ball, color)
                self.agent.renderer.draw_string_3d(car, 1, 1, "P0", color)
                self.agent.renderer.draw_string_3d(intermediate, 1, 1, "P1",
                                                   color)
                self.agent.renderer.draw_string_3d(ball, 1, 1, "P2", color)
            else:
                # Cubic bezier
                # TODO: Implement cubic bezier path. Using linear curve in place of cubic bezier for now.
                self.path = pathing.linear_bezier(car, ball)

        return self.path
Ejemplo n.º 2
0
def simple_aim(position: Vector3, yaw: float, target: Vector3) -> float:
    """Bang bang controller for steering."""
    pos_to_target: Vector3 = target - position
    facing: Vector3 = Vector3(math.cos(yaw), math.sin(yaw), 0)
    self_right: Vector3 = Vector3.cross_product(facing, Vector3(0, 0, 1))

    if Vector3.dot_product(self_right, pos_to_target) < 0:
        return 1.0
    else:
        return -1.0