Get Velocity And Position Of A Projectile

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.
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.040.0);
40.                 System.out.println(ball.getPositionAt(0));
41.                 System.out.println(ball.getPositionAt(2));
42.                 System.out.println(ball.getPositionAt(5));
43.         }
44. }

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top