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

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

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:

  1. # 这是图形
  2. ##################################################
  3. g1 = graph(xtitle="t
    展开收缩
    "
    , ytitle="y [m]", width=500, height=150)
  4. fp = gcurve(color=color.green)
  5. g2 = graph(xtitle="t
    展开收缩
    "
    , ytitle="v [m/s]", width=500, height=150)
  6. fv = gcurve(color=color.red)
  7. g3 = graph(xtitle="t
    展开收缩
    "
    , ytitle="E [J]", width=500, height=150)
  8. fK = gcurve(color=color.blue)
  9. fU = gcurve(color=color.red)
  10. fE = gcurve(color=color.magenta)
  11. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  12. g4 = graph(xtitle="t
    展开收缩
    "
    , ytitle="y [m]", width=500, height=150)
  13. fyA = gcurve(color=color.green)
  14. fyB = gcurve(color=color.orange)
  15. ##################################################
  16. #定义弹跳的地板
  17. #//////////////////////////////////////////////////////////////////////////////////////////
  18. R = 0.02
  19. floor = box(pos=vector(0,-0.005-R,0), size=vector(0.1,0.01,0.1))
  20. #//////////////////////////////////////////////////////////////////////////////////////////
  21. #一些基本参数/初始条件
  22. ##################################################
  23. h = .3
  24. #g = vector(0,-9.8,0)
  25. g = 9.8
  26. c = 0.5
  27. t = 0
  28. dt = 0.01
  29. bt = 0
  30. #球的定义
  31. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  32. mA = 2
  33. x0A = 0
  34. y0A = h
  35. vx0A = 0
  36. vy0A = 0
  37. dtA = 0.01
  38. btA = 0
  39. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  40. #球2的定义
  41. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  42. mB = .05
  43. x0B = 0
  44. y0B = h + .1
  45. vx0B = 0
  46. vy0B = 0
  47. dtB = 0.01
  48. btB = 0
  49. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  50. ##################################################
  51. #定义一个球
  52. #//////////////////////////////////////////////////////////////////////////////////////////
  53. ball = sphere(pos=vector(0,h,0), radius=R, color=color.green, make_trail=True, trail_type="points",
  54. interval=10, retain=10)
  55. ball2 = sphere(pos=vector(0,h,0), radius=R, color=color.orange, make_trail=True, trail_type="points",
  56. interval=10, retain=10)
  57. #//////////////////////////////////////////////////////////////////////////////////////////
  58. #球的物理
  59. ##################################################
  60. #t<x; x定义了多少秒
  61. while t < 2:
  62. #rate(x); x定义了更新速率
  63. rate(10)
  64. #球的基本物理方程
  65. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  66. xA = x0A + vx0A * btA
  67. yA = y0A + vy0A * btA - .5 * g * btA**2
  68. vxA = vx0A
  69. vyA = vy0A - g * btA
  70. vA = sqrt(vxA**2 + vyA**2)
  71. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  72. #球2的基本物理方程
  73. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  74. xB = x0B + vx0B * btB
  75. yB = y0B + vy0B * btB - .5 * g * btB**2
  76. vxB = vx0B
  77. vyB = vy0B - g * btB
  78. vB = sqrt(vxB**2 + vyB**2)
  79. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  80. #根据生成的矢量定义球的新位置
  81. #/////////////////////////////////////////////////////////////////////
  82. ball.pos = vector(xA, yA, 0)
  83. ball2.pos = vector(xB, yB, 0)
  84. #/////////////////////////////////////////////////////////////////////
  85. #球弹跳物理
  86. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  87. #pos.y<x; x定义了球弹跳的高度
  88. if ball.pos.y < -0.005 - R:
  89. #球弹跳矢量变化
  90. #/////////////////////////////////
  91. vyA = -sqrt(c) * vyA
  92. vxA = sqrt(c) * vxA
  93. vy0A = vyA
  94. vx0A = vxA
  95. x0A = xA
  96. y0A = yA
  97. #/////////////////////////////////
  98. bt = 0
  99. btA = 0
  100. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  101. #pos.y<x; x定义了球弹跳的高度
  102. if ball2.pos.y < -0.005 - R:
  103. #球2弹跳矢量变化
  104. #/////////////////////////////////
  105. vyB = -sqrt(c) * vyB
  106. vxB = sqrt(c) * vxB
  107. vy0B = vyB
  108. vx0B = vxB
  109. x0B = xB
  110. y0B = yB
  111. #/////////////////////////////////
  112. bt = 0
  113. btB = 0
  114. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  115. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  116. if ball.pos.y >= ball2.pos.y: ##
  117. vyA = -sqrt(c) * vyA
  118. vxA = sqrt(c) * vxA
  119. vy0A = vyA
  120. vx0A = vxA
  121. x0A = xA
  122. y0A = yA
  123. #/////////////////////////////////
  124. bt = 0
  125. btA = 0
  126. if ball2.pos.y <= ball.pos.y: ##
  127. #球2弹跳矢量变化
  128. #/////////////////////////////////
  129. vyB = -sqrt(c) * vyB
  130. vxB = sqrt(c) * vxB
  131. vy0B = vyB
  132. vx0B = vxB
  133. x0B = xB
  134. y0B = yB
  135. #/////////////////////////////////
  136. bt = 0
  137. btB = 0
  138. if vyA <= 0.01:
  139. vyA = 0
  140. if vyB <= 0.01:
  141. vyB = 0
  142. #如果ball2.pos.y < = ball.pos.y:
  143. #更新图形和时间
  144. #/////////////////////////////////
  145. K = .5 * mA * (vxA**2 + vyA**2)
  146. U = mA * g * yA
  147. E = K + U
  148. fp.plot(t, yA)
  149. fv.plot(t
  150. <details>
  151. <summary>英文:</summary>
  152. 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.
  153. 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.
  154. link to code: https://www.glowscript.org/#/user/amirdmgazzali/folder/bouncingballs/program/bouncingballs1
  155. this is the code:
  156. \` Web VPython 3.2
  157. #these are the graphs
  158. ###############################################################################################
  159. g1 = graph(xtitle=&quot;t
    展开收缩
    &quot;,ytitle=&quot;y [m]&quot;, width=500, height=150)
  160. fp = gcurve(color=color.green)
  161. g2 = graph(xtitle=&quot;t
    展开收缩
    &quot;,ytitle=&quot;v [m/s]&quot;, width=500, height=150)
  162. fv = gcurve(color=color.red)
  163. g3 = graph(xtitle=&quot;t
    展开收缩
    &quot;,ytitle=&quot;E [J]&quot;, width=500, height=150)
  164. fK = gcurve(color=color.blue)
  165. fU = gcurve(color=color.red)
  166. fE = gcurve(color=color.magenta)
  167. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  168. g4 = graph(xtitle=&quot;t
    展开收缩
    &quot;,ytitle=&quot;y [m]&quot;, width=500, height=150)
  169. fyA = gcurve(color=color.green)
  170. fyB = gcurve(color=color.orange)
  171. #g5 = graph(xtitle=&quot;t
    展开收缩
    &quot;,ytitle=&quot;y [m]&quot;, width=500, height=150)
  172. #fyB = gcurve(color=color.red)
  173. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  174. ###############################################################################################
  175. #defining a floor to bounce on
  176. #//////////////////////////////////////////////////////////////////////////////////////////
  177. R = 0.02
  178. floor = box(pos = vector(0,-0.005-R,0),size=vector(0.1,0.01,0.1))
  179. #//////////////////////////////////////////////////////////////////////////////////////////
  180. #some basic perameters/initial conditions
  181. ###############################################################################################
  182. h = .3
  183. #g = vector(0,-9.8,0)
  184. g = 9.8
  185. c = 0.5
  186. t = 0
  187. dt = 0.01
  188. bt = 0
  189. #ball definitions
  190. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  191. mA = 2
  192. x0A=0
  193. y0A=h
  194. vx0A=0
  195. vy0A=0
  196. dtA = 0.01
  197. btA = 0
  198. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  199. #ball2 definitions
  200. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  201. mB = .05
  202. x0B=0
  203. y0B=h+.1
  204. vx0B=0
  205. vy0B=0
  206. dtB = 0.01
  207. btB = 0
  208. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  209. ###############################################################################################
  210. #defining a ball
  211. #//////////////////////////////////////////////////////////////////////////////////////////
  212. ball = sphere(pos=vector(0,h,0), radius=R, color=color.green, make_trail=True, trail_type=&quot;points&quot;,
  213. interval=10, retain=10)
  214. ball2 = sphere(pos=vector(0,h,0), radius=R, color=color.orange, make_trail=True, trail_type=&quot;points&quot;,
  215. interval=10, retain=10)
  216. #//////////////////////////////////////////////////////////////////////////////////////////
  217. #physics behind the ball
  218. ###############################################################################################
  219. #t&lt;x; x defines how many seconds
  220. while t&lt;2:
  221. #rate(x); x defines update rate
  222. rate(10)
  223. #just the basic physics equations for the ball
  224. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  225. xA = x0A + vx0A*btA
  226. yA = y0A + vy0A*btA - .5*g*btA**2
  227. vxA = vx0A
  228. vyA = vy0A - g*btA
  229. vA = sqrt(vxA**2 + vyA**2)
  230. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  231. #just the basic physics equations for the ball2
  232. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  233. xB = x0B + vx0B*btB
  234. yB = y0B + vy0B*btB - .5*g*btB**2
  235. vxB = vx0B
  236. vyB = vy0B - g*btB
  237. vB = sqrt(vxB**2 + vyB**2)
  238. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  239. #defines ball new position based of the generated vector
  240. #/////////////////////////////////////////////////////////////////////
  241. ball.pos = vector(xA,yA,0)
  242. ball2.pos = vector(xB,yB,0)
  243. #/////////////////////////////////////////////////////////////////////
  244. #ball bounce phyics
  245. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  246. #pos.y&lt;=x; x defines the height at which the ball bounces
  247. if ball.pos.y&lt;-.005-R:
  248. #Ball bounce vector changes
  249. #/////////////////////////////////
  250. vyA = -sqrt(c)*vyA
  251. vxA = sqrt(c)*vxA
  252. vy0A = vyA
  253. vx0A = vxA
  254. x0A = xA
  255. y0A = yA
  256. #/////////////////////////////////
  257. bt = 0
  258. btA = 0
  259. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  260. #pos.y&lt;=x; x defines the height at which the ball bounces
  261. if ball2.pos.y&lt;-.005-R:
  262. #Ball2 bounce vector changes
  263. #/////////////////////////////////
  264. vyB = -sqrt(c)*vyB
  265. vxB = sqrt(c)*vxB
  266. vy0B = vyB
  267. vx0B = vxB
  268. x0B = xB
  269. y0B = yB
  270. #/////////////////////////////////
  271. bt = 0
  272. btB = 0
  273. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  274. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  275. if ball.pos.y&gt;=ball2.pos.y: ##
  276. vyA = -sqrt(c)*vyA
  277. vxA = sqrt(c)*vxA
  278. vy0A = vyA
  279. vx0A = vxA
  280. x0A = xA
  281. y0A = yA
  282. #/////////////////////////////////
  283. bt = 0
  284. btA = 0
  285. if ball2.pos.y&lt;=ball.pos.y: ##
  286. #Ball2 bounce vector changes
  287. #/////////////////////////////////
  288. vyB = -sqrt(c)*vyB
  289. vxB = sqrt(c)*vxB
  290. vy0B = vyB
  291. vx0B = vxB
  292. x0B = xB
  293. y0B = yB
  294. #/////////////////////////////////
  295. bt = 0
  296. btB = 0
  297. if vyA&lt;=0.01:
  298. vyA=0
  299. if vyB&lt;=0.01:
  300. vyB=0
  301. # if ball2.pos.y&lt;=ball.pos.y:
  302. #updates graph and time
  303. #/////////////////////////////////
  304. K = .5*mA*(vxA**2+vyA**2)
  305. U = mA*g*yA
  306. E = K+U
  307. fp.plot(t,yA)
  308. fv.plot(t,vyA)
  309. fE.plot(t,E)
  310. fK.plot(t,K)
  311. fU.plot(t,U)
  312. fyA.plot(t,vyA)
  313. fyB.plot(t,vyB)
  314. t = t + dt
  315. btA = btA + dt
  316. btB = btB + dt
  317. bt = bt + dt
  318. #/////////////////////////////////
  319. ###############################################################################################
  320. #prints time
  321. print(&quot;t = &quot;,t ,&quot;seconds&quot;)`
  322. </details>
  323. # 答案1
  324. **得分**: 1
  325. 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
  326. <details>
  327. <summary>英文:</summary>
  328. 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
  329. </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:

确定