【问题标题】:VPython Physics Simulation Lagging After Running Pause Function运行暂停功能后 VPython 物理模拟滞后
【发布时间】:2021-05-23 16:38:51
【问题描述】:

我正在使用 VPython Glowscript 进行物理模拟,我需要能够通过按下按钮来暂停和播放我的程序。到目前为止,我已经添加了暂停/播放按钮,效果很好!但是,在取消暂停模拟后,一旦我第一次暂停它,它就会严重滞后。老实说,我不确定如何解决这个问题。有人可以帮忙吗?

这是link to my Vpython code,以及我在此处添加的代码。

GlowScript 3.0 VPython
scene.background = vector(0,0.7,1)
scene.width = 400



# Setting Inital Variables
degrees = 45
theta = -degrees*(pi/180)
omega = 0
L=1
alpha = -sin(theta)
t=0
tMax = 9999
dt = 0.01
damp = 0.5
driveAmp = 1.2
driveFreq = 2/3
running = True

def Run(b):
    global running, last_dt, dt
    running = not running
    if running:
        b.text = "Pause"
        dt = last_dt
    else:
        b.text = "Run"
        last_dt = dt
        dt = 0
    return
    
button(text="Pause", bind=Run)

support = box(pos=vector(0,0,0), size = vector(0.2, 0.01, 0.2)) 
ball = sphere(pos=vector(support.pos.x-L*sin(theta),support.pos.y-L*cos(theta),0), radius=0.05, color=color.green) 
string = cylinder(pos=support.pos, axis=ball.pos-support.pos, color=color.white, radius=0.008)

def torque(theta, omega, t):
    return -sin(theta) - damp*omega + driveAmp*sin(driveFreq*t)

    
# Create graph
Pendulum_graph = graph(width=600, height=375, 
        title="Theta vs Time",
        xtitle="<i>Time (s) </i>", ytitle="<i>Theta (radians) </i>",
        background=color.white)

# Set time variable, and the color of the graph dots
vyDots = gdots(color=color.green, interval=30)




while (True):
    rate(1/dt)
    
    alpha = torque(theta, omega, t)
    
    thetaMid = theta + omega*0.5*dt
    
    omegaMid = omega + alpha*0.5*dt
    
    dtmid = dt*0.5
    
    tmid = 0.5*t
    
    alphaMid = torque(thetaMid, omegaMid, tmid)
    
    theta += omegaMid * dt
    
    
    omega += alphaMid*dt
    
    vyDots.plot(t,theta)
    tmid+= dtmid
    
    t += dt
    
    x = sin(theta)
    y = -cos(theta)
    
    ball.pos = vector(x,y,0)
    string.axis = ball.pos
    
    
    

        

    
    
    
                                         # y = theta , vy = omega, alpha = ay
    

    # 12 -  10 seconds. What is the significance of this value?
    # 13 - The graph is all crazy for about 90 seconds, and then it eventually equals out into a perodic motion

    
    

【问题讨论】:

    标签: python python-3.x pause vpython glowscript


    【解决方案1】:

    另外,在循环中,根据“if running:”进行所有计算。

    【讨论】:

      【解决方案2】:

      问题出在这一行:

      rate(1/dt)
      

      当您按下按钮时,dt 变为零。不知道 GlowScript 是如何解决这个问题的(普通 Python 问题ZeroDivisionError,在 IDLE 中使用这个程序时)。所以用通常的方式改变它

      rate(100)
      

      会没事的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-26
        • 2022-01-13
        • 2019-01-20
        • 1970-01-01
        相关资源
        最近更新 更多