def _bounce_one(a1, a2): # Bounces a1 dir_angle = vectors.angle(a2.pos, a1.pos) vel_angle = vectors.angle(a1.velocity) # If they are head on then we want to swivel them a little if vectors.bound_angle(dir_angle[0] + 180) == vel_angle[0]: dir_angle[0] = vectors.bound_angle(dir_angle[0] + 40) # Keep trying distances further and further apart until they're # not going to be overlapping any more overlapping = True dist = vectors.total_velocity(a1.velocity) a2_rect = (a2.pos[0], a2.pos[1], a2.size[0], a2.size[1]) while overlapping: new_pos = vectors.add_vectors(a1.pos, vectors.move_to_vector(dir_angle, dist)) new_rect = (new_pos[0], new_pos[1], a1.size[0], a1.size[1]) if not geometry.rect_collision(new_rect, a2_rect, True): overlapping = False dist += 1 # Add a bit to be safe new_pos = vectors.add_vectors( a1.pos, vectors.move_to_vector(dir_angle, dist + vectors.total_velocity(a1.velocity))) a1.pos = new_pos
def _bounce_one(a1, a2): # Bounces a1 dir_angle = vectors.angle(a2.pos, a1.pos) vel_angle = vectors.angle(a1.velocity) # If they are head on then we want to swivel them a little if vectors.bound_angle(dir_angle[0]+180) == vel_angle[0]: dir_angle[0] = vectors.bound_angle(dir_angle[0] + 40) # Keep trying distances further and further apart until they're # not going to be overlapping any more overlapping = True dist = vectors.total_velocity(a1.velocity) a2_rect = (a2.pos[0], a2.pos[1], a2.size[0], a2.size[1]) while overlapping: new_pos = vectors.add_vectors(a1.pos, vectors.move_to_vector( dir_angle, dist )) new_rect = (new_pos[0], new_pos[1], a1.size[0], a1.size[1]) if not geometry.rect_collision(new_rect, a2_rect, True): overlapping = False dist += 1 # Add a bit to be safe new_pos = vectors.add_vectors(a1.pos, vectors.move_to_vector( dir_angle, dist + vectors.total_velocity(a1.velocity) )) a1.pos = new_pos
def _move_ai(self, target): # We can drift to the side (like a spaceship) if self.drifts: # First we turn if self._turn_ai(target): # And if facing the right way we accelerate self._accelerate_ai(target) else: if vectors.total_velocity(self.velocity) > 0: self._decelerate_ai() else: # We cannot drift (like a tank) origional_facing = list(self.facing) if self._turn_ai(target): # We are facing the right way, lets accelerate self._accelerate_ai(target) else: # Wrong way, if we are moving we need to stop before turning if self.velocity.magnitude() > 0: self.facing = origional_facing self._decelerate_ai()