-1

Projectile tracing path Problem in 2D game:

Assumption:

We make the simplifying assumption that gravity is constant and that there is no wind or drag. The motion of the projectile is given by the following equations:

x = x0 + v0t cos(theta)

y = y0 + v0t sin(theta) + .5 g t^2

where (x0, y0) is the initial position, v0 the initial velocity (magnitude only), theta the angle of discharge, and g the gravitational acceleration. Solving the first equation for v0t and substituting into the second, we get equation [1]:

y = y0 + (x-x0) tan(theta) + .5 (g/v0^2) (x-x0)^2 / cos(theta)^2

Calibration:

Calibration is the process of determining the value of g from an actual projectile. To do that, we shoot a random projectile and capture:

  1. The starting point (x0, y0)
  2. The aim vector (v0, theta)
  3. A random point on the curve (x1, y1)

Substituting the values into the equation [1] and solving for g, we get:

g = (v0^2) * {[2 cos(theta)^2 (y1-y0) / (x1-x0)^2] - [sin(2theta) / (x1-x0)]}

Application:

Now that we have g, we can substitute it back into equation [1], which now can be used to trace the path of a projectile from any starting point and initial velocity. (this is the part I don’t understand)

g=5.89

(x0,y0) starting position = 0,0

Initial velocity = 1-100

Discharge Angle = 0-360

can someone please explain how to get the full plotted path of the parabola for any initial velocity between 1-100, and for any discharge angle between 0-360, if the acceleration due to gravity is 5.89 (in this game), and the starting position is 0,0?

I am a complete newb at math, all of this stuff not in bold lettering I found elsewhere and have been racking my brain over. Please assume I know nothing.

