pygame弹性碰撞和重力模拟错误

问题描述

我做了一个模拟来计算行星之间的弹性碰撞和重力。
一开始还好,但随着我们继续下去,对称性在某些时候会崩溃。 Here is the Video(1:40 休息)
我尝试了很多代码,但对称性最终被打破。

这是弹性碰撞代码

@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

这里是重力代码

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

完整代码here
为什么会发生以及如何解决
我想可能是浮动的精度错误,这只是我的猜测..
谢谢!
(对不起,我的英语不好,我用了一点翻译..)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)