Untitled

mail@pastecode.io avatar
unknown
python
a year ago
3.5 kB
5
Indexable
# Built-In Imports:
import math


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)

    # noinspection PyMethodMayBeStatic
    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:
        """

        return velocity * math.sin(self.angle_radians)

    def get_x_velocity(self, velocity):
        """ The returned argument is the horizontal velocity component of the initial velocity """

        return velocity * math.cos(self.angle_radians)

    def get_max_time(self, velocity, height, grav_acceleration):
        parabola_time = 2 * (self.get_y_velocity(velocity) / grav_acceleration)

        if height == 0:
            return parabola_time

        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_position(self, time=0):
        pass


if __name__ == "__main__":

    blobby = Projectile(30, 10, 40)
    print(blobby.max_time)