Skip to content

Instantly share code, notes, and snippets.

@quackduck
Created February 9, 2022 02:42
Show Gist options
  • Save quackduck/e8a3dca5e5be24fb891b017db3a3b9aa to your computer and use it in GitHub Desktop.
Save quackduck/e8a3dca5e5be24fb891b017db3a3b9aa to your computer and use it in GitHub Desktop.
public class Projectile {
public double angleOfLaunch; // in radians
public double initialX; // in meters
public double initialVelocity; // in m/s
public boolean useTimeToMaxHeight = false;
private double gravity; // m/s^2
public String description;
public Projectile(String description, double angleOfLaunch, double initialVelocity, double initialX) {
this(description, angleOfLaunch, initialVelocity, initialX, 9.81);
}
public Projectile(String description, double angleOfLaunch, double initialVelocity) {
this(description, angleOfLaunch, initialVelocity, 0);
}
public Projectile(String description, double angleOfLaunch, double initialVelocity, boolean useTimeToMaxHeight) {
this(description, angleOfLaunch, initialVelocity, 0);
this.useTimeToMaxHeight = useTimeToMaxHeight;
}
public Projectile(String description, double angleOfLaunch, double initialVelocity, double initialX, double gravity) {
this.angleOfLaunch = angleOfLaunch;
this.initialX = initialX;
this.initialVelocity = initialVelocity;
this.description = description;
this.gravity = gravity;
}
public void setGravity(double gravity) {
this.gravity = gravity;
}
public void setDescription(String description) {
this.description = description;
}
public double getTimeTaken() {
if (useTimeToMaxHeight) {
return getYComponent() / gravity;
}
return 2 * getYComponent() / gravity;
}
private double getYComponent() {
return initialVelocity * Math.sin(angleOfLaunch);
}
private double getXComponent() {
return initialVelocity * Math.cos(angleOfLaunch);
}
public double getMaxHeight() {
return getYComponent() * getTimeTaken();
}
public double getRange() {
return getXComponent() * getTimeTaken();
}
public static double degreesToRadians(int degrees) {
return Math.PI / 180 * degrees;
}
public static double radiansToDegrees(double radians) {
return radians * 180 / Math.PI;
}
public String toString() {
return "Projectile[" + description + ": angle of launch " + round(radiansToDegrees(angleOfLaunch)) +" degrees, launch velocity " + initialVelocity + " m/s, range " + round(getRange()) + " m, max height "+ round(getMaxHeight()) + " m, time taken " + round(getTimeTaken()) + "s]";
}
private static double round(double d) {
return Math.round(d * 100) / 100.0;
}
public static void main(String[] args) {
// this doesn't take drag into account, so the earth one is a bit less: around 20 meters less
Projectile p = new Projectile("Golf ball on the earth", Projectile.degreesToRadians(16), 59.28);
System.out.println(p); // average golf shot
p.setGravity(1.62);
p.setDescription("Golf ball on the moon");
System.out.println(p); // average golf shot with gravity of the moon
// moon because it has no atmosphere, so the results will be more realistic
System.out.println(new Projectile("Bullet fired on the moon", Projectile.degreesToRadians(10), 200, 0, 1.62));
// a firework configured to explode at the top of its trajectory
System.out.println(new Projectile("Firework", Projectile.degreesToRadians(75), 70, true));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment