Example The Projectile Class

Returning to the cannonball example, we want a class that can represent projectiles. This class will need a contructor to initialize instance variables, an update method to change the state of the projectile, and getX and getY methods so that we can find the current position.

Let's start with the constructor. In the main program, we will create a cannonball from the initial angle, velocity and height.

cball = Projectile(angle, vel, h0)

The Projectile class must have an __init__ method that uses these values to initialize the instance variables of cball. But what should the instance variables be? Of course, they will be the four pieces of information that characterize the flight of the cannonball: xpos, ypos, xvel and yvel. We will calculate these values using the same formulas that were in the original program. Here is how our class looks with the constructor:

class Projectile:

def _init_(self, angle, velocity, height):

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

Notice how we have created four instance variables inside the object using the self dot notation. The value of theta is not needed after __init__ terminates, so it is just a normal (local) function variable.

The methods for accessing the position of our projectiles are straightforward; the current position is given by the instance variables xpos and ypos. We just need a couple methods that return these values.

def getX(self):

return self.xpos def getY(self):

return self.ypos

Finally, we come to the update method. This method takes a single normal parameter that represents an interval of time. We need to update the state of the projectile to account for the passage of that much time. Here's the code:

def update(self, time):

self.xpos = self.xpos + time * self.xvel yvel1 = self.yvel - time * 9.8

self.ypos = self.ypos + time * (self.yvel + yvel1)/2.0 self.yvel = yvel1

Basically, this is the same code that we used in the original program updated to use and modify instance variables. Notice the use of yvel1 as a temporary (ordinary) variable. This new value is saved by storing it into the object in the last line of the method.

That completes our projectile class. We now have a complete object-based solution to the cannonball problem.

# cball3.py from math import pi, sin, cos class Projectile:

def _init_(self, angle, velocity, height):

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 getX(self):

return self.xpos def getInputs():

a = input("Enter the launch angle (in degrees): ") v = input("Enter the initial velocity (in meters/sec): ") 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) print "\nDistance traveled: %0.1f meters." % (cball.getX())

Was this article helpful?

0 0

Post a comment