def car_trajectory(self, car: Car, end_time: float, dt: float = 1 / 10): steps = [] test_car = Car(car) while test_car.time < end_time: dt = min(dt, end_time - test_car.time) test_car.step(Input(), dt) test_car.time += dt steps.append(vec3(test_car.pos)) self.polyline(steps)
def estimate_time(car: Car, target, speed, dd=1) -> float: dist = distance(car, target) if dist < 100: return 0 travel = dist / speed turning = angle_between(car.forward() * dd, direction( car, target)) / math.pi * 2 if turning < 1: turning **= 2 acceleration = (speed * dd - dot(car.vel, car.forward())) / 2100 * 0.6 * dd return travel + acceleration + turning * 0.7
def __init__(self, index, team, field_info=None): self.time = 0 self.ball = Ball() self.pitch = Pitch() if field_info is None: self.my_goal = Goal(team) self.their_goal = Goal(1 - team) self.boost_pads = [ BoostPad(3, vec3(-3072.0, -4096.0, 73.0), True, 0.0), BoostPad(4, vec3(3072.0, -4096.0, 73.0), True, 0.0), BoostPad(15, vec3(-3584.0, 0.0, 73.0), True, 0.0), BoostPad(18, vec3(3584.0, 0.0, 73.0), True, 0.0), BoostPad(29, vec3(-3072.0, 4096.0, 73.0), True, 0.0), BoostPad(30, vec3(3072.0, 4096.0, 73.0), True, 0.0) ] self.small_boost_pads = [] else: self.my_goal = Goal(team, field_info) self.their_goal = Goal(1 - team, field_info) self.boost_pads = [] self.small_boost_pads = [] for i in range(field_info.num_boosts): current = field_info.boost_pads[i] if field_info.boost_pads[i].is_full_boost: self.boost_pads.append( BoostPad( i, vec3(current.location.x, current.location.y, current.location.z), True, 0.0)) else: self.small_boost_pads.append( BoostPad( i, vec3(current.location.x, current.location.y, current.location.z), True, 0.0)) self.team = team self.index = index self.cars = [] self.teammates = [] self.opponents = [] self.my_car = Car() self.ball_predictions = [] self.about_to_score = None self.about_to_be_scored_on = None self.time_of_goal = -1
def find_landing_orientation(self, num_points): f = vec3(0, 0, 0) l = vec3(0, 0, 0) u = vec3(0, 0, 0) dummy = Car(self.car) self.trajectory.append(vec3(dummy.pos)) for i in range(0, num_points): dummy.step(Input(), 0.0333) self.trajectory.append(vec3(dummy.pos)) u = dummy.pitch_surface_normal() if norm(u) > 0.0 and i > 10: f = normalize(dummy.vel - dot(dummy.vel, u) * u) l = normalize(cross(u, f)) self.found = True break if self.found: self.target = mat3(f[0], l[0], u[0], f[1], l[1], u[1], f[2], l[2], u[2]) else: self.target = self.car.theta
def best_intercept(self, cars, max_height=9999) -> Intercept: best_intercept = None best_car = None for car in cars: intercept = Intercept(car, self.info.ball_predictions, lambda car, ball: ball.pos[2] < max_height) if best_intercept is None or intercept.time <= best_intercept.time: best_intercept = intercept best_car = car if best_intercept is None: best_car = Car() best_intercept = Intercept(best_car, []) return best_intercept, best_car
def read_packet(self, packet): self.time = packet.game_info.seconds_elapsed dyn = packet.game_ball.physics self.ball.pos = vec3(dyn.location.x, dyn.location.y, dyn.location.z) self.ball.vel = vec3(dyn.velocity.x, dyn.velocity.y, dyn.velocity.z) self.ball.omega = vec3(dyn.angular_velocity.x, dyn.angular_velocity.y, dyn.angular_velocity.z) self.ball.t = self.time self.ball.step(GameInfo.DT) for i in range(0, packet.num_cars): game_car = packet.game_cars[i] dyn = game_car.physics car = Car() if i < len(self.cars): car = self.cars[i] car.pos = vec3(dyn.location.x, dyn.location.y, dyn.location.z) car.vel = vec3(dyn.velocity.x, dyn.velocity.y, dyn.velocity.z) car.omega = vec3(dyn.angular_velocity.x, dyn.angular_velocity.y, dyn.angular_velocity.z) car.theta = euler_rotation( vec3(dyn.rotation.pitch, dyn.rotation.yaw, dyn.rotation.roll)) car.on_ground = game_car.has_wheel_contact car.supersonic = game_car.is_super_sonic car.jumped = game_car.jumped car.double_jumped = game_car.double_jumped car.boost = game_car.boost car.time = self.time car.extrapolate(GameInfo.DT) if len(self.cars) <= i: car.id = i car.team = game_car.team self.cars.append(car) if game_car.team == self.team: if i == self.index: self.my_car = car else: self.teammates.append(car) else: self.opponents.append(car) for i in range(0, len(self.boost_pads)): boost_pad = packet.game_boosts[self.boost_pads[i].index] self.boost_pads[i].is_active = boost_pad.is_active self.boost_pads[i].timer = boost_pad.timer for i in range(0, len(self.small_boost_pads)): boost_pad = packet.game_boosts[self.small_boost_pads[i].index] self.small_boost_pads[i].is_active = boost_pad.is_active self.small_boost_pads[i].timer = boost_pad.timer self.time += GameInfo.DT
from RLUtilities.Simulation import Car, Ball from RLUtilities.LinearAlgebra import vec3, dot from RLUtilities.Maneuvers import AerialTurn, Aerial c = Car() b = Ball() b.pos = vec3(0, 0, 1000) b.vel = vec3(0, 0, 0) b.omega = vec3(0, 0, 0) b.t = 0 action = Aerial(c, b.pos, b.t) b.step(0.0166) print(action.t_arrival) print(action.target)
def get_output(self, packet: GameTickPacket) -> SimpleControllerState: self.info.read_packet(packet) self.controls = SimpleControllerState() if self.timer == 0.0: self.csign = random.choice([-1, 1]) # this just initializes the car and ball # to different starting points each time c_position = Vector3(random.uniform(-1000, 1000), random.uniform(-4500, -4000), 25) car_state = CarState(physics=Physics( location=c_position, velocity=Vector3(0, 1000, 0), rotation=Rotator(0, 1.5 * self.csign, 0), angular_velocity=Vector3(0, 0, 0) )) self.bsign = random.choice([-1, 1]) b_position = Vector3(random.uniform(-3500, -3000) * self.bsign, random.uniform(-1500, 1500), random.uniform( 150, 500)) b_velocity = Vector3(random.uniform( 1000, 1500) * self.bsign, random.uniform(- 500, 500), random.uniform( 1000, 1500)) ball_state = BallState(physics=Physics( location=b_position, velocity=b_velocity, rotation=Rotator(0, 0, 0), angular_velocity=Vector3(0, 0, 0) )) self.set_game_state(GameState( ball=ball_state, cars={self.index: car_state}) ) self.increment_timer = True if self.timer < 0.3: self.controls.throttle = 1 * self.csign else: if self.action == None: # set empty values for target and t_arrival initially self.action = Aerial(self.info.my_car, vec3(0,0,0), 0) # predict where the ball will be prediction = Ball(self.info.ball) self.ball_predictions = [vec3(prediction.pos)] for i in range(150): prediction.step(0.016666) prediction.step(0.016666) self.ball_predictions.append(vec3(prediction.pos)) # if the ball is in the air if prediction.pos[2] > 100: self.action.target = prediction.pos self.action.t_arrival = prediction.t # check if we can reach it by an aerial if self.action.is_viable(): break nsteps = 30 delta_t = self.action.t_arrival - self.info.my_car.time prediction = Car(self.info.my_car) self.car_predictions = [vec3(prediction.pos)] for i in range(nsteps): prediction.step(Input(), delta_t / nsteps) self.car_predictions.append(vec3(prediction.pos)) r = 200 self.renderer.begin_rendering() purple = self.renderer.create_color(255, 230, 30, 230) white = self.renderer.create_color(255, 230, 230, 230) x = vec3(r,0,0) y = vec3(0,r,0) z = vec3(0,0,r) target = self.action.target future = self.action.target - self.action.A self.renderer.draw_polyline_3d(self.ball_predictions, purple) self.renderer.draw_line_3d(target - x, target + x, purple) self.renderer.draw_line_3d(target - y, target + y, purple) self.renderer.draw_line_3d(target - z, target + z, purple) self.renderer.draw_polyline_3d(self.car_predictions, white) self.renderer.draw_line_3d(future - x, future + x, white) self.renderer.draw_line_3d(future - y, future + y, white) self.renderer.draw_line_3d(future - z, future + z, white) self.renderer.end_rendering() self.action.step(1.0 / 60.0) self.controls = self.action.controls if self.action.finished or self.timer > 10.0: self.timer = 0.0 self.action = None self.increment_timer = False self.predictions = [] if self.increment_timer: self.timer += 1.0 / 60.0 self.info.my_car.last_input.roll = self.controls.roll self.info.my_car.last_input.pitch = self.controls.pitch self.info.my_car.last_input.yaw = self.controls.yaw self.info.my_car.last_input.boost = self.controls.boost return self.controls