我有一个粒子数组,它们是类对象,我试图编程任何粒子碰撞时的弹性碰撞。 我只使用了两个硬编码,但使用for循环似乎不起作用:
for _p1 in _particles:
for _p2 in _particles:
if _p1 != _p2:
_p1_x, _p1_y = _p1.get_pos()
_p2_x, _p2_y = _p2.get_pos()
_distance = ((_p2_x - _p1_x) ** 2 + (_p2_y - _p1_y) ** 2) ** 0.5
if _distance <= abs(_p1.get_radius() + _p2.get_radius()):
_u1x, _u1y = _p1.get_velocity()
_u2x, _u2y = _p2.get_velocity()
_m1 = _p1.get_mass()
_m2 = _p2.get_mass()
# Calculate the final velocities
_v1x = _u1x * (_m1 - _m2) / (_m1 + _m2) + 2 * _u2x * _m2 / (
_m1 + _m2)
_v2x = 2 * _u1x * _m1 / (_m1 + _m2) - _u2x * (_m1 - _m2) / (_m1 + _m2)
# Apply the new velocity in the x plane
_p1.set_velocity(_v1x, _u1y)
_p2.set_velocity(_v2x, _u2y)
粒子只是相互穿过。我的理论是,在下一个循环中切换的粒子抵消了净速度的变化,因此它们的路径保持不变。 有什么建议可以解决这个问题吗
尝试仅计算每个粒子组合一次:
相关问题 更多 >
编程相关推荐