def calculate_controller_state(self, request): ball_location = Vector2(request.ball.location.x, request.ball.location.y) my_car = request.players[request.player_index] car_location = Vector2(my_car.location.x, my_car.location.y) car_direction = get_car_facing_vector(my_car) car_to_ball = ball_location - car_location steer_correction_radians = car_direction.correction_to(car_to_ball) steer_magnitude = abs(steer_correction_radians) if steer_correction_radians > 0: # Positive radians in the unit circle is a turn to the left. turn = -1.0 # Negative value for a turn to the left. else: turn = 1.0 controls = game_data_pb2.ControllerState() controls.throttle = 1 controls.steer = turn if steer_magnitude > math.pi / 12 else turn * .2 controls.boost = 1 if steer_magnitude < math.pi / 12 else 0 controls.handbrake = 1 if steer_magnitude > math.pi / 2 else 0 return controls
def GetControllerState(self, request, context): try: if request.player_index < len(request.players): return self.calculate_controller_state(request) except Exception as e: print('Exception running bot: ' + str(e)) pass return game_data_pb2.ControllerState( ) # Return neutral input because we failed.
def calculate_controller_state(self, request): controller_state = game_data_pb2.ControllerState() player = request.players[request.player_index] ball_x = request.ball.location.x ball_y = request.ball.location.y player_y = player.location.y player_x = player.location.x player_nose_y = math.cos(player.rotation.pitch) * math.cos( player.rotation.yaw) player_nose_x = math.cos(player.rotation.pitch) * math.sin( player.rotation.yaw) # Need to handle atan2(0,0) case, aka straight up or down, eventually player_front_direction_in_radians = math.atan2(player_nose_y, player_nose_x) relative_angle_to_ball_in_radians = math.atan2((ball_x - player_x), (ball_y - player_y)) if (not (abs(player_front_direction_in_radians - relative_angle_to_ball_in_radians) < math.pi)): # Add 2pi to negative values if (player_front_direction_in_radians < 0): player_front_direction_in_radians += 2 * math.pi if (relative_angle_to_ball_in_radians < 0): relative_angle_to_ball_in_radians += 2 * math.pi if (relative_angle_to_ball_in_radians > player_front_direction_in_radians): controller_state.steer = -1 else: controller_state.steer = 1 controller_state.throttle = 1 return controller_state