def __init__(self, car: Car, ball_predictions, predicate: callable = None): self.ball: Ball = None self.is_viable = True #find the first reachable ball slice that also meets the predicate test_car = Car(car) test_aerial = Aerial(car) for ball in ball_predictions: test_aerial.target = ball.position test_aerial.arrival_time = ball.time # fake our car state :D dir_to_target = ground_direction(test_car.position, test_aerial.target) test_car.velocity = dir_to_target * max(norm(test_car.velocity), 1200) test_car.orientation = look_at(dir_to_target, vec3(0,0,1)) if test_aerial.is_viable() and (predicate is None or predicate(car, ball)): self.ball = ball break #if no slice is found, use the last one if self.ball is None: self.ball = ball_predictions[-1] self.is_viable = False self.time = self.ball.time self.ground_pos = ground(self.ball.position) self.position = self.ball.position
def simulate(self, bot) -> vec3: # print('simulate intercept') # Init vars c = Car(bot.game.my_car) b = Ball(bot.game.ball) t = vec3(bot.target) intercept = self.location dt = 1.0 / 60.0 hit = False min_error = None # Drive towards intercept (moving in direction of c.forward()) c.rotation = look_at(intercept, c.up()) direction = normalize(intercept - c.location) #c.forward() advance_distance = norm(intercept - c.location) - c.hitbox( ).half_width[0] - b.collision_radius translation = direction * advance_distance sim_start_state: ThrottleFrame = BoostAnalysis().travel_distance( advance_distance, norm(c.velocity)) c.velocity = direction * sim_start_state.speed c.location += translation c.time += sim_start_state.time bot.ball_predictions = [vec3(b.location)] while b.time < c.time: b.step(dt) bot.ball_predictions.append(vec3(b.location)) # print(c.time, b.time) # print(c.location, b.location) # Simulate the collision and resulting for i in range(60 * 3): c.location += c.velocity * dt b.step(dt, c) # Check if we hit the ball yet if norm(b.location - c.location) < (c.hitbox().half_width[0] + b.collision_radius) * 1.05: hit = True # print('hit') # Measure dist from target error = t - b.location if hit and (min_error == None or norm(error) < norm(min_error)): min_error = error # Record trajectory bot.ball_predictions.append(vec3(b.location)) if not hit: return None return min_error
def reset_gamestate(self): print('> reset_gamestate()') # Initialize inputs self.reset_for_ground_shots() t = self.target b = Ball(self.game.ball) c = Car(self.game.cars[self.index]) b.location = to_vec3(self.initial_ball_location) b.velocity = to_vec3(self.initial_ball_velocity) c.location = to_vec3(self.initial_car_location) c.velocity = to_vec3(self.initial_car_velocity) # Point car at ball c.rotation = look_at( vec3(b.location[0] - c.location[0], b.location[1] - c.location[1], 0), vec3(0, 0, 1)) rotator = rotation_to_euler(c.rotation) # Reset self.aerial = None self.dodge = None self.rotation_input = None self.timer = 0.0 # Set gamestate car_state = CarState(boost_amount=100, physics=Physics( location=self.initial_car_location, velocity=self.initial_car_velocity, rotation=rotator, angular_velocity=Vector3(0, 0, 0))) ball_state = BallState( Physics(location=self.initial_ball_location, velocity=self.initial_ball_velocity, rotation=Rotator(0, 0, 0), angular_velocity=Vector3(0, 0, 0))) game_state = GameState(ball=ball_state, cars={self.index: car_state}) self.set_game_state(game_state)
from rlutilities.simulation import Car, Navigator from rlutilities.mechanics import FollowPath from rlutilities.linear_algebra import vec3, normalize c = Car() c.time = 0.0 c.position = vec3(0, 0, 0) c.velocity = vec3(1000, 0, 0) c.angular_velocity = vec3(0.1, -2.0, 1.2) c.on_ground = False navigator = Navigator(c) drive = FollowPath(c) drive.arrival_speed = 1234 drive.path = navigator.path_to(vec3(1000, 0, 0), vec3(1, 0, 0), 1000) for p in drive.path.points: print(p)
from rlutilities.linear_algebra import vec3, mat3 from rlutilities.simulation import Game, Car, Ball, intersect Game.set_mode("dropshot") c = Car() c.location = vec3(-164.13, 0, 88.79) c.velocity = vec3(1835.87, 0, 372.271) c.angular_velocity = vec3(0, 3.78721, 0) c.rotation = mat3(0.9983, -5.23521e-6, 0.0582877, 5.5498e-6, 1.0, -5.23521e-6, -0.0582877, 5.5498e-6, 0.9983) b = Ball() b.location = vec3(0, 0, 150) b.velocity = vec3(0, 0, 0) b.angular_velocity = vec3(0, 0, 0) print("before:") print(b.location) print(b.velocity) print(b.angular_velocity) print("overlapping: ", intersect(c.hitbox(), b.hitbox())) print() b.step(0.008333, c) print("after:") print(b.location)
def calculate(car: Car, ball: Ball, target: vec3, ball_predictions=None): # Init vars b = Ball(ball) dt = 1.0 / 60.0 # Generate predictions of ball path if ball_predictions is None: ball_predictions = [] for i in range(60 * 5): b.step(dt) ball_predictions.append(vec3(b.location)) # Gradually converge on ball location by aiming at a location, checking time to that location, # and then aiming at the ball's NEW position. Guaranteed to converge (typically in <10 iterations) # unless the ball is moving away from the car faster than the car's max boost speed intercept = Intercept(b.location) intercept.purpose = 'ball' intercept.boost = True intercept_ball_position = vec3(b.location) collision_achieved = False last_horizontal_error = None last_horizontal_offset = None i = 0 max_tries = 101 analyzer = BoostAnalysis() if intercept.boost else ThrottleAnalysis() while i < max_tries: i += 1 fake_car = Car(car) direction = normalize(intercept.location - car.location) fake_car.rotation = look_at(direction, fake_car.up()) for t in range(60 * 5): # Step car location with throttle/boost analysis data # Not super efficient but POITROAE frame = analyzer.travel_time(dt, norm(fake_car.velocity)) # print('in 1 frame I travel', frame.time, frame.distance, frame.speed) fake_car.location += direction * frame.distance fake_car.velocity = direction * frame.speed fake_car.time += dt ball_location = ball_predictions[t] # Check for collision p = closest_point_on_obb(fake_car.hitbox(), ball_location) if norm(p - ball_location) <= ball.collision_radius: direction_vector = p - (fake_car.location - normalize( fake_car.forward()) * 13.88) # octane center of mass direction_vector[2] = 0 target_direction_vector = target - ball_location target_direction_vector[2] = 0 intercept_ball_position = ball_location direction = atan2(direction_vector[1], direction_vector[0]) ideal_direction = atan2(target_direction_vector[1], target_direction_vector[0]) horizontal_error = direction - ideal_direction # intercept.location = vec3(ball_location) # intercept.time = fake_car.time # return intercept # Now descend the hit direction gradient # Kick off the gradient descent with an arbitrary seed value if last_horizontal_error is None: last_horizontal_error = horizontal_error last_horizontal_offset = 0 if horizontal_error > 0: horizontal_offset = 25 else: horizontal_offset = 25 intercept.location = ball_location - normalize( fake_car.left()) * horizontal_offset break # Recursive case of gradient descent if horizontal_offset == last_horizontal_offset: gradient = 0 else: gradient = (horizontal_error - last_horizontal_error ) / (horizontal_offset - last_horizontal_offset) if gradient == 0: predicted_horizontal_offset = horizontal_offset else: predicted_horizontal_offset = horizontal_offset - horizontal_error / gradient # Base case (convergence) if abs(gradient) < 0.0005: print(f'convergence in {i} iterations') print(f'gradient = {gradient}') print( f'last_horizontal_offset = {last_horizontal_offset}' ) print(f'direction = {degrees(direction)}') print(f'ideal direction = {degrees(ideal_direction)}') print(f'target = {target}') print(f'ball_location = {ball_location}') return intercept # Edge case exit: offset maxed out max_horizontal_offset = car.hitbox( ).half_width[1] + ball.collision_radius if predicted_horizontal_offset > max_horizontal_offset: predicted_horizontal_offset = max_horizontal_offset elif predicted_horizontal_offset < -max_horizontal_offset: predicted_horizontal_offset = -max_horizontal_offset last_horizontal_offset = horizontal_offset last_horizontal_error = horizontal_error horizontal_offset = predicted_horizontal_offset # Return the latest intercept location and continue descending the gradient intercept.location = ball_location - normalize( fake_car.left()) * predicted_horizontal_offset print(f'iteration {i}') print(f'gradient = {gradient}') print(f'horizontal_offset = {horizontal_offset}') print(f'horizontal_error = {degrees(horizontal_error)}') # print(f'ideal direction = {degrees(ideal_direction)}') break # Check for arrival if norm(fake_car.location - intercept.location) < ball.collision_radius / 2: intercept.location = ball_location break if i >= max_tries: print( f'Warning: max tries ({max_tries}) exceeded for calculating intercept' ) return intercept
from rlutilities.linear_algebra import vec3, axis_to_rotation, look_at from rlutilities.mechanics import AerialTurn from rlutilities.simulation import Car c = Car() c.time = 0.0 c.location = vec3(0, 0, 500) c.velocity = vec3(0, 0, 0) c.angular_velocity = vec3(0.1, -2.0, 1.2) c.rotation = axis_to_rotation(vec3(1.7, -0.5, 0.3)) c.on_ground = False turn = AerialTurn(c) turn.target = look_at(vec3(1, 0, 0), vec3(0, 0, 1)) turn.step(0.0166) print(turn.controls.roll) print(turn.controls.pitch) print(turn.controls.yaw) simulation = turn.simulate() print(simulation.time)
from rlutilities.linear_algebra import vec3, vec2, mat3, mat2 from rlutilities.mechanics import Dodge from rlutilities.simulation import Car c = Car() c.time = 0.0 c.location = vec3(1509.38, -686.19, 17.01) c.velocity = vec3(-183.501, 1398., 8.321) c.angular_velocity = vec3(0, 0, 0) c.rotation = mat3(-0.130158, -0.991493, -0.00117062, 0.991447, -0.130163, 0.00948812, -0.00955977, 0.0000743404, 0.999954) c.dodge_rotation = mat2(-0.130163, -0.991493, 0.991493, -0.130163) c.on_ground = True c.jumped = False c.double_jumped = False c.jump_timer = -1.0 c.dodge_timer = -1.0 dodge = Dodge(c) dodge.direction = vec2(-230.03, 463.42) dodge.duration = 0.1333 dodge.delay = 0.35 f = open("dodge_simulation.csv", "w") for i in range(300): dodge.step(0.008333) print(c.time, dodge.controls.jump, dodge.controls.pitch, dodge.controls.yaw) c.step(dodge.controls, 0.008333)