Geekpedia Programming Tutorials






Get Velocity And Position Of A Projectile

Use Java, physics and simple math to calculate the position of a projectile at a given time and its velocity.

On Thursday, August 7th 2008 at 04:51 PM
By Andrew Pociu (View Profile)
-----   (Rated 0 with 0 votes)
Contextual Ads
More Java Resources
Advertisement
  1. import java.awt.Point;
  2.  
  3. public class Projectile
  4. {
  5.         double angle;
  6.         double initialVelocity;
  7.         Point initialPoint;
  8.  
  9.         public Projectile(Point initPoint, double angle, double initialVelocity)
  10.         {
  11.                 this.angle = angle;
  12.                 this.initialVelocity = initialVelocity;
  13.                 this.initialPoint = initPoint;
  14.         }
  15.        
  16.         private double getHorizontalVelocity(double angle, double velocity)
  17.         {
  18.                 return Math.cos(Math.toRadians(angle)) * velocity;
  19.         }
  20.        
  21.         private double getVerticalVelocity(double angle, double velocity)
  22.         {
  23.                 return Math.sin(Math.toRadians(angle)) * velocity;
  24.         }
  25.        
  26.         public Point getPositionAt(double time)
  27.         {
  28.                 double x = getHorizontalVelocity(angle, initialVelocity) * time;
  29.                 double y = getVerticalVelocity(angle, initialVelocity) * time - 0.5 * (9.8 * time * time);
  30.                 Point currPoint;
  31.                 currPoint = new Point(
  32.                                 (int)Math.round(x) + initialPoint.x,
  33.                                 (int)Math.round(y) + initialPoint.y);
  34.                 return currPoint;
  35.         }
  36.        
  37.         public static void main(String args[])
  38.         {
  39.                 Projectile ball = new Projectile(new Point(0,0), 35.0, 40.0);
  40.                 System.out.println(ball.getPositionAt(0));
  41.                 System.out.println(ball.getPositionAt(2));
  42.                 System.out.println(ball.getPositionAt(5));
  43.         }
  44. }
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this code snippet
Comment Current Comments
There are no comments.

Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs Java Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons