I working on a 2D planetary orbit simulator and I've encountered a problem regarding the collision of planets. Currently, here is what I have
if self._dist_between(p1.pos, p2.pos) <= p1.radius + p2.radius: pass
I detect the collisions but I don't know how to resolve them. I don't want the planets to bounce or do anything fancy, I simply want them to not pass through one another. I have tried disabling their movement completely on collision but this fails to work when more than 2 planets are active in the simulation, as when 2 planets collide they freeze in time rather than continue to be affected by the other planets.
This is the code that manages the movement
def update(self): for p1 in self.planets: for p2 in self.planets: if p1 != p2: # Calculate and apply force temp_force_vector = [ self._gravitational_force(p1.mass, p2.mass, self._dist_between(p1.pos, p2.pos)), self._radians_between(p1.pos, p2.pos) ] if self._dist_between(p1.pos, p2.pos) > p1.radius + p2.radius: p1.apply_force(temp_force_vector) p1.update_pos()