I made a simulation that calculates the elastic collision and gravity between planets.
It was fine at first, but as we go on, the symmetry breaks down at some point. Here is the Video (break at 1:40)
I tried with a lot of codes, but the symmetry eventually breaked.
Here is elastic collision code:
@staticmethod
def reflect_colliding_circles(a, b):
delta = vector(a.rect.center[0], a.rect.center[1]) - vector(b.rect.center[0], b.rect.center[1])
d = delta.length()
mtd = delta * ((a.radius+b.radius)-d)/d
im1 = 1 / a.m
im2 = 1 / b.m
a.pos += mtd * (im1 / (im1 + im2))
b.pos -= mtd * (im2 / (im1 + im2))
v = a.vel - b.vel
vn = v.dot(mtd.normalize())
if vn > 0.0:
return
i = (-1 * (1.0 + E) * vn) / (im1 + im2)
impulse = mtd.normalize() * i
a.vel += impulse * im1
b.vel -= impulse * im2
And Here is gravity code:
def calc_gravity(self, otpl):
force = self.pos - otpl.pos
if force == vector(0, 0):
return vector(0, 0)
distance = force.magnitude()
force = force.normalize()
strength = -(G * self.m * otpl.m) / (distance ** 2)
force = force * strength
return force
The Full code is here
Why it happen and how to fix it?
I think maybe it is float's accuracy error, It's just my guess..
Thanks!
(Sorry for my poor English, I used translator a little bit..)