33from rlbot .agents .base_agent import BaseAgent , SimpleControllerState
44from rlbot .utils .structures .game_data_struct import GameTickPacket
55
6+ from util .orientation import Orientation
7+ from util .vec import Vec3
8+
69
710class PythonExample (BaseAgent ):
811
912 def initialize_agent (self ):
10- #This runs once before the bot starts up
13+ # This runs once before the bot starts up
1114 self .controller_state = SimpleControllerState ()
1215
1316 def get_output (self , packet : GameTickPacket ) -> SimpleControllerState :
14- ball_location = Vector2 (packet .game_ball .physics .location . x , packet . game_ball . physics . location . y )
17+ ball_location = Vec3 (packet .game_ball .physics .location )
1518
1619 my_car = packet .game_cars [self .index ]
17- car_location = Vector2 (my_car .physics .location . x , my_car . physics . location . y )
18- car_direction = get_car_facing_vector ( my_car )
20+ car_location = Vec3 (my_car .physics .location )
21+
1922 car_to_ball = ball_location - car_location
2023
21- steer_correction_radians = car_direction .correction_to (car_to_ball )
24+ # Find the direction of our car using the Orientation class
25+ car_orientation = Orientation (my_car .physics .rotation )
26+ car_direction = car_orientation .forward
27+
28+ steer_correction_radians = find_correction (car_direction , car_to_ball )
2229
2330 if steer_correction_radians > 0 :
2431 # Positive radians in the unit circle is a turn to the left.
@@ -35,42 +42,25 @@ def get_output(self, packet: GameTickPacket) -> SimpleControllerState:
3542
3643 return self .controller_state
3744
38- class Vector2 :
39- def __init__ (self , x = 0 , y = 0 ):
40- self .x = float (x )
41- self .y = float (y )
42-
43- def __add__ (self , val ):
44- return Vector2 (self .x + val .x , self .y + val .y )
4545
46- def __sub__ ( self , val ) :
47- return Vector2 ( self . x - val . x , self . y - val . y )
46+ def find_correction ( current : Vec3 , ideal : Vec3 ) -> float :
47+ # Finds the angle from current to ideal vector in the xy-plane. Angle will be between -pi and +pi.
4848
49- def correction_to (self , ideal ):
50- # The in-game axes are left handed, so use -x
51- current_in_radians = math .atan2 (self .y , - self .x )
52- ideal_in_radians = math .atan2 (ideal .y , - ideal .x )
49+ # The in-game axes are left handed, so use -x
50+ current_in_radians = math .atan2 (current .y , - current .x )
51+ ideal_in_radians = math .atan2 (ideal .y , - ideal .x )
5352
54- correction = ideal_in_radians - current_in_radians
53+ diff = ideal_in_radians - current_in_radians
5554
56- # Make sure we go the 'short way'
57- if abs (correction ) > math .pi :
58- if correction < 0 :
59- correction += 2 * math .pi
60- else :
61- correction -= 2 * math .pi
62-
63- return correction
64-
65-
66- def get_car_facing_vector (car ):
67- pitch = float (car .physics .rotation .pitch )
68- yaw = float (car .physics .rotation .yaw )
55+ # Make sure that diff is between -pi and +pi.
56+ if abs (diff ) > math .pi :
57+ if diff < 0 :
58+ diff += 2 * math .pi
59+ else :
60+ diff -= 2 * math .pi
6961
70- facing_x = math .cos (pitch ) * math .cos (yaw )
71- facing_y = math .cos (pitch ) * math .sin (yaw )
62+ return diff
7263
73- return Vector2 (facing_x , facing_y )
7464
7565def draw_debug (renderer , car , ball , action_display ):
7666 renderer .begin_rendering ()
0 commit comments