Hi,
I have been trying to solve a problem,i need to write code to an existing projectile class to calculate the maximum height reached by a projectile ,i have tried all possible solutions but seem stuck,here is the code below

# Canonball in form of a class
# projectile.py

from math import pi,sin,cos

class Projectile:

    def __init__(self,angle,velocity,height):
        self.xpos=0.0
        self.ypos=height
        theta=pi*angle/180.0
        self.xvel=velocity*cos(theta)
        self.yvel=velocity*sin(theta)

    def update(self,time):
        self.xpos=self.xpos+time*self.xvel
        yvel1=self.yvel-9.8*time
        self.ypos=self.ypos+time*(self.yvel+yvel1)/2.0
        self.yvel=yvel1



    def getY(self):
        return self.ypos

    def maxHeight(self,time):
        self.ypos=self.ypos+time*(4.9*time)
        return self.ypos


    def getX(self):
        return self.xpos

def getInputs():
    a=input('Enter the launch angle in degrees : ')
    v=input('Enter the initial velocity in m/s : ')
    h=input('Enter the initial height in meters : ')
    t=input('Enter the time interval between position calculations :')
    return a,v,h,t

def main():
    zenith=0.0
    angle,vel,h0,time=getInputs()
    cball=Projectile(angle,vel,h0)
    while cball.getY()>=0:
        cball.update(time)
        if cball.yvel==0:
            zenith=cball.maxHeight()





    print '\nDistance traveled: %0.1f meters .'%(cball.getX())
    print '\nMaximum height: ',zenith

Please can somebody help me,Thanks in advance

Recommended Answers

All 2 Replies

The maximum height is where yvel = 0. In your initialization method you have:
self.yvel=velocity*sin(theta)
You know that yvel goes to zero when 0.98*time equals the initial velocity, or at
velocity*sin(theta)/9.8 seconds
So you can figure out when you get to that time at your interval.
Now since xvel is presumed not to change, the xpos at that time is
(velocity*sin(theta)/9.8)*velocity*cos(theta)

thanks fot the tip,i just got confused a little bit,here is the working code

# Canonball in form of a class
# projectile.py

from math import pi,sin,cos

class Projectile:

    def __init__(self,angle,velocity,height):
        self.xpos=0.0
        self.ypos=height
        theta=pi*angle/180.0
        self.xvel=velocity*cos(theta)
        self.yvel=velocity*sin(theta)


    def update(self,time):
        self.xpos=self.xpos+time*self.xvel
        yvel1=self.yvel-9.8*time
        self.ypos=self.ypos+time*(self.yvel+yvel1)/2.0
        self.yvel=yvel1



    def getY(self):
        return self.ypos

    def maxHeight(self):
        time=self.yvel/9.8
        self.maxheight=self.ypos+time*(self.yvel/2.0)
        return self.maxheight


    def getX(self):
        return self.xpos

def getInputs():
    a=input('Enter the launch angle in degrees : ')
    v=input('Enter the initial velocity in m/s : ')
    h=input('Enter the initial height in meters : ')
    t=input('Enter the time interval between position calculations :')
    return a,v,h,t

def main():
    angle,vel,h0,time=getInputs()
    cball=Projectile(angle,vel,h0)
    while cball.getY()>=0:
        cball.update(time)
        zenith=cball.maxHeight()







    print '\nDistance traveled: %0.1f meters .'%(cball.getX())
    print '\nMaximum height: ',zenith
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.