Java Reference
In-Depth Information
V 0
V t
V 0y
V ty
V 0x
V tx
Figure 3.1
Initial and Final Velocity of Projectile
Look more closely at this line of code:
double angle = Math.toRadians(console.nextDouble());
Some beginners would write this as two separate steps:
double angleInDegrees = console.nextDouble();
double angle = Math.toRadians(angleInDegrees);
Both approaches work and are reasonable, but keep in mind that you don't need to
divide this operation into two separate steps. You can write it in the more compact
form as a single line of code.
Once we have obtained these values from the user, we are ready to begin the compu-
tations for the trajectory table. The x / y -position of the projectile at each time increment
is determined by its velocity in each dimension and by the acceleration on the projectile
due to gravity. Figure 3.1 shows the projectile's initial velocity v 0 and angle
Θ
just as it
is thrown and final velocity v t just as it hits the ground.
We need to compute the x component of the velocity versus the y component of
the velocity. From physics, we know that these can be computed as follows:
double xVelocity = velocity * Math.cos(angle);
double yVelocity = velocity * Math.sin(angle);
Because we are ignoring the possibility of air resistance, the x -velocity will not
change. The y -velocity, however, is subject to the pull of gravity. Physics tells us that
on the surface of the Earth, acceleration due to gravity is approximately 9.81
meters/second 2 . This is an appropriate value to define as a class constant:
public static final double ACCELERATION = -9.81;
Notice that we define gravity acceleration as a negative number because it
decreases the y -velocity of an object (pulling it down as opposed to pushing it away).
Our goal is to display x , y , and elapsed time as the object goes up and comes back
down again. The y -velocity decreases steadily until it becomes 0. From physics, we
Search WWH ::




Custom Search