def calc_receive_ball_moving(self): #make sure that you're only doing this if for i in range(len(self.keeperArray)): if self.keeperArray[i].inPosession == True: rDecision = (i, self.keeperArray[i].true_pos) return for i in range(len(self.takerArray)): if self.takerArray[i].inPosession == True: return TA = kUtil.addVectorToPoint(self.fieldBall.trueBallPos, self.fieldBall.trueBallDirection) TB = self.fieldBall.trueBallPos minTime = 99999.0 argmin = None bestPerpIntersect = None for i in range(len(self.keeperArray)): TC = self.keeperArray[i].true_pos if (kUtil.cosTheta(TA, TB, TC)) < 0: #print("Keeper " , i, " can't get to ball: the cosTheta is negetive.") #it's impossible for this keeper to get the ball continue else: pd = kUtil.getPerpDist(TA, TB, TC) pt = pd/self.maxPlayerSpeed normalVector = kUtil.getNormalVector(TA, TB, TC) perpIntersect = kUtil.addVectorToPoint(TC, normalVector) bd = kUtil.getDist(TB, perpIntersect) bt = bd/self.maxBallSpeed if pt > bt: #keeper wont' be able be able to get to ball in time #print("player ", i+1, "can't reach ball as pt:",pt," and bt: ",bt) continue else: #keeper CAN get to ball. can it get there soonest though? #save the fastest keeper if (pt < minTime): minTime = pt argmin = i bestPerpIntersect = perpIntersect #at this point, if a keeper can get to the ball, the fastest and it's intercept are saved if (argmin != None): rDecision = [argmin, self.calcOptimal(self.keeperArray, argmin, bestPerpIntersect)] for i in range(len(self.keeperArray)): self.keeperArray[i].receiveDecision(rDecision) for i in range(len(self.takerArray)): self.takerArray[i].receiveDecision(rDecision) else: print("no argmin found. game about to crash for sure")
def __calc_receive_ball_moving(worldRef, inputDirection, possessingKeeperIndex): """ This function is a private function meant to assist calc_receive. This function will go and calculate the receive decision for the special case where the ball is moving. The receive decision is a tuple that simply contains the index of the keeper that should run towards the ball, and the coordinate that the keeper should run to. If the ball is moving, then calc_receieve will find an intersection point along the balls projected path that the selected keeper can run to. The intercept point is selected such that the selected keeper will run a short distance, be far away from the takers, and also be far away from out of bounds. .. note:: This is a private function that the user shouldn't worry about calling. Only the calc_receieve function of this method will use this function. And only the simulator class should call the calc_receive function. :param worldRef: a reference to the simulator class which is calling this function :param inputDirection: the current direction the ball is moving. :param possessingKeeperIndex: the index of the keeper who currently has possession :type worldRef: keepAway :type inputDirection: tuple of floats :type possessingKeeperIndex: integer :returns: tuple, where first element is the index of the keeper picked to run towards the ball. The simulator will use this index to look up the index of the keeper in its self.keeperArray. The 2nd element is the intersection coordinate :rtype: tuple where first element is integer, second element is tuple. 2nd element tuple contains integers """ #TA is a point that the ball is heading to in the next time step TA = kUtil.addVectorToPoint(worldRef.fieldBall.trueBallPos, inputDirection) #TB is the current ball position, and for angle calculations, it will be the vertex TB = worldRef.fieldBall.trueBallPos minTime = float("inf") argmin = None bestPerpIntersect = None #the purpose of this for loop is to find which keeper should go to the ball. for i in range(len(worldRef.keeperArray)): #TC is the position of the keeper who's figuring out if he should goToBall(), or getOpen() TC = worldRef.keeperTruePosArray[i] if (kUtil.cosTheta(TA, TB, TC)) < 0: #print "Keeper " , i, " can't get to ball: the cosTheta is negetive." #it's impossible for this keeper to get the ball continue else: pd = kUtil.getPerpDist(TA, TB, TC) pt = pd/worldRef.maxPlayerSpeed normalVector = kUtil.getNormalVector(TA, TB, TC) perpIntersect = kUtil.addVectorToPoint(TC, normalVector) bd = kUtil.getDist(TB, perpIntersect) bt = bd/worldRef.maxBallSpeed if pt > bt: #keeper wont' be able be able to get to ball in time #print "player ", i+1, "can't reach ball as pt:",pt," and bt: ",bt continue else: #keeper CAN get to ball. can it get there soonest though? #save the fastest keeper if (pt < minTime and i != possessingKeeperIndex): minTime = pt argmin = i bestPerpIntersect = perpIntersect #at this point, if a keeper can get to the ball, #the fastest and it's intercept are saved if (argmin != None): rDecision = [argmin, __calcOptimal(worldRef, worldRef.keeperArray, argmin, bestPerpIntersect)] return rDecision else: rDecision = [1 , worldRef.get_field_center()] #print("no argmin found. game about to end for sure.") return rDecision