我正在尝试实现this video中引入的著名的弹性碰撞问题。
我正在使用VPython模拟整个过程,但是由于某些原因,我的代码无法正常运行。好像发生第三次碰撞时,这些块会无限期地“碰撞”(冲突计数器不断增加),然后将这些块粘在一起。他们甚至是穿过墙而不是在墙上弹跳,我不知道为什么。
这是我的代码:
from vpython import *
import numpy as np
import time
def compute_pi(number_of_digits):
scene = canvas(width=1280, height=720, range=3.8, center=vec(6, 0, 0))
scene.title = 'Computing PI with collisions'
# blocks
small_block = box(
pos=vec(2.0, -2.5, 0.0),
size=vec(1.0, 1.0, 0.01),
color=vec(0.5, 1.0, 0.3)
)
large_block = box(
pos=vec(4.0, -2.0, 0.0),
size=vec(2.0, 2.0, 0.01),
color=vec(1.0, 0.5, 0.3)
)
# wall & ground
wall = box(
pos=vec(0.0, 0.0, 0.0),
size=vec(0.1, 6, 0.01),
color=vec(1.0, 1.0, 1.0)
)
ground = box(
pos=vec(9.95, -3.0, 0.0),
size=vec(20, 0.1, 0.01),
color=vec(1.0, 1.0, 1.0)
)
# initial conditions
small_block.m = 1.0
large_block.m = 100 ** (number_of_digits - 1)
small_block.vel = vec(0.0, 0.0, 0.0)
large_block.vel = vec(-0.01, 0.0, 0.0)
t = 0
dt = 0.05
collisions_counter = 0
time.sleep(0.5)
while True:
rate(1000)
large_block.pos += large_block.vel * dt
small_block.pos += small_block.vel * dt
# wall collision
if (small_block.pos.x - small_block.size.x / 2 < wall.pos.x + wall.size.x / 2):
small_block.vel *= -1
collisions_counter += 1
print ("Number of collisions: " + str(collisions_counter))
# blocks collision
if (small_block.pos.x + small_block.size.x / 2 > large_block.pos.x - large_block.size.x / 2):
small_block.vel = ((small_block.m - large_block.m) / (small_block.m + large_block.m)) * small_block.vel + ((2 * large_block.m) / (small_block.m + large_block.m)) * large_block.vel
large_block.vel = ((2 * small_block.m) / (small_block.m + large_block.m)) * small_block.vel + ((large_block.m - small_block.m) / (small_block.m + large_block.m)) * large_block.vel
collisions_counter += 1
print ("Number of collisions: " + str(collisions_counter))
t += dt
答案 0 :(得分:0)
我无法在这里学习所有物理学,但我认为您可能已经成为以下问题的受害者:当发生碰撞而使速度的x分量反转时,设置vx = -vx似乎很自然。不幸的是,如果最近的移动足够深地越过边界,则在下一个时间间隔内您仍然足够越过边界以再次翻转电荷的信号时,您可能会产生振荡性的原纤化。明确的测试是更安全的:如果您发现位置在边界的右侧,则显式设置vx = -abs(vx),如果在边界的左侧,则显式设置vx = abs(vx)。