简体   繁体   中英

Calculate coordinates (x,y) of a ball at any given time

I'm making a simulation of a ball in the a 2D space, drawing its path.

I'm using g.fillOval(x,y,height,width); to draw the ball

The ball is affected by three forces: Gravity, Wind and shot-power, it also has a starting angle.

This picture demonstrates what i mean

在此处输入图像描述

Knowing that the ball starts from x=11 y=360

i want to change the value of x and y according to ball's position at any time given and under the mentioned forces..

an equation that would do that should be something like

x += Angle Power Gravity Wind
y -= Angle Power Gravity Wind

The equation is well known if you've studied physics. It's Newton's law in 2D:

sum(forces) = mass * acceleration

This is really three equations, since force, acceleration, velocity, and displacement are all vector quantities.

You'll simplify by assuming 2 dimensions.

Gravity will always act in the negative y direction in your simplified view; no x-component.

Wind can vary in direction, so you'll need to say how it evolves with time or assume that it's constant for the period you're studying.

Mass of the ball is fixed. It's big enough where quantum effects can be ignored.

Velocities are much smaller than the speed of light, so relativistic effects can be ignored.

You'll need to know a bit about calculus - do you? These are differential equations in time. You'll start with initial conditions and either integrate in closed form or step the equations forward numerically in time steps to see how the motion evolves.

Let's make it easy: constant wind in the negative x-direction.

I don't know what "shot power" is, but I'm betting you'll know the initial velocity of the ball as it's shot out of the cannon.

So here are the variables you'll need:

ax = acceleration of the ball in the x-direction
ay = acceleration of the ball in the y-direction
fx = wind resistance force
fy = gravity
vx = velocity of the ball in the x-direction
vy = velocity of the ball in the y-direction
ux = displacement of the ball in the x-direction
uy = displacement of the ball in the y-direction
t  = time
m  = mass of the ball

And here are the equations that relate them:

ax = -fx/m
ay = -fy/m
ax = dvx/dt = first derivative of vx w.r.t. time
ay = dvy/dt = first derivative of vy w.r.t. time
vx = dux/dt = first derivative of ux w.r.t. time
vy = duy/dt = first derivative of uy w.r.t. time

You'll need initial conditions:

ux = 0 at time = 0
uy = 0 at time = 0
vx = vx0 at time = 0; it's greater than zero if you shoot to the right
vy = vy0 at time = 0; it's greater than zero if you aim the cannon up 

It's easy to solve if you substitute:

d^2(ux)/dt^2 = -fx/m
d^2(uy)/dt^2 = -fy/m

Integrate once wrt time:

dux/dt = -(fx/m)*t + c0
duy/dt = -(fy/m)*t + c1

Integrate again to get the position of the ball as a function of time:

ux = -(fx/m)*t^2/2 + c0*t + c3
uy = -(fy/m)*t^2/2 + c1*t + c4

Your initial conditions help with the constants:

ux(0) = c3 = 0
uy(0) = c4 = 0

and

vx(0) = c0 = vx0
vy(0) = c1 = vy0

So here are the final equations for the position of the ball of mass m after you shoot it with initial velocity (vx0, vx0) into a head wind fx and gravity fy:

ux = (vx0-(fx/m)*t/2)*t
uy = (vy0-(fy/m)*t/2)*t

You can shift the answers relative to any starting point (ux0, uy0):

ux = ux0 + (vx0-(fx/m)*t/2)*t
uy = uy0 + (vy0-(fy/m)*t/2)*t

If you shoot the cannon at an angle theta from the horizon, and the ball has initial speed v, then the initial conditions become:

(vx0, vy0) = (v*cos(theta), v*sin(theta))

If the wind is calm, this solution still works: just set fx = 0.

This solution assumes that the head wind is constant and isn't a function of the velocity of the ball. If it is, the equations are nonlinear and you have to solve them numerically.

As noted in the comments, you can make the head wind term more complex, too.

You can make these equations (and their solution) more complex by including more physics effects:

http://en.wikipedia.org/wiki/External_ballistics

Here you go, since you're talking java, here's a method that would do pretty much what you want, what it does is it calculates the whole path of a projectile and motion and shows the (x,y) of the ball at each step, so you can store those information in an array and use them to calculate the path and draw it!

// constant for Earth's gravity acceleration in meters/second^2
public static final double ACCELERATION = -9.81;

// returns the displacement for a body under acceleration
public static double displacement(double v0, double t, double a) {
    return v0 * t + 0.5 * a * t * t;
}

// prints a table showing the trajectory of an object given
// its initial velocity v and angle and number of steps
public static void table(double v0, double angle, int steps) {
    double v0x = v0 * Math.cos(Math.toRadians(angle));
    double v0y = v0 * Math.sin(Math.toRadians(angle));
    double totalTime = -2.0 * v0y / ACCELERATION;
    double dt = totalTime / steps;

    System.out.println("    step       x       y    time");
    for (int i = 0; i <= steps; i++) {
        double time = i * dt;
        double x = i * v0x * dt;
        double y = displacement(v0y, time, ACCELERATION);
        System.out.printf("%8d%8.2f%8.2f%8.2f\n", i, x, y, time);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM