def predict_puck_movement(self, bag): #ausgefuehrt """ Predicts the pucks movement. :param bag: The parameter bag. :return: """ # need at least 2 points if len(self.buffer) < 2: return # loop over points direction_sum = (0, 0) for i in xrange(0, len(self.buffer) - 1): # get tuples and time diff current_tuple = self.buffer[i] next_tuple = self.buffer[i+1] time_diff = next_tuple[2] - current_tuple[2] # get direction (length is velocity) and divide by time_diff direction = vector.mul_by(vector.from_to(current_tuple, next_tuple), 1.0 / time_diff) # sum up direction_sum = vector.add(direction_sum, direction) # averaging direction = vector.mul_by(direction_sum, 1.0 / self.buffer_size) # add puck direction (normalized) and velocity bag.puck.velocity = vector.length(direction) bag.puck.direction = vector.normalize(direction) bag.next.puck.olddirection = bag.puck.direction
def move_stick(self, bag): """ Moves the stick. :param bag: The parameter bag. :return: """ if 'position' not in bag.stick: bag.next.stick.position = strategy.SimpleStrategy.HOME_POSITION return if 'dest_position' not in bag.stick: position = bag.stick.position else: # move stick towards destination # ignore dest_direction and dest_velocity dist_moved = strategy.SimpleStrategy.STICK_MAX_SPEED * bag.time_diff direction = vector.from_to(bag.stick.position, bag.stick.dest_position) if dist_moved > vector.length(direction): position = bag.stick.dest_position else: direction = vector.mul_by(vector.normalize(direction), dist_moved * strategy.SimpleStrategy.STICK_MAX_SPEED) position = vector.add(bag.stick.position, direction) # set new position bag.next.stick.position = position
def evaluate(self, goal, goal2): dir = vector.normalize(vector.from_to(self.position, goal)) stop = vector.sub(self.velocity, vector.mult(vector.along(self.velocity, dir), 0.25)) if vector.sqr_distance(self.position, goal) < 0.2: return vector.normalize( vector.sub(vector.sub(dir, stop), self.velocity)) return vector.normalize(vector.sub(dir, stop))
def MoveBetweenPuckAndGoal(self, bag): """ calculates the point between Goal an puck with x Koordinates 0.1 :param bag: The parameter bag. :return: point between Goal an puck with x Koordinates 0.1 """ positionGoal = [0, 0.5] directionGoalToPuck = vector.from_to(bag.puck.position, positionGoal) return self.CrossXLine(bag, 0.1, positionGoal, directionGoalToPuck)
def _check_for_wild_shot(self, bag): """ Checks if the latest puck position is a wild shot. Requires bag.puck.predicted_position. :param bag: The parameter bag :return: """ #TODO ueberlegen ob nutzen oder loeschen predicted_position_pixel = vector.coord_to_image_space(bag.puck.predicted_position, bag.table_boundaries) diff = vector.length(vector.from_to(bag.puck.position_pixel, predicted_position_pixel)) # use time_diff and velocity to play with this value #accepted = self.ACCEPTABLE_PUCK_POSITION_DIFF * bag.time_diff * max((1000 * self.last_path[-1][2]) if len(self.last_path) > 0 else 1.0, 1.0) #return diff > accepted return False
def go_home(self, bag): """ Starts to move the stick to its home position. :param bag: The parameter bag. :return: """ # can only go home if we actually have a stick if 'position' in bag.stick: dist_home = vector.length(vector.from_to(bag.stick.position, self.HOME_POSITION)) bag.stick.dest_position = self.HOME_POSITION bag.stick.dest_direction = (0, 0) bag.stick.dest_velocity = 0 bag.stick.dest_time = time.time() + dist_home / self.STICK_MAX_SPEED
def _calculate_collision_point(self, bag): """ Calculates the collision point between the puck and the stick. :param bag: The parameter bag. :return: """ # NOTE: First attempt, ignoring stick and puck acceleration, puck radius and stick radius here # OK, math first: # # S = stick position # C = point of collision # v = max speed of the stick # P = puck position # p = puck direction * velocity # # Points which the stick can reach in time t build a circle around him: # 1. (Cx - Sx)^2 + (Cy - Sy)^2 = r = v * t # # Points which the puck reaches in time t are on the line: # 2. Px + px * t = Cx # 3. Py + py * t = Cy # # Approach: # - replace Cx and Cy in 1. with 2. and 3. # - bracket the square terms (Px + px * t - Sx)^2 and (Py + py * t - Sy)^2 # - outline t^2 and t to get a formula like t^2*(...) + t*(...) + (...) # - name (...) a, b and c # - use the quadratic formula to get t # - use puck movement and t to get collision point # - use stick position and collision point # - gg Sx = bag.stick.position[0] Sy = bag.stick.position[1] Px = bag.puck.position[0] Py = bag.puck.position[1] px = bag.puck.direction[0] * bag.puck.velocity py = bag.puck.direction[1] * bag.puck.velocity v = self.STICK_MAX_SPEED # calculate a, b and c a = px*px + py*py - v*v b = 2*Px*px - 2*Sx*px + 2*Py*py - 2*Sy*py c = Px*Px - 2*Px*Sx + Sx*Sx + Py*Py - 2*Py*Sy + Sy*Sy # calculate t inner_sqrt = b*b - 4*a*c if inner_sqrt < 0: print("Going home, inner sqrt: " + str(inner_sqrt)) # no chance to get that thing, go home self.go_home(bag) return # use + first, since a is negative (Vpuck - Vstick) # (... / 2 * a) -> shortest time needed sqrt_b2_4ac = math.sqrt(inner_sqrt) t = (-b + sqrt_b2_4ac) / 2*a print("Vp: "+str(bag.puck.velocity)) print("t: "+str(t)) if t < 0: # too late for that chance, use other result t = (-b - sqrt_b2_4ac) / 2*a print("t2: "+str(t)) # get collision point C = vector.add((Px, Py), vector.mul_by((px, py), t)) s = vector.from_to((Sx, Sy), C) # save bag.stick.dest_position = C bag.stick.dest_direction = vector.normalize(s) bag.stick.dest_velocity = self.STICK_MAX_SPEED bag.stick.dest_time = time.time() + t
def evaluate(self, goal, goal2): dir = vector.normalize(vector.from_to(self.position, goal)) stop = vector.normalize(self.velocity) return vector.normalize(vector.sub(dir, stop))
def evaluate(self, goal, goal2): dir = vector.normalize(vector.from_to(self.position, goal)) stop = vector.normalize(self.velocity) stop = vector.mult(stop, vector.dot(stop, dir) * self.scale) return vector.normalize(vector.sub(dir, stop))
def evaluate(self, goal, goal2): dir = vector.normalize(vector.from_to(self.position, goal)) stop = vector.mult( vector.sub(self.velocity, vector.along(self.velocity, dir)), vector.length(self.velocity) * 2) return vector.normalize(vector.sub(dir, stop))