Untitled
unknown
plain_text
2 years ago
4.9 kB
5
Indexable
# Built-In Imports: import math # Third-Party Imports: import pygame 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, diameter 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, diameter=0.1, grav_acceleration=9.81, drag_coefficient=0): """ 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 diameter: The initial value for the diameter 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.diameter = diameter self.grav_acceleration = grav_acceleration self.drag_coefficient = drag_coefficient self.angle_radians = self.degrees_to_radians(angle_degrees) self.max_time = self.get_max_time(init_velocity, init_height, grav_acceleration) self.height = self.get_height(init_velocity, init_height, grav_acceleration) 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_y_velocity(self, velocity): """ Calculates vertical component of the user-inputted value for velocity, so you can compute SUVAT equations in the y-dimension. :param velocity: The velocity of the projectile. :return: The value of the vertical component of velocity. (Float) """ return velocity * math.sin(self.angle_radians) def get_x_velocity(self, velocity): """ 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 velocity * math.cos(self.angle_radians) def get_max_time(self, velocity, height, grav_acceleration): """ Calculates the total flight time of the projectile, and therefor the maximum flight time of the projectile. :param velocity: The velocity of the projectile. :param height: The initial height of the projectile. :param grav_acceleration: The value being used for the acceleration due to gravity. :return: Returns the value for maximum time of the projectile. (Flloat) """ parabola_time = 2 * (self.get_y_velocity(velocity) / grav_acceleration) # Calculates the time taken for the projectile to traverse the parabola if 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. vertical_velocity = self.get_y_velocity(velocity) # The vertical component of the initial velocity max_time = parabola_time + (-vertical_velocity + math.sqrt(vertical_velocity ** 2 + 2 * grav_acceleration * height)) / grav_acceleration return max_time def get_height(self, time, velocity, grav_acceleration): vertical_velocity = self.get_y_velocity(velocity) # The vertical component of the initial velocity if self.init_height != 0: pass else: height = vertical_velocity*time + 0.5*grav_acceleration*time**2 return height def get_x_displacement(self, velocity): pass def get_position(self, time=0): pass blobby = Projectile(30, 10) print(blobby.height)
Editor is loading...
Leave a Comment