# Projectile Trajectory

Printable View

• 11-27-2009, 02:39 AM
shuks
Projectile Trajectory
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.
• 11-27-2009, 02:52 AM
Fubarable
So xSpeed and xPos are easy to deal with since xSpeed is a constant and doesn't change with time.

ySpeed however changes continuously with time under the influence of gravity, and this will have to be dealt with. First your equation:

ySpeed = V * sin(A)

This only defines y's starting speed, that's it, and so you can only use this equation at the beginning of your calculations. Perhaps you should think of V * sin(A) as ySpeed0. So to do this correctly, you'd have this equation before the loop. something like so:

Code:

```// pseudocode double xSpeed = v * cos(angle) double ySpeed = v * sin(angle) loop incrementing time {   xPos = xPos + xSpeed * time;   yPos = yPos + ySpeed * time;   ySpeed = ySpeed - gravity * time; }```
• 11-27-2009, 03:05 AM
shuks
Ahh, that makes alot of sense! I will try that as soon as I get home. Thanks for the advice.
• 11-27-2009, 03:07 AM
Fubarable
You're welcome, and much luck!