Python Forum

Full Version: Numerically determining the highest a ball travels when shot straight up
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all, I'm very new to python and I have no prior experience with other coding languages, so my main issues are with the formatting and finding the appropriate vocabulary. 

My goal is to determine the maximum height a ball reaches when thrown straight up.  I already wrote the code for the visual of the ball, but I don't know how to find and display a maximum value.  Any other advice or suggestions in regards to the code I already have would also be greatly appreciated.

Thanks!  Smile

from visual import *

myuniv = display(range = vector(3,3,3))

So = vector(0,1,0)
Vo = vector(0,6,0)
a = vector(0,-9.8,0)
s = vector(0,2,0)

dt = 0.1
t = 0.0

ball = sphere(pos = s, radius = 0.25, color = color.cyan)

while s.y > -2.0:
    rate(25)
    s = .5*a*t**2 + Vo*t + So
    v = a*t + Vo
    t = t + dt
    ball.pos = s
    ballghost = sphere(pos = s, radius = .2)
I'm guessing s is the current height, since ball.pos = s.  In that case, just keep track of s.

Something like
max_pos = 0
while s.y > -2.0:
   s = #... calculate s
   if s > max_pos:
       max_pos = s
Thanks! This worked out perfectly.  I wasn't sure how to set up an "if" statement before.  Big Grin
Was you get is not the maximum height of the ball, but the maximum height you compute, and the accuracy therefore depends on the size of your samples. Getting the real value isn't that difficult (t for which v==0 so tmax=-Vo/a).