views:

291

answers:

4

What I'm trying to do is simulate projectile motion in Visual Basic using Visual Studio. Essentially, something like this but without air resistance or mass.

Anyhow, the way I'm doing it is using a PictureBox and drawing the image in (of, say, a circle) every so often with a timer of interval 1 with its x and y properties being variables calculated by the formulas.

I want to know what formulas I can use to assign to x and y so that I get a movement like the one on the example. I've tried a bunch of stuff, with help from wikipedia and other sites, but can't seem to pull it off. Thanks!

A: 

Set X = the total time since firing

Y = -0.5 * A * X*X + V * X + InitialHeight

A = the desired acceleration. Earth's acceleration (i.e. gravity) is ca. 9.8 meters/second squared.

V = your initial velocity in the up direction

InitialHeight is zero in your example, but you could start out higher if you want to.

EDIT:

One other post states

x = V * cos(a) * t + g * t * t / 2

y = V * sin(a) * t - g * t * t / 2

This is essentially the same formula, but leaves out the optional + InitialHeight parameter.

My statement "velocity in the up direction" is the same as V (total velocity) * sin(a) in his formula.

I equated X to t (the time) for simplicity, but x = V * cos(a) lets you map a time to a physical distance. In my simplification, I assumed you don't really care about exactly what units are on the X axis (which may or may not be a valid assumption).

Eric J.
But what if I don't have a time constant?
Joel M.
At the end of the day, you are graphing the Y axis as a function of the X axis. As you increment X, you calculate what Y should be. Your options are either just to directly do an X loop (increment X, calculate Y, and either plot (X,Y) or stop if Y < 0) or do something with time (loop looking at actual time and convert that into X units by some ratio that makes sense to you). If you just want to get the graph, go with the first option and ignore time altogether. If you want to model the real world somehow and have "hang time" be relevant, count time and compute X=speed*time, Y using formula
Eric J.
+1  A: 

Assume a launch speed of V, launch angle of a radians, gravity g, time t:

x = V * cos(a) * t + g * t * t / 2
y = V * sin(a) * t - g * t * t / 2

Background info is here.

Hans Passant
Thanks for your answer. But what if I don't have a time constant?
Joel M.
Sure you do. It is not a constant, time is incremented by your Timer.
Hans Passant
+3  A: 

Here's the absolute simplest example to minimize precious clock cycles:

// Initialization
x = 0; // x position
y = 0; // y position
xi = 0.1; // x incremental value (velocity)
yi = 0.1; // y incremental value (velocity)

// Rendering loop
x += xi;
y += yi;
yi -= 0.01; // decrement by arbitrary value to simulate gravity

The idea is to use incremental variables xi and yi and perform all of your forces on them. You can tweak the numbers to get the effect you're looking for. If you wanted to take it a step further you can easily apply friction or air resistance by using multipliers to adjust those incremental variables at every iteration.

This is the basic method I've used many times in particle engines and such. Since you're not making any calls to any of the trig functions it has the benefit of being extremely fast and it scales well to large particle engines.

UPDATE:

There's an example of this method using numbers from the formula for earth's gravity here : http://www.forums.evilmana.com/game-programming-theory/euler-vs-verlet-vs-rk4-physics/?wap2

It's still simple code even with the -9.8 value and timestep variable. My code is just optimized to the absolute simplest it can be.

Steve Wortham
xi, and yi here typically called "velocity", and this is indeed the stock answer to this question. It's Euler integration (http://en.wikipedia.org/wiki/Euler_integration) for acceleration due to gravity.
munificent
@munificent. I didn't realize I was using Euler integration. It's funny how many times I've written algorithms only to find out afterwards what they're called. I've used this algorithm so many times. So it's also good to get affirmation from a real game developer. Thanks man.
Steve Wortham
@steve: I actually stumbled onto this same algorithm years ago when I was a kid programming in QuickBASIC. I didn't know what it was or why it worked until about a decade later when I started learning a little calculus. And I didn't know it was Euler integration until a couple of years ago.
munificent
@munificent - That's funny... I also first wrote this in QuickBASIC as a kid. It then progressed into elastic attraction and N-body simulations (also a term I only learned recently). I eventually got into OpenGL and I wanted to be a game programmer. I built a site to show off my work at gldomain.com, which is now horribly outdated. But ultimately I got sucked into the web world instead. Anyway, I hope this helps the OP.
Steve Wortham
A: 

You could look at the QuickBasic source code for the "gorillas" game first included with MS-DOS 5. It took account of gravity to animate the motion of a thrown banana in exactly the way you describe.

As far as I know the maths part of the Basic language hasn't changed since those days.

MarkJ