def look(self, target): """ Update the sprite for the being to always face the cursor""" angle = geometry.angle_between(self.rect, target) if angle >= 45 and angle <= 135: self.direction = NORTH elif angle > 135 or angle < -135: self.direction = WEST elif angle >= -135 and angle <= -45: self.direction = SOUTH elif angle > -45 and angle < 45: self.direction = EAST
def _parallelish_pairs(lines): assert len(lines) == 4 pair1 = None closest_angle = float('inf') for line1, line2 in combinations(lines, 2): angle = geometry.angle_between(line1, line2) if angle < closest_angle: pair1 = {line1, line2} closest_angle = angle pair2 = set(lines) - pair1 return list(pair1), list(pair2)
def get_angle(self, polar_point): cartesian_sun, cartesian_zenith, cartesian_observed = [*map(to_cartesian, [self.sun, self.zenith, polar_point])] # We get the vector perpendicular to the scattering plane formed by the observer, the zenith and the sun angle_vector = self.get_angle_vector(polar_point) # Get the normal of a vertical plane that goes through the zenith and the observed point vertical_plane_normal = np.cross(cartesian_zenith, cartesian_observed) # Calculate the angle from the observer's perspective by taking the angle between the vertical plane normal (acting as X axis) and the angle vector angle = angle_between(vertical_plane_normal, angle_vector) % (np.pi) # Take the reminder of division by pi as in reality we cannot distinguish whether angle is alpha or alpha+pi #TODO understand why this is necessary if self.is_left_of_the_sun_antisun_split(polar_point): angle = np.pi - angle return angle
def rotate_towards(self, position, slowmo_factor=1.0): """Rotate the ball to face towards a specific position. Does not rotate instantly. """ angle = geometry.angle_between(self.position, position) delta_angle = angle - self.angle if delta_angle > math.pi: delta_angle = -(math.pi * 2 - delta_angle) elif delta_angle < -math.pi: delta_angle = -(-math.pi * 2 - delta_angle) self.angle += delta_angle / slowmo_factor while self.angle < -math.pi: self.angle += math.pi * 2 while self.angle > math.pi: self.angle -= math.pi * 2
def point_towards_end(self, level): end_point = levels.middle_pixel(level.end_tile) self.angle = geometry.angle_between(self.position, end_point)
def launch_towards(self, position, power=12.0): angle = geometry.angle_between(self.position, position) self.launch(angle, power)
def get_gamma(self, polar_point): """ Angular distance between the observed pointing and the sun. Scattering angle. """ cartesian_sun, cartesian_observed = [*map(to_cartesian, [self.sun, polar_point])] return angle_between(cartesian_sun, cartesian_observed)