Python coding: create a new "class" to calculate motion of a projectile

927 views Asked by At

I'm working on a project to create a new "class" in python to do some kinematics calculations.

But, I'm having trouble seeing how to build the formula in python given the common physics kinematics equations.

Here is the general goal:

"Construct a class named projectile. The class variable should include the height from which the projectile is shot, the height at which it lands, the initial velocity, and the angle of the initial velocity with respect to the horizontal. The class methods should include a calculation of the range of the projectile, its maximum height and its final velocity."

I got as far as defining the new class and the variables, but am not sure how to build the functions. Any assistance would be so helpful. Here is the code so far:

import math
import numpy as np
import matplotlib.pyplot as plt
import pylab
import os

# acceleration due to gravity = 9.80665 m/s**2

class Projectile(object):
# h0 is vertical starting position which starts at y = 0
    def __init__(self,h0,v0,theta):
        self.h0 = h0 #height
        self.v0 = v0 #speed
        self.theta = theta # angle above horizontal
        self.v0x = v0*math.cos(theta) #x component of velocity
        self.v0y = v0*math.sin(theta) #y component of velocity
1

There are 1 answers

0
TKA On
  • Three equations of motions relating initial and final velocity, distance, acceleration and time.
  • Each can be used separately for horizontal and vertical components for all variables.
  • for max height use vertical components. Final vertical velocity is zero and acceleration is due to gravity.
  • to get range first calculate flight time using the distance formula with vertical components of variables (distance being difference of initial and final heights)
  • than use the time to calculate final values. You may have to calculate horizontal and vertical values and then sum them up.