Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-27-2009, 02:39 AM
Member
 
Join Date: Oct 2009
Posts: 11
Rep Power: 0
shuks is on a distinguished road
Default 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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-27-2009, 02:52 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
Default
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;
}
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-27-2009, 03:05 AM
Member
 
Join Date: Oct 2009
Posts: 11
Rep Power: 0
shuks is on a distinguished road
Default
Ahh, that makes alot of sense! I will try that as soon as I get home. Thanks for the advice.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-27-2009, 03:07 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
Default
You're welcome, and much luck!
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trajectory JDCAce Advanced Java 5 10-13-2008 07:02 AM


All times are GMT +2. The time now is 06:06 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org