当我运行它时,循环以改变弹簧长度并不会改变任何东西

时间:2017-05-22 13:58:02

标签: vpython helix-3d-toolkit helix

我正在研究的项目应该模拟一个脉冲,该脉冲沿着由弹簧连接的一组球体向下移动。当脉冲向下移动时,我试图减小弹簧长度,但是当我运行它时,没有任何反应。

这是我的代码:

    from visual import *

one = sphere(pos=(-10,0,0), radius = 0.5, color = color.red)
two = sphere(pos=(-8,0,0), radius = 0.5, color = color.orange)
three = sphere(pos=(-6,0,0), radius = 0.5, color = color.yellow)
four = sphere(pos=(-4,0,0), radius = 0.5, color = color.green)
five = sphere(pos=(-2,0,0), radius = 0.5, color = color.blue)
six = sphere(pos=(0,0,0), radius = 0.5, color = color.cyan)
seven = sphere(pos=(2,0,0), radius = 0.5, color = color.magenta)
eight = sphere(pos=(4,0,0), radius = 0.5, color = color.white)
nine = sphere(pos=(6,0,0), radius = 0.5, color = color.red)
ten = sphere(pos=(8,0,0), radius = 0.5, color = color.orange)

spring1 = helix(pos = (-10, 0, 0), length = 2, radius = 0.3,
   thickness = 0.05, color = color.red)
spring2 = helix(pos = (-8, 0, 0), length = 2, radius = 0.3,
   thickness = 0.05, color = color.orange)
spring3 = helix(pos = (-6, 0, 0), length = 2, radius = 0.3,
   thickness = 0.05, color = color.yellow)
spring4 = helix(pos = (-4, 0, 0), length = 2.0, radius = 0.3,
   thickness = 0.05, color = color.green)
spring5 = helix(pos = (-2, 0, 0), length = 2.0, radius = 0.3,
   thickness = 0.05, color = color.blue)
spring6 = helix(pos = (0, 0, 0), length = 2.0, radius = 0.3,
   thickness = 0.05, color = color.cyan)
spring7 = helix(pos = (2, 0, 0), length = 2.0, radius = 0.3,
   thickness = 0.05, color = color.magenta)
spring8 = helix(pos = (4, 0, 0), length = 2.0, radius = 0.3,
   thickness = 0.05, color = color.white)
spring9 = helix(pos = (6, 0, 0), length = 2.0, radius = 0.3,
   thickness = 0.05, color = color.red)

masses = [one, two, three, four, five, six, seven, eight, nine, ten]
springs = [spring1, spring2, spring3, spring4, spring5, spring6, spring7,
           spring8, spring9]

while True:
    n=0
    deltax=.2
    while n < 10:
        rate(30)
        masses[n].pos.x = masses[n].pos.x + deltax
        if n < 9:
            springs[n].pos = masses[n].pos
            springs[n].axis = masses[n+1].pos-masses[n].pos
        n=n+1

    n = n-1
    while n >= 0:
        rate(30)
        masses[n].pos.x = masses[n].pos.x - deltax
        if n < 0:
            springs[n-1].pos = masses[n-1].pos - deltax
            springs[n-1].axis = masses[n].pos-masses[n-1].pos
        n = n-1


while True:
    m=0
    deltat=.2
    while m<9:
        rate(30)
        springs[m].length = springs[m].length - deltat
        springs[m].length = springs[m].length + deltat
        m=m+1

    m=m-1
    while n>=0:
        rate(30)
        springs[m].length = springs[m].length - deltat
        springs[m].length = springs[m].length + deltat
        m=m-1

1 个答案:

答案 0 :(得分:0)

使质量具有不透明度= 0.3并用scene.waitfor替换第一个速率语句(&#39;单击&#39;)。你会看到当你向右移动第n个质量时,弹簧实际上会缩短,当第(n + 1)个质量移动时,右边的弹簧会缩短,在第n个之间留下间隙春天和第(n + 1)个春天。

顺便说一下,我不明白哪里有第二个&#34;而真实&#34;。它永远不会到达。

我将宣传一个更好的地方来提出VPython问题是在VPython论坛上

https://groups.google.com/forum/?fromgroups&hl=en#!forum/vpython-users