Modeling a Bouncing Ball with Python

preview_player
Показать описание
Here is a model of a bouncing ball

Here is the python code
Рекомендации по теме
Комментарии
Автор

You can be an awesome physicist, but this just shows that programming is hard.
This whole thing gave me highschool nostalgia writing turbo pascal to bounce balls and rotate cubes. Cheers!
By the way, your ball goes back to 0 because you set x0 = 0 after it hits the ground.

teenspirit
Автор

You need to set the x0 = x when checking if the ball.y <= 0. When setting it to: 0 it just teleports the ball back to the center of the screen, but by updating the initial x to the current x, the x velocity works correctly!
P.s: I didn't understand whatever you were talking about with the math and stuff, so I just looked at the code and studied what it was doing. I needed the formula for this for a project I am working on, so thanks for the video!

EDIT:
Heres the fixed code:
GlowScript 3.2 VPython
g1 = graph(xtitle="t [s]", ytitle="y [m]", width=500, height=150)
fp = gcurve(color=color.blue)
g2 = graph(xtitle="t [s]", ytitle="v [m/s]", width=500, height=150)
fv = gcurve(color=color.red)
g3 = graph(xtitle="t [s]", ytitle="E [J]", width=500, height=150)
fK = gcurve(color=color.blue)
fU = gcurve(color=color.red)
fE = gcurve(color=color.magenta)
R = 0.01
floor = box(pos = vector(0, -0.005-R, 0), size=vector(.5, 0.01, 0.2))

h = .3
#g = vector(0, -9.8, 0)
g = 9.8
m = 0.02
x0=0
y0=h
vx0=1
vy0 = 0
c = 0.5
ball = sphere(pos=vector(x0, h, 0), radius=R, color=color.yellow, make_trail=True,
trail_type="points",
interval=1, retain=1000)

t = 0
dt = 0.01
bt = 0

while t< 4:
rate(100)
x = x0 + vx0*bt
y = y0 + vy0*bt - .5*g*bt**2
vx = vx0
vy = vy0 - g*bt

ball.pos = vector(x, y, 0)
if ball.pos.y<=0:
vy = -sqrt(c)*vy
vx = sqrt(c)*vx
vy0 = vy
vx0 = vx
x0 = x # THIS IS WHAT I CHANGED, Originally it was resetting the x0 to 0, but by setting it to the current x pos of the ball, it continues its x motion
y0 = 0
bt = 0
K = .5*m*(vx**2+vy**2)
U = m*g*y
E = K+U
fp.plot(t, y)
fv.plot(t, vy)
fE.plot(t, E)
fK.plot(t, K)
fU.plot(t, U)
t = t + dt
bt = bt + dt

print("t = ", t, " s")

villocity
Автор

This is such a helpful video! Thank you! Great work.

lilliank.
Автор

This is suck an awesome video!!! good Work !!! so Helpfull!!! MATH!!!

harryfhumbert
join shbcf.ru