/** * @author Gaspar Guerra * Rocket.java * Project Assignment5 */ public class Rocket { //Constant to convert from Feet to Meters public final static double FEET_TO_METERS = 0.3048; //Constant, Gravitational Force, in ft/s^2 public final static double G = 32.152231; //Initial Velocity of Rocket private double v0; //Angle of launch of Rocket private double angle; public Rocket( double init, double ang ){ //Initialize variables with given values v0 = init; angle = ang; } /* * Converts the given degrees to radians */ public double degToRad( double deg ){ return ( deg * Math.PI ) / 180.0; } /* * Returns the speed in Meters */ public double speedInMeters( ){ return v0 * FEET_TO_METERS; } /* * Returns the horizontal component * of the initial velocity */ public double horizontalComponent( ){ return v0 * Math.cos( degToRad( angle ) ); } /* * Returns the vertical component * of the initial velocity */ public double verticalComponent( ){ return v0 * Math.sin( degToRad( angle ) ); } /* * Returns the state of this Rocket, at the given time */ public CurrentState returnState( double time ){ if( time <= 0.0 ){ //If time is negative or zero, Rocket has not launched return new CurrentState( 0, 0, 0, 0 ); } double vertical = verticalComponent( ) * time - 0.5 * G * time * time; if( vertical < 0.0 ){ //If the Rocket's Y Position in the plane is negative, the //Rocket has fallen to the ground. We calculate the time //When the Rocket fell, and return the state at that time time = 2 * ( verticalComponent( ) / G ); return new CurrentState( horizontalComponent( ) * time, 0, 0, 0 ); } //At this point, the Rocket has been determined to still be in the air //We return its position, and Velocity, as its State return new CurrentState( horizontalComponent( ) * time, vertical, horizontalComponent( ), verticalComponent( ) - G * time ); } }