Gallaxhar
  • 976
  • 1
  • 15
  • 28
  • I am not sure I understand your question. At the top, you give your parametric path equations (where x and y are functions of time) which is enough to plot the projectile path. Are you looking to get the equation for the parabola itself, i.e., y as a function of x? – nicholas Jun 07 '12 at 15:41
  • Put all your constants into equation [1] and you're left with a simple `y(x)` quadratic function. So what is your question? (Of course you have to choose *one* initial velocity and *one* discharge angle since each will give you a different path) – Howard Jun 07 '12 at 15:43
  • thanks howard, it seems like you know what you're talking about, could you stick around and help me comprehend what you're saying? I did what you said and got a single answer for y. I don't understand how a single answer for y will plot a parabola, given one initial velocity and one discharge angle? – Gallaxhar Jun 07 '12 at 15:46
  • After putting everyting into [1] there is also the `x` variable left (you didn't specify `x`!), thus you have a function equation `y(x) = A * x^2 + B * x + C` with some constants `A`, `B` and `C`. So you may plot `y(x)` directly which shows you the path. – Howard Jun 07 '12 at 16:06
  • the x variable you are talking about is the 'starting' point, right? – Gallaxhar Jun 07 '12 at 16:08
  • how do I know what A and B is? I assume C is g and A and B are initial velocity/discharge angle but which is which? – Gallaxhar Jun 07 '12 at 16:15
  • "Please assume I know nothing." - no need to say this. Provide a link to the stuff you found, because I'm not sure any of it is correct. – duffymo Jun 07 '12 at 17:22

3 Answers3

2

You only miss the time t in your first set of equations and the spacial/time constrain, plot the first x second of the projectile or until the projectile left the bounding box m. so the pseudocode would be:

  • t=0; x(0)=some coordinate; y(0)=another coordinate
  • LOOP t
  • t=t+1
  • Calculate coordinates {X(t=1), Y(t=1)}
  • Draw the point or the line between this point an the previous one
  • LOOP UNTIL t>time limit OR point {X(t), Y(t)} is outside your bounding box

I hope this help in your endevour

Alen
  • 1,040
  • 1
  • 8
  • 13
  • can you give me a point of this parabola (that obeys the above criteria) that crosses the x line (or horizontal plane) so that I can reverse engineer what you are talking about? – Gallaxhar Jun 07 '12 at 15:54
  • sure, since cross the x axis, implies Y(t)=0 sing your initial set of equations ans the condition Y(0)=0 we can solve t=-2*Vo*Sin(Theta)/g, this implies as g in downward (g<0) to get a t>0, t<0 implies that the projectile was shoot under the axis lines (case with theta<0) – Alen Jun 07 '12 at 16:22
2

Choosing v0 = 10 and theta = 60 degrees we have

tan(theta) = 1.732
cos(theta) = 0.5

and thus equation 1 reads (x0=0, y0=0, g=5.89 were given)

y = 1.732*x - 0.1178*x^2

which can be plotted directly (y vs. x): see here

Note: I corrected the - sign for gravity.

Howard
  • 38,639
  • 9
  • 64
  • 83
  • how did you get tan(theta) = 1.732 and cos(theta) = 0.5? if you plug in 60 into theta such as tan(60) and cos(60) I get different values – Gallaxhar Jun 08 '12 at 01:30
  • EDIT: it seems this is because typing tan(60) into google gives you the wrong answer, but it works with my calculator, thank you very much!! – Gallaxhar Jun 08 '12 at 01:49
0

It's a pretty simple undergraduate physics problem. You start with Newton's law for the x- and y-directions:

force equilibrium in x-direction

force equilibrium in y-direction

with initial conditions:

x-displacement at time zero

x-velocity at time zero

y-displacement at time zero

y-velocity at time zero

Where Q is the initial speed of the projectile and theta is the angle measured counterclockwise from the horizon if the gun points to the right.

So if you integrate each one once w.r.t. time, you get:

x-velocity versus time

y-velocity versus time

Applying initial conditions:

x-velocity constant

y-velocity constant

Integrating a second time gives:

x-displacement versus time

y-displacement versus time

Applying initial conditions again:

x-displacement initial condition

y-displacement initial condition

Making the substitutions gives the final equations that you want for the (u, v) position of the projectile from the moment the gun goes off:

x-position of the projectile as a function of time

y-position of the projectile as a function of time

If you put the origin of the coordinate system at the gun muzzle exit, then the two initial displacements are zero.

Now you have two equations for the (u, v) position of the projectile. You can plug in the initial speed of the projectile and the angle of the gun as measured from the horizon at zero, pointing to the right.

You just start with time at zero, pick an increment in time, and loop for as long a time interval as you like. You plug the current value of time into those equations, evaluate the results, increment the time, and repeat.

Let's imagine that you angle the gun at forty-five degrees counterclockwise from the horizon and fire a projectile at 1000 in/sec. You can step through time and see the projectile move up and to the right in a parabolic path until it reaches its apex and then starts to fall back to the horizon. Eventually your y-displacement will return back to zero and then continue into negative territory, as if you had shot it from the edge of a cliff.

If you want to know how far the projectile goes before it hits the ground, just stop the time loop when the height becomes less than or equal to zero:

time to fall back to earth

Here's a Java implementation for you:

package physics;

/**
 * CannonSimulator simulates shooting a projectile.  Users are responsible for making
 * sure that all constants use consistent units (e.g. meters for length, seconds for
 * time, kg for mass, etc.)
 * @author Michael
 * @since 6/14/12 9:47 PM
 * @link http://stackoverflow.com/questions/10935060/2d-projectile-tracing-path-clarification/11043389#11043389
 */
public class CannonSimulator {

    private double m;
    private double g;
    private double q;
    private double theta;

    public static void main(String[] args) {
        double m = ((args.length > 0) ? Double.valueOf(args[0]) : 1.0); // default mass is 1 kg
        double g = ((args.length > 1) ? Double.valueOf(args[1]) : 9.8); // default gravity is 9.8 m/sec^2
        double q = ((args.length > 2) ? Double.valueOf(args[2]) : 100.0); // default velocity is 100 m/sec
        double theta = ((args.length > 3 ? Double.valueOf(args[3]) : Math.PI/4.0)); // default angle is 45 degrees
        CannonSimulator simulator = new CannonSimulator(m, g, q, theta);
        double t = 0.0;
        double dt = 0.001; // time increment of 0.1 seconds
        while (simulator.v(t) >= 0.0) {
            System.out.println(String.format("time: %10.3f u: %10.3f v: %10.3f", t, simulator.u(t), simulator.v(t)));
            t += dt;
        }
    }

    public CannonSimulator(double m, double g, double q, double theta) {
        if (m <= 0.0) throw new IllegalArgumentException("mass must be greater than zero");
        if (g <= 0.0) throw new IllegalArgumentException("gravity must be greater than zero");
        if (q <= 0.0) throw new IllegalArgumentException("velocity must be greater than zero");

        this.m = m;
        this.g = g;
        this.q = q;
        this.theta = theta;
    }

    public double v(double time) {
        return time*(q*Math.sin(theta) - g*time/m);
    }

    public double u(double time) {
        return time*q*Math.cos(theta);
    }
}

That's how to solve this problem.

duffymo
  • 305,152
  • 44
  • 369
  • 561