Phase 2
unknown
python
a year ago
5.6 kB
6
Indexable
# Built-in imports: import math import numpy class Projectile: """ This class is responsible for defining a projectile within the system. It will calculate its position and display it on the screen. This is the general constructor for the projectile class. It sets the initial values for the angle of projection, initial velocity, initial height, mass of the projectile, the value for the acceleration due to gravity and the drag coefficient. """ def __init__(self, angle_degrees = 30, init_velocity = 10, init_height = 0, mass = 0.5, grav_acceleration = 9.81, drag_coefficient = 0.4): """ This is the general constructor for the projectile class. It sets the initial values for the angle of projection, initial velocity, initial height, diameter of the projectile, the value for the acceleration due to gravity and the drag coefficient. :param angle_degrees: The initial value for the angle of projection :param init_velocity: The initial value for the velocity of the projectile. :param init_height: The initial value for the height of the projectile. :param mass: The value for mass of the projectile. :param grav_acceleration: The initial value for the acceleration due to gravity. :param drag_coefficient: The initial value for the drag coefficient. """ self.angle_degrees = angle_degrees self.init_velocity = init_velocity self.init_height = init_height self.mass = mass self.grav_acceleration = grav_acceleration self.drag_coefficient = drag_coefficient self.angle_radians = self.degrees_to_radians(angle_degrees) def degrees_to_radians(self, angle_degrees): """ Converts the user inputted angle into radians, so you can utilise the values in various other calculations. :param angle_degrees: The initial value for the angle of projection :return: The value of the initial angle converted to radians. (Float) """ return math.radians(angle_degrees) def get_initial_y_velocity(self): """ Calculates vertical component of the user-inputted value for velocity, so you can compute SUVAT equations in the y-dimension. :return: The value of the vertical component of velocity. (Float) """ return self.init_velocity * math.sin(self.angle_radians) def get_initial_x_velocity(self): """ Calculates the horizontal component of the user-inputted value for velocity, ao you can compute SUVAT equations in the x-dimension. :param: velocity: The velocity of the projectile. :return: The value of the horizontal component of velocity. (Float) """ return self.init_velocity * math.cos(self.angle_radians) def get_max_time(self): """ Calculates the total flight time of the projectile, and therefor the maximum flight time of the projectile. :return: Returns the value for maximum time of the projectile. (Float) """ parabola_time = 2 * (self.get_initial_y_velocity() / self.grav_acceleration) # Calculates the time taken for the projectile to traverse the parabola if self.init_height == 0: # Selection statement to see whether the projectile needs to fall a further distance. return parabola_time # Calculation for the time taken to fall the further distance if the initial height of projection is > 0. max_time = parabola_time + (-(self.get_initial_y_velocity()) + math.sqrt( self.get_initial_y_velocity() ** 2 + 2 * self.grav_acceleration * self.init_height)) / self.grav_acceleration return max_time def get_height(self, time): """ Calculates the vertical displacement at a set time. :param time: The value of time to use in the calculation for vertical displacement. :return: The vertical displacement at the time set as the parameter. """ if time >= self.get_max_time(): return 0 else: return self.init_height + (self.get_initial_y_velocity() * time + (0.5 * -(self.grav_acceleration) * time ** 2)) def get_x_displacement(self, time): """ Calculates the horizontal displacement at a set time. :param time: The value of time to use in the calculation for horizontal displacement. :return: The horizontal displacement at the time set as the parameter. """ if time > self.get_max_time(): return self.get_initial_x_velocity() * self.get_max_time() else: return self.get_initial_x_velocity() * time def get_position(self): """ Calculates the position of the projectile as a system of co-ordinates: x-displacement against y-displacement :return: A 2D array with a series of co-ordinates at different times. """ position_array = [] for i in numpy.arange(0, self.get_max_time(), 0.01): # Returns evenly spaced values in the range. position_array.append([self.get_x_displacement(i), self.get_height(i), "time =",i]) # Calculation to append both the x-displacement, y-displacement and time in an array. return position_array test = Projectile(30,10, 0) print(test.get_position())
Editor is loading...
Leave a Comment