Created
December 27, 2013 21:03
-
-
Save dchapman1988/8152578 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mathn' | |
# Just a class with methods for calculating trajectories of a projectile | |
# Read more: http://en.wikipedia.org/wiki/Trajectory_of_a_projectile | |
class ProjectileTrajectory | |
attr_accessor :grav_acceleration, :launch_angle, :launch_velocity, | |
:initial_height, :distance_traveled | |
def initialize(g=9.81, theta, v, y_not) | |
self.grav_acceleration = g # the gravitational acceleration (usually taken to be 9.81 m/s^2 near the Earth's surface) | |
self.launch_angle = theta # the angle at which the projectile is launched (in degrees) | |
self.launch_velocity = v # the velocity at which the projectile is launched | |
self.initial_height = y_not # the initial height of the projectile | |
end | |
def distance_traveled | |
if initial_height.to_i == 0 && launch_angle.to_i == 45 | |
distance_traveled = ((launch_velocity**2) / grav_acceleration) | |
elsif initial_height.to_i == 0 | |
distance_traveled = ((launch_velocity**2) * Math.sin(2*launch_angle)) / grav_acceleration | |
else | |
distance_traveled = ((launch_velocity*Math.cos(launch_angle) / grav_acceleration) * ((launch_velocity*Math.sin(launch_angle)) + Math.sqrt((launch_velocity*Math.sin(launch_angle))**2 + (2*grav_acceleration*initial_height)))) | |
end | |
end | |
def flight_time | |
distance_traveled / (launch_velocity * Math.cos(launch_angle)) | |
end | |
# the angle at which a projectile must be launched | |
# in order to reach a desired distance, given the launch_velocity | |
def angle_of_reach(desired_distance) | |
0.5 * Math.asin((grav_acceleration*desired_distance) / (launch_velocity**2)) | |
end | |
# the height of the projectile given a horizontal_distance | |
def height(horizontal_distance) | |
initial_height + (horizontal_distance * Math.tan(launch_angle)) - ((grav_acceleration * (horizontal_distance**2)) / (2*((launch_velocity*Math.cos(launch_angle))**2))) | |
end | |
# the velocity of the projectile given a horizontal_distance | |
def velocity(horizontal_distance) | |
Math.sqrt((launch_velocity**2) - (2*grav_acceleration*horizontal_distance*Math.tan(launch_angle)) + ((grav_acceleration*horizontal_distance) / (launch_velocity*Math.cos(launch_angle)))) | |
end | |
# Angle required to hit a target at range x and altitude y when | |
# fired from (0,0) and with launch_velocity | |
def angle_required_to_hit_coordinate(x,y) | |
numerator = (launch_velocity**2) + Math.sqrt((launch_velocity**4) - (grav_acceleration*(grav_acceleration*(x**2)) + 2*y*(launch_velocity**2))) | |
Math.atan(numerator / (grav_acceleration * x)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment