我正在使用VPython构建这个双弹跳球模拟,并遇到了奇怪的橡皮筋问题。

huangapple go评论86阅读模式
英文:

I'm building this double bouncing ball simulation with vpython and am running into a weird rubber banding issue

问题

I understand that you want a translation of the provided code snippet. Here is the translated code:

# 这是图形
################################################## 
g1 = graph(xtitle="t 
展开收缩
"
, ytitle="y [m]", width=500, height=150)
fp = gcurve(color=color.green) g2 = graph(xtitle="t
展开收缩
"
, ytitle="v [m/s]", width=500, height=150)
fv = gcurve(color=color.red) g3 = graph(xtitle="t
展开收缩
"
, 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
展开收缩
"
, ytitle="y [m]", width=500, height=150)
fyA = gcurve(color=color.green) fyB = gcurve(color=color.orange) ################################################## #定义弹跳的地板 #////////////////////////////////////////////////////////////////////////////////////////// R = 0.02 floor = box(pos=vector(0,-0.005-R,0), size=vector(0.1,0.01,0.1)) #////////////////////////////////////////////////////////////////////////////////////////// #一些基本参数/初始条件 ################################################## h = .3 #g = vector(0,-9.8,0) g = 9.8 c = 0.5 t = 0 dt = 0.01 bt = 0 #球的定义 #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ mA = 2 x0A = 0 y0A = h vx0A = 0 vy0A = 0 dtA = 0.01 btA = 0 #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ #球2的定义 #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ mB = .05 x0B = 0 y0B = h + .1 vx0B = 0 vy0B = 0 dtB = 0.01 btB = 0 #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ################################################## #定义一个球 #////////////////////////////////////////////////////////////////////////////////////////// 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) #////////////////////////////////////////////////////////////////////////////////////////// #球的物理 ################################################## #t<x; x定义了多少秒 while t < 2: #rate(x); x定义了更新速率 rate(10) #球的基本物理方程 #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ xA = x0A + vx0A * btA yA = y0A + vy0A * btA - .5 * g * btA**2 vxA = vx0A vyA = vy0A - g * btA vA = sqrt(vxA**2 + vyA**2) #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ #球2的基本物理方程 #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ xB = x0B + vx0B * btB yB = y0B + vy0B * btB - .5 * g * btB**2 vxB = vx0B vyB = vy0B - g * btB vB = sqrt(vxB**2 + vyB**2) #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ #根据生成的矢量定义球的新位置 #///////////////////////////////////////////////////////////////////// ball.pos = vector(xA, yA, 0) ball2.pos = vector(xB, yB, 0) #///////////////////////////////////////////////////////////////////// #球弹跳物理 #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ #pos.y<x; x定义了球弹跳的高度 if ball.pos.y < -0.005 - R: #球弹跳矢量变化 #///////////////////////////////// vyA = -sqrt(c) * vyA vxA = sqrt(c) * vxA vy0A = vyA vx0A = vxA x0A = xA y0A = yA #///////////////////////////////// bt = 0 btA = 0 #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ #pos.y<x; x定义了球弹跳的高度 if ball2.pos.y < -0.005 - R: #球2弹跳矢量变化 #///////////////////////////////// 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: ## #球2弹跳矢量变化 #///////////////////////////////// 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 #如果ball2.pos.y < = ball.pos.y: #更新图形和时间 #///////////////////////////////// K = .5 * mA * (vxA**2 + vyA**2) U = mA * g * yA E = K + U fp.plot(t, yA) fv.plot(t <details> <summary>英文:</summary> Im trying to make a pair of balls bounce in 1D space and let them colide. I&#39;m running it on glowscript which uses webVPython, should be the same as VPython from what I can tell. I keep running into this issue where the balls tend to rubber band causing some weird collision issues where they can slowly phase through other objects, also I&#39;m too stupid to figure out how to calculate the momentum change, not sure if thats relavent to the rubber banding but if anyone knows an eqation I could use or a fix that would be appricated. link to code: https://www.glowscript.org/#/user/amirdmgazzali/folder/bouncingballs/program/bouncingballs1 this is the code: \` Web VPython 3.2 #these are the graphs ############################################################################################### g1 = graph(xtitle=&quot;t
展开收缩
&quot;,ytitle=&quot;y [m]&quot;, width=500, height=150)
fp = gcurve(color=color.green) g2 = graph(xtitle=&quot;t
展开收缩
&quot;,ytitle=&quot;v [m/s]&quot;, width=500, height=150)
fv = gcurve(color=color.red) g3 = graph(xtitle=&quot;t
展开收缩
&quot;,ytitle=&quot;E [J]&quot;, width=500, height=150)
fK = gcurve(color=color.blue) fU = gcurve(color=color.red) fE = gcurve(color=color.magenta) #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ g4 = graph(xtitle=&quot;t
展开收缩
&quot;,ytitle=&quot;y [m]&quot;, width=500, height=150)
fyA = gcurve(color=color.green) fyB = gcurve(color=color.orange) #g5 = graph(xtitle=&quot;t
展开收缩
&quot;,ytitle=&quot;y [m]&quot;, 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=&quot;points&quot;, interval=10, retain=10) ball2 = sphere(pos=vector(0,h,0), radius=R, color=color.orange, make_trail=True, trail_type=&quot;points&quot;, interval=10, retain=10) #////////////////////////////////////////////////////////////////////////////////////////// #physics behind the ball ############################################################################################### #t&lt;x; x defines how many seconds while t&lt;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&lt;=x; x defines the height at which the ball bounces if ball.pos.y&lt;-.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&lt;=x; x defines the height at which the ball bounces if ball2.pos.y&lt;-.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&gt;=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&lt;=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&lt;=0.01: vyA=0 if vyB&lt;=0.01: vyB=0 # if ball2.pos.y&lt;=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(&quot;t = &quot;,t ,&quot;seconds&quot;)` </details> # 答案1 **得分**: 1 Your program is way too (overly) complex to make it feasible to figure out what might be going wrong. I urge you to abandon using kinematics formulas to update positions. Here is a much simpler scheme: https://www.glowscript.org/#/user/Bruce_Sherwood/folder/Examples/program/00Demo/edit, where you calculate the net force F on an object and update an object's momentum as ball.p += F*dt and update the object's position as ball.pos += (ball.p/ball.m)*dt. You can detect collisions by finding that the distance between centers of the two balls is less than the sum of their radii. Note that a better place to ask about Web VPython is the forum https://groups.google.com/g/glowscript-users <details> <summary>英文:</summary> Your program is way to (overly) complex to make it feasible to figure out what might be going wrong. I urge you to abandon using kinematics formulas to update positions. Here is a much simpler scheme: https://www.glowscript.org/#/user/Bruce_Sherwood/folder/Examples/program/00Demo/edit, where you calculate the net force F on an object and update an object&#39;s momentum as ball.p += F*dt and update the object&#39;s position as ball.pos += (ball.p/ball.m)*dt. You can detect collisions by finding that the distance between centers of the two balls is less than the sum of their radii. Note that a better place to ask about Web VPython is the forum https://groups.google.com/g/glowscript-users </details>

huangapple
  • 本文由 发表于 2023年6月1日 05:05:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定