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. }
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

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

Back To Top