Hi...
I am writing a program that simulates a projectile's trajectory. I don't need wind resistance or anything fancy. The only force acting upon the projectile will be gravity.
I have spent a long time searching the web for information about how to do this and I have found some helpful information but I do not fully understand how to implement it.
Given a user defined angle and velocity, this program should be able to calculate the arc that is created when when the projectile is fired (something like a parabola).
The projectile will have an X and Y coordinate. From what I have learned so far it will also have a vertical and horizontal speed (ySpeed , xSpeed)...
Given a velocity (V) and an angle (A) the vertical and horizontal speeds are calculated using the following formula...
xSpeed = V * cos(A);
ySpeed = V * sin(A);
We will call the X and Y coordinates of the projectile xPos, and yPos ("pos" for position). The "time" variable is a timestep for each iteration of the loop. To make a moving projectile we use a loop that looks something like this...
xPos = xPos + xSpeed * time;
yPos = yPos + ySpeed * time;
So far this seems pretty straight forward. However, we need to add a gravitational pull on the projectile which is where i get lost. From what I understand gravity will affect the ySpeed of the projecile and must be changed by a fixed amount with each iteration of the loop. The formula I have is...
ySpeed = ySpeed - gravity * time;
Im confused on how to implement the gravity on the projectile. Can someone please enlighten me. The loop I have (which Im pretty sure is wrong) looks like this...
{
xSpeed = V * cos(A);
ySpeed = V * sin(A);
ySpeed = ySpeed - gravity * time;
xPos = xPos + xSpeed * time;
yPos = yPos + ySpeed * time;
}
If I have made any mistakes please let me know and if someone could provide me with a code example It would be greatly appreciated. Thanks.