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
import java.awt.Point;
public class Projectile
{
double angle;
double initialVelocity;
public Projectile (Point initPoint, double angle, double initialVelocity )
{
this.angle = angle;
this.initialVelocity = initialVelocity;
this.initialPoint = initPoint;
}
private double getHorizontalVelocity(double angle, double velocity)
{
return Math. cos(Math. toRadians(angle )) * velocity;
}
private double getVerticalVelocity(double angle, double velocity)
{
return Math. sin(Math. toRadians(angle )) * velocity;
}
public Point getPositionAt (double time )
{
double x = getHorizontalVelocity(angle, initialVelocity) * time;
double y = getVerticalVelocity(angle, initialVelocity) * time - 0.5 * (9.8 * time * time);
(int)Math. round(x ) + initialPoint. x,
(int)Math. round(y ) + initialPoint. y);
return currPoint;
}
public static void main (String args [])
{
Projectile ball = new Projectile (new Point(0, 0), 35.0, 40.0);
System. out. println(ball. getPositionAt(0));
System. out. println(ball. getPositionAt(2));
System. out. println(ball. getPositionAt(5));
}
}
|
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|
Rate this code snippet
Current Comments
There are no comments.
|
Related Source Code
There is no related code.
Related Tutorials
There are no related tutorials.
Java Job Search
|