简体   繁体   English

Python蹦极绳模拟

[英]Python Bungee Cord Simulation

I am trying to use visual to simulate a bungee jumper (I tried searching for a very similar answer here but it didn't provide an answer). 我正在尝试使用视觉效果模拟蹦极跳线(我尝试在此处搜索非常相似的答案,但未提供答案)。 My code is 我的代码是

def animateBungeeJump(mass, deltaT, simulationTime, surfaceArea, unstretchedBungeeLength):
g = 9.8
k = 21.7
vel = 0.0
accel = 0.0
distance = unstretchedBungeeLength
time_start = 0.0
d = 0.0

while(time_start <= simulationTime):
    Fg = mass * g
    Ff = (-0.65) * surfaceArea * vel * abs(vel)
    Fs = (-1) * k * d
    Ft = Fg + Ff + Fs

    accel = Ft / mass
    vel = accel * time_start
    d += vel * time_start

    print "Accel", accel
    print "D:", d
    print "Vel:", vel
    print ""
    time_start += deltaT
animateBungeeJump(60, 0.1, 5.0, 0.2, 30)

The result I want to see when debugging this is seeing how acceleration bounces and in general distance (d) should bounce up and down, but while running the code constantly it goes to -inf and inf if I used a simulationTime of 10s or more. 我想在调试时看到的结果是看到加速度如何弹跳,并且通常应该在上下距离(d)弹跳,但是在不断运行代码的情况下,如果我使用了10s或更长时间的SimulationTime,它将变为-inf和inf。 I don't know if I'm doing all of the math correctly. 我不知道我是否在正确地进行所有数学运算。 Sorry if my code looks stupid, I'm learning. 抱歉,如果我的代码看起来很愚蠢,我正在学习。

Example output of the end when it's around 5 seconds: 大约5秒钟时结束的示例输出:

Accel -4.29956602269e+105 D: -1.07489150567e+107 Vel: -2.14978301134e+106 Which I'm pretty sure the velocity shouldn't be THAT low Accel -4.29956602269e+105 D: -1.07489150567e+107 Vel: -2.14978301134e+106我很确定速度不应该那么低

Your incremental changes aren't done properly. 您的增量更改未正确完成。 The velocity is a change since last time; 速度是自上次以来的变化; both velocity and distance should be updated according to the time span, not the entire running time. 速度和距离都应根据时间跨度而不是整个运行时间进行更新。 Try these: 试试这些:

vel += accel * deltaT
d += vel * deltaT

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

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