def preprocess(self, gamePacket: GameTickPacket): """Calculates a set of values that may be useful. This function runs every tick, so it should not contain any operations that are slow. Additionally, the operations should be limited to what is necessary to have on every tick. Args: gamePacket (GameTickPacket): set of current information about the game Returns: This function updates the attributes of the class and therefore has no return type. """ #load data about self self.me.location = Vec3(gamePacket.game_cars[self.index].physics.location) self.me.velocity = Vec3(gamePacket.game_cars[self.index].physics.velocity) self.me.rotation = Orientation(gamePacket.game_cars[self.index].physics.rotation) self.me.rvelocity = Vec3(gamePacket.game_cars[self.index].physics.angular_velocity) self.me.boost = gamePacket.game_cars[self.index].boost #load data about the ball self.ball.location = Vec3(gamePacket.game_ball.physics.location) self.ball.velocity = Vec3(gamePacket.game_ball.physics.velocity) self.ball.rotation = Orientation(gamePacket.game_ball.physics.rotation) self.ball.rvelocity = Vec3(gamePacket.game_ball.physics.angular_velocity) self.ball.local_location = relative_location(self.me.location, self.me.rotation, self.ball.location)
def brake_and_orient(self, my_car, goal, ball): # Make car break self.controller_state.boost = False self.controller_state.hand_brake = True self.controller_state.throttle = 0.0 # Calculate car reorientation car_orientation = Orientation(my_car.physics.rotation) car_direction = car_orientation.forward car_yaw = my_car.physics.rotation.yaw ball_future_location = get_ball_predicted_pos(self, 30) car_to_ball = ball_future_location - my_car.physics.location steer_correction_radians = find_correction(car_direction, car_to_ball, car_yaw) if my_car.physics.velocity.x <= 30.0 and my_car.physics.velocity.y <= 30.0: if steer_correction_radians < math.radians(-10): # If the target is more than 10 degrees right from the centre, steer left turn = -1 elif steer_correction_radians > math.radians(10): # If the target is more than 10 degrees left from the centre, steer right turn = 1 else: # If the target is less than 10 degrees from the centre, steer straight turn = 0 self.controller_state.hand_brake = False self.controller_state.throttle = 1.0 self.controller_state.steer = turn
def get_output(self, packet: GameTickPacket) -> SimpleControllerState: ball_location = Vec3(packet.game_ball.physics.location) my_car = packet.game_cars[self.index] car_location = Vec3(my_car.physics.location) car_to_ball = ball_location - car_location # Find the direction of our car using the Orientation class car_orientation = Orientation(my_car.physics.rotation) car_direction = car_orientation.forward steer_correction_radians = find_correction(car_direction, car_to_ball) 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. action_display = "turn left" else: turn = 1.0 action_display = "turn right" self.controller_state.throttle = 1.0 self.controller_state.steer = turn draw_debug(self.renderer, my_car, packet.game_ball, action_display) return self.controller_state
def tick(self, info: MyInfo): print("first {}".format(self.done)) target = Vec3(self.info.ball_path[0].physics.location) relative = relative_location(Vec3(self.info.car.loc), Orientation(self.car.rot), target) if self.start_time + FLIP_TIME > info.seconds_elapsed: print("first jump") self.controls.jump = True #self.start_time = self.info.seconds_elapsed elif self.start_time + 2 * FLIP_TIME > self.info.seconds_elapsed: print("not jump") self.controls.jump = False self.controls.yaw = cos(relative.y / relative.x) elif self.start_time + 3 * FLIP_TIME > info.seconds_elapsed: print("second jump") if relative.x > 0: self.controls.pitch = -1 else: self.controls.pitch = 1 self.controls.yaw = cos(relative.y / relative.x) self.controls.jump = True if self.new_touch: # or self.start_time + 1.1 < info.seconds_elapsed: #print("start time: {}, current time: {}".format(self.start_time, info.seconds_elapsed)) self.done = True print(self.done) return self.controls
def get_output(self, packet: GameTickPacket) -> SimpleControllerState: ball_location = Vec3(packet.game_ball.physics.location) goal_location = Vec3( self.get_field_info().goals[not self.team].location) my_car = packet.game_cars[self.index] car_location = Vec3(my_car.physics.location) push_vec = (ball_location - goal_location).rescale(-70) car_to_ball = ball_location - car_location # - push_vec # Find the direction of our car using the Orientation class car_orientation = Orientation(my_car.physics.rotation) car_direction = car_orientation.forward steer_correction_radians = find_correction(car_direction, car_to_ball) self.controller_state.throttle = 1.0 self.controller_state.steer = clamp1(steer_correction_radians * -3) self.controller_state.handbrake = abs( steer_correction_radians) > math.pi * 0.5 self.controller_state.boost = abs(steer_correction_radians) < 0.15 draw_debug(self.renderer, my_car, packet.game_ball) return self.controller_state
def stuck_on_ground( physics: SimPhysics, controls: SimpleControllerState, dt: float, collision, renderer=None, ): # just keep it above ground and slight start rotating height = 36.16 / 2 normal = rlu_to_Vec3(collision.direction) normal_base = rlu_to_Vec3(collision.start) physics.velocity += Vec3(0, 0, -650 * dt) remove_velocity_against_normal(physics.velocity, normal) physics.location += physics.velocity * dt if (physics.location - normal_base).length() < height * 0.95: physics.location = normal_base + normal * height * 0.99 # update orientation so that in 0.5 seconds we have correct orientation orientation = Orientation(physics.rotation) physics.rotation = rotate_by_axis(orientation, orientation.up, normal, dt / 0.5) clamp(physics) return physics
def block_shot(self, my_car, goal, ball): # Calculate car reorientation car_orientation = Orientation(my_car.physics.rotation) car_direction = car_orientation.forward goal_direction = Vec3(goal.direction) angle_to_goal = car_direction.ang_to(goal_direction) ball_future_location = get_ball_predicted_pos(self, 30) my_car_location = Vec3(my_car.physics.location) car_to_ball = my_car_location.dist(ball_future_location) if angle_to_goal < math.pi / 2 and angle_to_goal > 0: print("< pi/2 and > 0 jumpin left") self.controller_state.jump = True self.controller_state.steer = -1 elif angle_to_goal > math.pi / 2 and angle_to_goal < math.pi: print("< pi and > pi/2 jumpin right") self.controller_state.jump = True self.controller_state.steer = 1 else: self.controller_state.jump = False if car_to_ball < 100: print("less than 100") self.controller_state.jump = True else: self.controller_state.jump = False
def get_output(self, packet: GameTickPacket) -> SimpleControllerState: secondsElapsed = packet.game_info.seconds_elapsed myCar = packet.game_cars[self.index] loc = packet.game_cars[0].physics.location curve = [loc] for i in range(0, 200): curve += [] car_location = Vec3(my_car.physics.location) car_to_target = self.target - car_location if car_location.dist(self.target) < 30: pick_random_target(self, self.get_field_info().boost_pads) # Find the direction of our car using the Orientation class car_orientation = Orientation(my_car.physics.rotation) car_direction = car_orientation.forward steer_correction_radians = find_correction(car_direction, car_to_target) if steer_correction_radians > 1: turn = -1 elif steer_correction_radians < -1: turn = 1 else: turn = -1 * steer_correction_radians self.controller_state.throttle = 1.0 self.controller_state.steer = turn # path = predictBallPath(packet.game_ball) # draw_debug(self.renderer, my_car, packet.game_ball, "", self.target, path) return self.controller_state
def go_to_position(self, packet: GameTickPacket, ideal: Vec3): my_car = packet.game_cars[self.index] car_orientation = Orientation(my_car.physics.rotation) car_direction = car_orientation.forward steer_correction_radians = find_correction( car_direction, Vec3(ideal) - Vec3(my_car.physics.location)) # Turn left if steer correction is positive in radians turn_direction_multiplier = -1.0 if steer_correction_radians > 0 else 1.0 abs_correction = abs(steer_correction_radians) if abs_correction >= .5: turn = 1 * turn_direction_multiplier else: turn = abs_correction * 2 * turn_direction_multiplier # Powerslide if the angle of correction is more than 1 radian if abs_correction > 1.3: self.controller_state.handbrake = True else: self.controller_state.handbrake = False # TODO: change. always boost self.controller_state.boost = not self.controller_state.handbrake self.controller_state.throttle = 1.0 self.controller_state.steer = turn
def drive_toward(self, my_car, car_to_desired_location) -> str: self.controller_state.boost = False # Find the direction of our car using the Orientation class car_orientation = Orientation(my_car.physics.rotation) car_direction = car_orientation.forward car_yaw = my_car.physics.rotation.yaw steer_correction_radians = find_correction(car_direction, car_to_desired_location, car_yaw) if steer_correction_radians < math.radians(-10): # If the target is more than 10 degrees right from the centre, steer left turn = -1 action_display = "turn left" elif steer_correction_radians > math.radians(10): # If the target is more than 10 degrees left from the centre, steer right turn = 1 action_display = "turn right" else: # If the target is less than 10 degrees from the centre, steer straight turn = 0 action_display = "drive straight" self.controller_state.throttle = 1.0 self.controller_state.steer = turn return action_display
def action_goto(self, my_car, car_location, target_location): controller = SimpleControllerState() if target_location is None: return controller car_to_target = target_location - car_location # Find the direction of our car using the Orientation class car_orientation = Orientation(my_car.physics.rotation) car_direction = car_orientation.forward steer_correction_radians = find_correction(car_direction, car_to_target) 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. self.action_display = "turn left" else: turn = 1.0 self.action_display = "turn right" controller.throttle = 1.0 controller.steer = turn controller.boost = True return controller
def simpleballChase(self, packet): ball_location = Vec3(packet.game_ball.physics.location) ball_velocity = Vec3(packet.game_ball.physics.velocity) main_car = packet.game_cars[self.index] car_location = Vec3(main_car.physics.location) car_to_ball = car_location - ball_location # Find the direction of our car using the Orientation class car_orientation = Orientation(main_car.physics.rotation) car_direction = car_orientation.forward distFromBall = self.Distance3D(car_location, ball_location) mirrorBall = Vec3(ball_location.x, ball_location.y, 0) mirrorCar = Vec3(car_location.x, car_location.y, 0) leg = self.Distance3D(mirrorBall, ball_location) legCar = self.Distance3D(mirrorBall, mirrorCar) distFromGround = self.Distance3D(car_location, mirrorCar) hypotenuse = self.Distance3D(mirrorCar, ball_location) theta = math.asin(leg / hypotenuse) if(legCar < 400): self.boost = 0 self.throttle = self.map(legCar, 400, 0, 1, 0) else: self.turn = self.find_correction(car_direction, car_to_ball) self.drift = self.determineDrift(self.find_correction_normal(car_direction, car_to_ball)) self.boost = self.manageBoost(self.find_correction_normal(car_direction, car_to_ball)) self.throttle = 1
def __init__(self): """Creates a new GameObject with zeroed data.""" self.location = Vec3(0,0,0) self.velocity = Vec3(0,0,0) self.orientation = Orientation(0,0,0) self.rvelocity = Vec3(0,0,0) self.local_location = Vec3(0,0,0)
def make_obb(physics: SimPhysics): fixed_size = rlu_vec3(118.0 / 2, 84.2 / 2, 36.16 / 2) # octane orientation = Orientation(physics.rotation).to_rot_mat() box = obb() box.center = to_rlu_vec(physics.location) box.half_width = fixed_size box.orientation = orientation return box
def car_location_angle_flattened(car, loc: Vec3): car_orientation = Orientation(car.rotation) relative_target = relative_location(car.location, car_orientation, loc).flat() # car_orientation_flat = car_orientation.forward.flat() car_direction = Vec3(10, 0, 0) angle_cos = (relative_target.dot(car_direction)) / ( car_direction.length() * relative_target.length()) return math.acos( angle_cos) if relative_target.y < 0 else -math.acos(angle_cos)
def game_tick_packet_to_rl_state(self, game_tick): my_car = game_tick.game_cars[0] car_location = Vec3(my_car.physics.location) car_rotation = Orientation(my_car.physics.rotation) car_velocity = Vec3(my_car.physics.velocity) ball_location = Vec3(game_tick.game_ball.physics.location) ball_location_rel = relative_location(car_location, car_rotation, ball_location) goal_location = Vec3(0, GameValues.GOAL_CENTER_Y, GameValues.GOAL_CENTER_Z) car_location_rel = relative_location(goal_location, car_rotation, car_location) dist = car_location.dist(ball_location)
def align_car_to(controller: SimpleControllerState, angular_velocity: Vec3, rotation: Orientation, forward: Vec3, up: Vec3): local_forward = rotation.cast_local(forward) ang_vel_local = rotation.cast_local(angular_velocity) pitch_angle = math.atan2(-local_forward.z, local_forward.x) yaw_angle = math.atan2(-local_forward.y, local_forward.x) pitch_angular_velocity = ang_vel_local.y yaw_angular_velocity = ang_vel_local.z p = 4 d = 0.9 controller.pitch = clamp(-pitch_angle * p + pitch_angular_velocity * d, -1, 1) controller.yaw = clamp(-yaw_angle * p - yaw_angular_velocity * d, -1, 1)
def compare( human_physics_last: SimPhysics, controls_last: SimpleControllerState, human_physics_cur: SimPhysics, dt, ): # assume ground for now full_step(human_physics_last, controls_last, dt) # compare cur and last cur = human_physics_cur last = human_physics_last velo = cur.velocity - last.velocity loc = cur.location - last.location rot = Orientation(cur.rotation).up - Orientation(last.rotation).up print( f"Off by: v: {velo}:{velo.length():3f}, p: {loc}:{loc.length():3f}, rot: {rot}:{rot.length():3f}" )
def get_info(self, packet): self.location: location = Vec3( packet.game_cars[self.my_car].physics.location) self.velocity: velocity = Vec3( packet.game_cars[self.my_car].physics.velocity) self.rotation: rotation = packet.game_cars[ self.my_car].physics.rotation self.angular_V: angular_V = Vec3( packet.game_cars[self.my_car].physics.angular_velocity) self.orientation: orientation = Orientation(self.rotation)
def get_rpy_input_to_target(self, target_orientation: Orientation): co = self.orientation to = target_orientation w0 = self.ang_velocity w1 = co.rotation_diff_to(to) rpy = Orientation.get_rpy(w0, w1) angle = rpy.length() rpy = rpy.normalized() max_scalable = min(w1.length() / 5.5, 1 / max(abs(rpy.x), abs(rpy.y), abs(rpy.z))) return rpy * min(angle / np.pi, max_scalable)
def groundTouch(self, packet): timeChange = self.findBallGround()[1] - packet.game_info.seconds_elapsed fballLoc = self.findBallGround()[0] #original Code: ball_location = Vec3(packet.game_ball.physics.location) ball_velocity = Vec3(packet.game_ball.physics.velocity) main_car = packet.game_cars[self.index] car_velocity = Vec3(main_car.physics.velocity) car_location = Vec3(main_car.physics.location) ballHeight = ball_location.z carSpeed = 1410 carSpeedBoost = 2300 carMagnitude = self.findMagnitude(car_velocity.x, car_velocity.y) fcarToBall = self.Distance3D(car_location, fballLoc) if(ball_location.z < 94): requiredSpeed = 2300 else: requiredSpeed = fcarToBall/timeChange #end new mirrorBall = Vec3(ball_location.x, ball_location.y, 0) mirrorCar = Vec3(car_location.x, car_location.y, 0) car_to_ball = car_location - ball_location # Find the direction of our car using the Orientation class car_orientation = Orientation(main_car.physics.rotation) car_direction = car_orientation.forward if(self.index == 0 ): print(car_direction) distFromBall = self.Distance3D(car_location, ball_location) mirrorBall = Vec3(ball_location.x, ball_location.y, 0) mirrorCar = Vec3(car_location.x, car_location.y, 0) leg = self.Distance3D(mirrorBall, ball_location) legCar = self.Distance3D(mirrorBall, mirrorCar) distFromGround = self.Distance3D(car_location, mirrorCar) hypotenuse = self.Distance3D(mirrorCar, ball_location) theta = math.asin(leg / hypotenuse) self.turn = self.find_correction(car_direction, car_to_ball) self.drift = self.determineDrift(self.find_correction_normal(car_direction, car_to_ball)) if(self.manageBoost(self.find_correction_normal(car_direction, car_to_ball)) == 1): #Allowed to boost self.defaultThrottleDribble(requiredSpeed, carMagnitude) else: self.boost = 0 self.throttle = 1
def update(self, packet: GameTickPacket): packet_car = packet.game_cars[self.index] self.location = Vec3(packet_car.physics.location) self.velocity = Vec3(packet_car.physics.velocity) self.orientation = Orientation(packet_car.physics.rotation) self.angular_velocity = Vec3(packet_car.physics.angular_velocity) self.dead = packet_car.is_demolished self.flying = not packet_car.has_wheel_contact self.supersonic = packet_car.is_super_sonic self.jumped = packet_car.jumped self.double_jumped = packet_car.double_jumped self.boost = packet_car.boost
def tick(self, info : MyInfo) -> SimpleControllerState: self.new_touch(info) target = Vec3(self.info.ball_path[0].physics.location) offset = (Vec3(target) - self.info.their_goal.center).rescale(OFFSET_LEN) #print("in tick: target = {}".format(target)) relative = relative_location(Vec3(self.car.loc), Orientation(self.car.rot), target + offset) angle = atan2(relative.y, relative.x) other_ang = self.car.vel.ang_to(target) #print("relative: {}".format(relative)) #print("angle to ball: {}, other angle: {}".format(angle * 180 / pi, other_ang * 180 / pi)) self.controls.steer = self.steeringMagic(angle * 5) self.controls.throttle = 1 return self.controls
def _set_rlu_car(self): c = self.rlu_car c.position = to_rlu_vec(self.physics.location) c.velocity = to_rlu_vec(self.physics.velocity) c.angular_velocity = to_rlu_vec(self.physics.angular_velocity) c.orientation = Orientation(self.physics.rotation).to_rot_mat() c.boost = self.boost c.jumped = False c.double_jumped = False c.on_ground = True c.team = 0 c.time = 0 self.is_rlu_updated = True
def update(self, packet): self.location = Vec3(packet.game_cars[self.index].physics.location) self.velocity = Vec3(packet.game_cars[self.index].physics.velocity) self.rotation = packet.game_cars[self.index].physics.rotation self.angular_velocity = Vec3( packet.game_cars[self.index].physics.angular_velocity) self.orientation = Orientation(self.rotation) self.yaw_velocity = self.orientation.up.dot(self.angular_velocity) self.pitch_velocity = self.orientation.right.dot(self.angular_velocity) self.roll_velocity = self.orientation.forward.dot( self.angular_velocity) self.grounded = packet.game_cars[self.index].has_wheel_contact self.supersonic = packet.game_cars[self.index].is_super_sonic self.team = packet.game_cars[self.index].team self.vec_to_ball = Vec3( packet.game_ball.physics.location) - self.location self.boost = packet.game_cars[self.index].boost #deal with instability of velocity-facing deficiet or jumping if self.velocity.length() != 0: if self.velocity.ang_to( self.orientation.forward) > 0.2 or not self.grounded: self.last_unstable = packet.game_info.seconds_elapsed self.stable = False elif packet.game_info.seconds_elapsed - self.last_unstable > 0.2: self.stable = True #logic for on_wall self.on_wall = self.grounded and self.orientation.up.z < 0.1 #logic for guessing what they are doing, out of BALL, ROTATE, SUPPORT self.assumed_maneuver = None if -2 < self.velocity.flat().signed_ang_to( self.vec_to_ball ) < -0.3 and self.yaw_velocity < 0 or 0.3 < self.velocity.flat( ).signed_ang_to( self.vec_to_ball ) < 2 and self.yaw_velocity > 0 or -0.3 < self.velocity.flat( ).signed_ang_to(self.vec_to_ball) < 0.3: self.assumed_maneuver = "BALL" if self.location.y > packet.game_ball.physics.location.y + 100 and self.team == 0 or self.location.y < packet.game_ball.physics.location.y - 100 and self.team == 1: self.assumed_maneuver = "OFFSIDE" if self.velocity.length() != 0: if self.velocity.normalized( ).y < -0.8 and self.team == 0 or self.velocity.normalized( ).y > 0.8 and self.team == 1: self.assumed_maneuver = "ROTATE" if self.assumed_maneuver is None: self.assumed_maneuver = "SUPPORT"
def tick(self, info: MyInfo): self.new_touch(info) if self.can_challenge(): self.done = True #print("last touch: {}".format(self.last_touch)) ball = self.info.ball_path[0].physics target = Vec3(self.info.my_goal.center) - ball.location relative = relative_location(Vec3(self.car.loc), Orientation(self.car.rot), Vec3(ball.location)) angle = atan2(relative.y, relative.x) self.controls.steer = self.steeringMagic(angle) boost, throttle = self.match_speed(self.car, ball) self.controls.boost = boost self.controls.throttle = throttle return self.controls
def __init__(self, packet: GameTickPacket, index: int): self.index = index this_car = packet.game_cars[self.index] self.jump_time = -1 self.is_bot = this_car.is_bot self.name = this_car.name self.team = this_car.team self.team_string = "blue" if self.team == 0 else "red" self.hitbox: RectHitbox = RectHitbox(this_car.hitbox, offset=Vec3( this_car.hitbox_offset)) self.score_info = this_car.score_info # score, goals, own_goals, assists, saves, shots, demolitions self.double_jumped = this_car.double_jumped self.orientation = Orientation(this_car.physics.rotation) self.update(packet)
def _rlu_step(self, controls, dt, on_ground): # we think RLU sims this situation better # print(f"{self.count}: rlusim") if not self.is_rlu_updated: self._set_rlu_car() self.rlu_car.on_ground = on_ground inputs = self._make_input(controls) self.rlu_car.step(inputs, dt) self.physics.location = rlu_to_Vec3(self.rlu_car.position) self.physics.velocity = rlu_to_Vec3(self.rlu_car.velocity) self.physics.angular_velocity = rlu_to_Vec3( self.rlu_car.angular_velocity) self.physics.rotation = Orientation.from_rot_mat( self.rlu_car.orientation)
def get_output(self, packet: GameTickPacket) -> SimpleControllerState: self.delta_time = packet.game_info.seconds_elapsed-self.time self.time = packet.game_info.seconds_elapsed self.ball_predictions = self.get_ball_prediction_struct() predicted_goal = find_future_goal(self.ball_predictions) goal_text = "No Goal Threats" if predicted_goal: goal_text = f"Goal in {'%.4s' % str(predicted_goal[1]-packet.game_info.seconds_elapsed)}s" ball_location = Vec3(packet.game_ball.physics.location) my_car = packet.game_cars[self.index] car_location = Vec3(my_car.physics.location) car_to_ball = ball_location - car_location # Find the direction of our car using the Orientation class car_orientation = Orientation(my_car.physics.rotation) car_direction = car_orientation.forward steer_correction_radians = find_correction(car_direction, car_to_ball) 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. action_display = "turn left" else: turn = 1.0 action_display = "turn right" self.controller_state.throttle = 1.0 self.controller_state.steer = turn draw_debug(self.renderer, my_car, packet.game_ball, action_display,goal_text) if self.current_action_chain != None: if not self.current_action_chain.complete: self.controller_state = self.current_action_chain.update(self.delta_time) else: self.current_action_chain = None else: if int(self.time) % 5 == 0: self.current_action_chain = simple_front_flip_chain() return self.controller_state
def update(self, packet: GameTickPacket): my_car = packet.game_cars[self.car_id] self.location = Vec3(my_car.physics.location) self.orientation = Orientation(my_car.physics.rotation) self.velocity = Vec3(my_car.physics.velocity) self.angular_velocity = Vec3(my_car.physics.angular_velocity) self.is_demolished = my_car.is_demolished self.has_wheel_contact = my_car.has_wheel_contact self.is_super_sonic = my_car.is_super_sonic self.is_bot = my_car.is_bot self.jumped = my_car.jumped self.double_jumped = my_car.double_jumped self.name = my_car.name self.team = my_car.team self.boost = my_car.boost self.hitbox = my_car.hitbox self.hitbox_offset = Vec3(my_car.hitbox_offset)