def backsolve(target, car, time, gravity=650): #Finds the acceleration required for a car to reach a target in a specific amount of time d = target - car.location dvx = ((d[0] / time) - car.velocity[0]) / time dvy = ((d[1] / time) - car.velocity[1]) / time dvz = (((d[2] / time) - car.velocity[2]) / time) + (gravity * time) return Vector3(dvx, dvy, dvz)
def in_field(point, radius): #determines if a point is inside the standard soccer field point = Vector3(abs(point[0]), abs(point[1]), abs(point[2])) if point[0] > 4080 - radius: return False elif point[1] > 5900 - radius: return False elif point[0] > 880 - radius and point[1] > 5105 - radius: return False elif point[0] > 2650 and point[1] > -point[0] + 8025 - radius: return False return True
def shot_valid(agent, shot, threshold=45): #Returns True if the ball is still where the shot anticipates it to be #First finds the two closest slices in the ball prediction to shot's intercept_time #threshold controls the tolerance we allow the ball to be off by slices = agent.get_ball_prediction_struct().slices soonest = 0 latest = len(slices) - 1 while len(slices[soonest:latest + 1]) > 2: midpoint = (soonest + latest) // 2 if slices[midpoint].game_seconds > shot.intercept_time: latest = midpoint else: soonest = midpoint #preparing to interpolate between the selected slices dt = slices[latest].game_seconds - slices[soonest].game_seconds time_from_soonest = shot.intercept_time - slices[soonest].game_seconds slopes = (Vector3(slices[latest].physics.location) - Vector3(slices[soonest].physics.location)) * (1 / dt) #Determining exactly where the ball will be at the given shot's intercept_time predicted_ball_location = Vector3( slices[soonest].physics.location) + (slopes * time_from_soonest) #Comparing predicted location with where the shot expects the ball to be return (shot.ball_location - predicted_ball_location).magnitude() < threshold
def defaultPD(agent, local_target, direction=1.0): # points the car towards a given local target. # Direction can be changed to allow the car to steer towards a target while driving backwards local_target *= direction up = agent.me.local(Vector3(0, 0, 1)) # where "up" is in local coordinates target_angles = [ math.atan2(local_target[2], local_target[0]), # angle required to pitch towards target math.atan2(local_target[1], local_target[0]), # angle required to yaw towards target math.atan2(up[1], up[2]) ] # angle required to roll upright # Once we have the angles we need to rotate, we feed them into PD loops to determing the controller inputs agent.controller.steer = steerPD(target_angles[1], 0) * direction agent.controller.pitch = steerPD(target_angles[0], agent.me.angular_velocity[1] / 4) agent.controller.yaw = steerPD(target_angles[1], -agent.me.angular_velocity[2] / 4) agent.controller.roll = steerPD(target_angles[2], agent.me.angular_velocity[0] / 2) # Returns the angles, which can be useful for other purposes return target_angles