DAMPENING_FACTOR = 0.88 def hits(self, other): if (self == other): # I can't collide with myself. return False else: # Determine if I overlap with the target particle. return (self._radius + other.get_radius()) >= distance(self._x, self._y, other.get_x(), other.get_y()) def bounce(self, target): ''' This method modifies this Particle object's velocities based on its collision with the given target particle. It modifies both the magnitude and the direction of the velocities based on the interacting magnitude and direction of particles. It only changes the velocities of this object; an additional call to bounce() on the other particle is required to implement a complete bounce interaction. The collision algorithm is based on a similar algorithm published by K. Terzidis, Algorithms for Visual Design. target the other particle ''' if self.hits(target): angle = math.atan2(target.get_y() - self._y, target.get_x() - self._x) targetX = self._x + math.cos(angle) * (self._radius + target.get_radius()) targetY = self._y + math.sin(angle) * (self._radius + target.get_radius()) ax = targetX - target.get_x() ay = targetY - target.get_y() self._velX = (self._velX - ax) * DAMPENING_FACTOR self._velY = (self._velY - ay) * DAMPENING_FACTOR