提问人:Amir G 提问时间:6/1/2023 更新时间:6/2/2023 访问量:58
我正在使用 vpython 构建这个双弹跳球模拟,但遇到了一个奇怪的橡皮筋问题
I'm building this double bouncing ball simulation with vpython and am running into a weird rubber banding issue
问:
我试图让一对球在 1D 空间中弹跳并让它们结肠杆菌。我在使用 webVPython 的 glowscript 上运行它,据我所知,应该与 VPython 相同。
我一直遇到这个问题,球往往会用橡皮筋引起一些奇怪的碰撞问题,它们可以慢慢地穿过其他物体,而且我太愚蠢了,无法弄清楚如何计算动量变化,不确定这是否与橡皮筋有关,但如果有人知道我可以使用的方程或将要应用的修复。
代码链接:https://www.glowscript.org/#/user/amirdmgazzali/folder/bouncingballs/program/bouncingballs1 这是代码:
' Web VPython 3.2
#these are the graphs
###############################################################################################
g1 = graph(xtitle="t [s]",ytitle="y [m]", width=500, height=150)
fp = gcurve(color=color.green)
g2 = graph(xtitle="t [s]",ytitle="v [m/s]", width=500, height=150)
fv = gcurve(color=color.red)
g3 = graph(xtitle="t [s]",ytitle="E [J]", width=500, height=150)
fK = gcurve(color=color.blue)
fU = gcurve(color=color.red)
fE = gcurve(color=color.magenta)
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
g4 = graph(xtitle="t [s]",ytitle="y [m]", width=500, height=150)
fyA = gcurve(color=color.green)
fyB = gcurve(color=color.orange)
#g5 = graph(xtitle="t [s]",ytitle="y [m]", width=500, height=150)
#fyB = gcurve(color=color.red)
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
###############################################################################################
#defining a floor to bounce on
#//////////////////////////////////////////////////////////////////////////////////////////
R = 0.02
floor = box(pos = vector(0,-0.005-R,0),size=vector(0.1,0.01,0.1))
#//////////////////////////////////////////////////////////////////////////////////////////
#some basic perameters/initial conditions
###############################################################################################
h = .3
#g = vector(0,-9.8,0)
g = 9.8
c = 0.5
t = 0
dt = 0.01
bt = 0
#ball definitions
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
mA = 2
x0A=0
y0A=h
vx0A=0
vy0A=0
dtA = 0.01
btA = 0
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#ball2 definitions
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
mB = .05
x0B=0
y0B=h+.1
vx0B=0
vy0B=0
dtB = 0.01
btB = 0
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
###############################################################################################
#defining a ball
#//////////////////////////////////////////////////////////////////////////////////////////
ball = sphere(pos=vector(0,h,0), radius=R, color=color.green, make_trail=True, trail_type="points",
interval=10, retain=10)
ball2 = sphere(pos=vector(0,h,0), radius=R, color=color.orange, make_trail=True, trail_type="points",
interval=10, retain=10)
#//////////////////////////////////////////////////////////////////////////////////////////
#physics behind the ball
###############################################################################################
#t<x; x defines how many seconds
while t<2:
#rate(x); x defines update rate
rate(10)
#just the basic physics equations for the ball
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
xA = x0A + vx0A*btA
yA = y0A + vy0A*btA - .5*g*btA**2
vxA = vx0A
vyA = vy0A - g*btA
vA = sqrt(vxA**2 + vyA**2)
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#just the basic physics equations for the ball2
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
xB = x0B + vx0B*btB
yB = y0B + vy0B*btB - .5*g*btB**2
vxB = vx0B
vyB = vy0B - g*btB
vB = sqrt(vxB**2 + vyB**2)
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#defines ball new position based of the generated vector
#/////////////////////////////////////////////////////////////////////
ball.pos = vector(xA,yA,0)
ball2.pos = vector(xB,yB,0)
#/////////////////////////////////////////////////////////////////////
#ball bounce phyics
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#pos.y<=x; x defines the height at which the ball bounces
if ball.pos.y<-.005-R:
#Ball bounce vector changes
#/////////////////////////////////
vyA = -sqrt(c)*vyA
vxA = sqrt(c)*vxA
vy0A = vyA
vx0A = vxA
x0A = xA
y0A = yA
#/////////////////////////////////
bt = 0
btA = 0
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#pos.y<=x; x defines the height at which the ball bounces
if ball2.pos.y<-.005-R:
#Ball2 bounce vector changes
#/////////////////////////////////
vyB = -sqrt(c)*vyB
vxB = sqrt(c)*vxB
vy0B = vyB
vx0B = vxB
x0B = xB
y0B = yB
#/////////////////////////////////
bt = 0
btB = 0
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
if ball.pos.y>=ball2.pos.y: ##
vyA = -sqrt(c)*vyA
vxA = sqrt(c)*vxA
vy0A = vyA
vx0A = vxA
x0A = xA
y0A = yA
#/////////////////////////////////
bt = 0
btA = 0
if ball2.pos.y<=ball.pos.y: ##
#Ball2 bounce vector changes
#/////////////////////////////////
vyB = -sqrt(c)*vyB
vxB = sqrt(c)*vxB
vy0B = vyB
vx0B = vxB
x0B = xB
y0B = yB
#/////////////////////////////////
bt = 0
btB = 0
if vyA<=0.01:
vyA=0
if vyB<=0.01:
vyB=0
# if ball2.pos.y<=ball.pos.y:
#updates graph and time
#/////////////////////////////////
K = .5*mA*(vxA**2+vyA**2)
U = mA*g*yA
E = K+U
fp.plot(t,yA)
fv.plot(t,vyA)
fE.plot(t,E)
fK.plot(t,K)
fU.plot(t,U)
fyA.plot(t,vyA)
fyB.plot(t,vyB)
t = t + dt
btA = btA + dt
btB = btB + dt
bt = bt + dt
#/////////////////////////////////
###############################################################################################
#prints time
print("t = ",t ,"seconds")`
答:
1赞
user1114907
6/2/2023
#1
你的程序太复杂了,可以找出可能出错的地方。我敦促您放弃使用运动学公式来更新位置。这是一个更简单的方案:https://www.glowscript.org/#/user/Bruce_Sherwood/folder/Examples/program/00Demo/edit,您计算物体上的净力 F 并将物体的动量更新为 ball.p += F*dt,并将物体的位置更新为 ball.pos += (ball.p/ball.m)*dt。您可以通过发现两个球的中心之间的距离小于其半径之和来检测碰撞。请注意,询问 Web VPython 的更好地方是论坛 https://groups.google.com/g/glowscript-users
评论