package uwstout.courses.cs144.labs.lab5;
|
|
|
|
/**
|
|
* Represents a projectile being launched into the air at a specific velocity,
|
|
* and at a specific angle. Allows the user to calculate the distance traveled
|
|
* by the projectile at a particular time, as well as the maximum distance.
|
|
*
|
|
* @author Brett Bender
|
|
* @version 2022.10.26
|
|
*/
|
|
public class Projectile {
|
|
|
|
/**
|
|
* The acceleration due to gravity
|
|
*/
|
|
private static final double G = 9.8;
|
|
private static final int DEFAULT_ANGLE = 45;
|
|
|
|
private double velocity;
|
|
private int angle;
|
|
|
|
/**
|
|
* Constructs a new Projectile with a given velocity and angle.
|
|
*
|
|
* @param nvelocity The velocity of the projectile
|
|
* @param nangle The angle of the projectile
|
|
*/
|
|
public Projectile(double nvelocity, int nangle) {
|
|
velocity = nvelocity;
|
|
angle = nangle;
|
|
}
|
|
|
|
/**
|
|
* Constructs a new Projectile with a given velocity. Defaults angle to
|
|
* 45deg.
|
|
*
|
|
* @param nvelocity The velocity of the projectile
|
|
*/
|
|
public Projectile(double nvelocity) {
|
|
velocity = nvelocity;
|
|
angle = DEFAULT_ANGLE;
|
|
}
|
|
|
|
/**
|
|
* Constructs a new Projectile that is a copy of the specified Projectile.
|
|
*
|
|
* @param other The projectile that is being copied
|
|
*/
|
|
public Projectile(Projectile other) {
|
|
velocity = other.velocity;
|
|
angle = other.angle;
|
|
}
|
|
|
|
/**
|
|
* Calculates the distance traveled at a specified time.
|
|
*
|
|
* @param time The time (in seconds)
|
|
* @return The distance traveled
|
|
*/
|
|
public double getDistance(double time) {
|
|
return velocity * time * Math.cos(Math.toRadians(angle));
|
|
}
|
|
|
|
/**
|
|
* Calculates the maximum distance the projectile may travel.
|
|
*
|
|
* @return The maximum distance
|
|
*/
|
|
public double getMaximumDistance() {
|
|
return (velocity * velocity / G) * Math.sin(2 * Math.toRadians(angle));
|
|
}
|
|
|
|
/**
|
|
* Get the velocity of the projectile.
|
|
*
|
|
* @return The velocity of the projectile
|
|
*/
|
|
public double getVelocity() {
|
|
return velocity;
|
|
}
|
|
|
|
/**
|
|
* Get the angle of the projectile.
|
|
*
|
|
* @return The angle of the projectile
|
|
*/
|
|
public int getAngle() {
|
|
return angle;
|
|
}
|
|
|
|
/**
|
|
* Set the velocity of the projectile
|
|
*
|
|
* @param nvelocity The new velocity
|
|
*/
|
|
public void setVelocity(double nvelocity) {
|
|
velocity = nvelocity;
|
|
}
|
|
|
|
/**
|
|
* Set the angle of the projectile
|
|
*
|
|
* @param nangle The new angle
|
|
*/
|
|
public void setAngle(int nangle) {
|
|
angle = nangle;
|
|
}
|
|
|
|
}
|