Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /**
- * Created by Edwin on 11/11/2014.
- */
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- double vinput, anglein, angleinradians, c, m, voutput, vx, vy, ff, fg, ffy;
- double ffx, fnety, fnetx, dx, dy, ay, ax, totaldisplacement, height;
- /*
- * Index of variables, in order:
- * [calder, list them here]
- */
- boolean doISeriouslyHaveToKeepLooping = true;
- totaldisplacement = 0;
- height = 0;
- System.out.println("Enter the magnitude of the launch velocity.");
- vinput = in.nextDouble();
- System.out.println("Enter the launch angle.");
- anglein = in.nextDouble();
- angleinradians = anglein * 0.01745329251; //Multiplied by 2pi/360 to convert to radians.
- c = 0.3; //Most simulations use 0.3. Using the cd = 2Fd/(ρ*v^2*A) equation, I got 0.298.
- m = 0.144; //The baseball was massed in class.
- while (doISeriouslyHaveToKeepLooping == true) {
- vx = vinput * Math.cos(angleinradians);
- vy = vinput * Math.sin(angleinradians);
- ff = vinput * Math.pow(c, 2); // Ff = cv^2
- ffx = ff * Math.cos(angleinradians);
- if (vy > 0) {
- ffy = ff * Math.sin(angleinradians); //this is in the DOWN direction.
- //If the object is travelling upwards, the resistance will be in the down direction.
- } else {
- ffy = -ff * Math.sin(angleinradians);
- //If the object is travelling downwards (ay is negative), air resistance will be applying a force upwards.
- }
- fg = 9.8 * m;
- fnety = ffy + fg; //[down]
- fnetx = ffx; //[bckwd]
- ay = -fnety / m; //[up]
- ax = -fnetx / m; //[fwd]
- dx = vx * 0.01 + 0.5 * ax * Math.pow(0.01, 2);
- dy = vy * 0.01 + 0.5 * ay * Math.pow(0.01, 2);
- totaldisplacement = totaldisplacement + dx;
- height = height + dy;
- vx = vx + ax * 0.01;
- vy = vy + ay * 0.01;
- angleinradians = -Math.atan(vy / vx);
- if (totaldisplacement > 40) {
- doISeriouslyHaveToKeepLooping = false;
- } else {
- doISeriouslyHaveToKeepLooping = true;
- System.out.println(height); //We want height to be 0 when the loop ends.
- //This ensures our trajectory will be as desired.
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement