简体   繁体   中英

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. 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:

Accel -4.29956602269e+105 D: -1.07489150567e+107 Vel: -2.14978301134e+106 Which I'm pretty sure the velocity shouldn't be THAT low

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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