简体   繁体   English

春天不会振荡vpython

[英]spring wont oscillate vpython

from __future__ import division, print_function
from visual import *
scene.width = 400
scene.height = 760
## constants and data
g = 9.8
mball = 0.03
L0 = 0.26
ks = 1.8
sf = .5
sf2 = 3

#Graphs


## objects (origin is at ceiling)
ceiling = box(pos=vector(0,0,0), length=0.2, height=0.01, width=0.2)
ball = sphere(pos=vector(0,-0.3,0), radius=0.025, color=color.orange)
spring = helix(pos=ceiling.pos, axis=ball.pos-ceiling.pos,color=color.cyan, thickness=.003, coils=40, radius=0.010)
#more constants
Lvec= (ball.pos-ceiling.pos)
Lhat = Lvec / mag(Lvec)
## initial values
pball = mball*vector(0,0,0)
Fgrav = mball*g*vector(0,-1,0)
t = 0
deltat = .01
Fspring = -ks*ball.pos

#arrow vectors
Fharr = arrow(pos=ball.pos, axis=vector(0,0,0), color=color.green) #parallel
Ftarr = arrow(pos=ball.pos, axis=vector(0,0,0), color=color.blue) #perpendicular
parr = arrow(pos=ball.pos, axis=vector(0,0,0), color=color.magenta)

#improve the display
scene.autoscale = False ## turn off automatic camera zoom
scene.center = vector(0,-L0,0) ## move camera down
#scene.waitfor ('click')

## calculation loop
while t <10:
    rate(100)
    #length of L
    Lmag=abs(Lvec.y)
    Lhat = Lvec / mag(Lvec)
    #spring force
    Fspring = -ks*(mag(Lvec)-L0)*Lhat
    #updating position
    Fnet = Fgrav + Fspring
    pball = pball + Fnet*deltat
    ball.pos = ball.pos + (pball/mball)*deltat
    spring.axis = ball.pos - ceiling.pos
    #perpendicuar and parrellel forces
    phat = pball/mag(pball)
    #parallel
    FH = dot(Fnet,phat)*phat
    #perpendicular
    FT = Fnet - FH
    #updating arrows

    t = t + deltat

here's my code, my spring is suppose to oscillate but it wont. 这是我的代码,我的春天应该摆动,但是不会摆动。 It just shoots straight down and doesn't retract, can anyone help me? 它只是直射而不会缩回,有人可以帮我吗? I think I need to update the position or something in the while loop so when it reaches 0 momentum at the bottom , it retracts but i dont know how. 我想我需要在while循环中更新位置或其他内容,所以当它在底部达到0动量时,它会缩回,但我不知道如何。

您永远不会在循环中更改Lvec,因此它始终具有原始值,即使用ball.pos和ceiling.pos的原始值(ball.pos-ceiling.pos)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM