[comp.graphics] Graphics -- projectile movement question

c_s245010114@stat.appstate.edu (03/27/91)

This may not be the right place to post this, but this is the only graphics
news feed I have access to.

I'm writing a program in Turbo C++ (not using too many C++ commands, so it's
mostly just Turbo C) that uses some graphics.  Without telling too much what
the program does, I am trying to "launch" a projectile from the bottom of the
screen and have it sort of "arc" upward.  I have found several Calculus
formulas for determining velocity/speed, and I am hoping to make the simulation
fairly true-to-life as far as gravity goes.

Imagine firing a bottle rocket up into the air.  If you hold it at, say, 85
degrees, it arcs up and the velocity slows as it reaches the pinnacle of its
arc.  The only difference is, a bottle rocket accelerates as it goes up since
the "thrusters" are still burning.  With my computer simulation, the launch
speed is as fast as it will ever go, because "gravity" will slow it down.

My question is, what's the best way to devise the arc it will follow, using two
pixels as the "bomb?"  I will generate a random x-position at the bottom of the
screen and a random angle of elevation.  The "speed" will be a fixed value
which I must mess around with.  "Gravity" will also be a problem.

Has anyone written this kind of code?  I need some code that will move the
pixels upward until it reaches speed=0 (at the top of the arc).  Can anyone
offer any advice or directions where I might look?  I'm a poor college student,
so buying books is out (the library here really sucks, too).

					Thanks in advance...
 
  BITNET:  C_S245010114@appstate.bitnet		Scott E. Schnegelberger
INTERNET:  C_S245010114@conrad.appstate.edu	Appalachian State University

"The smallest hole  will eventually drain the largest container, unless it is
 made intentionally for drainage, in which case it will clog." - Dave Grissom

kilian@cray.com (Alan Kilian) (03/27/91)

In article <1991Mar26.125744.623@stat.appstate.edu>,
    c_s245010114@stat.appstate.edu writes:
> 
> My question is, what's the best way to devise the arc it will follow using two
> pixels as the "bomb?" I will generate a random x-position at the bottom of the
> screen and a random angle of elevation.  The "speed" will be a fixed value
> which I must mess around with.  "Gravity" will also be a problem.
> 
>   BITNET:  C_S245010114@appstate.bitnet		Scott E. Schnegelberger
> INTERNET:  C_S245010114@conrad.appstate.edu	Appalachian State University


This is easy do do really fast. Here's how:

x,y,ox and oy are integers, xv,yv,gravity are floats)

x = rand
y = 0             (0,0) is at the lower left of the screen
xv = rand         (X and y velocities in pixels per iteration)
yv = rand         (Better make yv positive)
gravity = .1      (Or something like that)

ox = x		  (Save the initial location)
oy = y

draw (ox,oy)      (draw the initial object)

do {
    ox = x    oy = y      (Save the position of the object)
    x = x + xv            (Move the object in the X direction)
    y = y + yv;           (Move the object up or down)
    yv = yv - gravity     (add the gravity effect)
    draw(x,y)		  (draw the object)
    erase(ox,oy)          (erase the old object)
} while (y > 0)


That's it. It almost allways goes too fast. you'll need to put a delay
loop or have 100 objects flying to get it to slow down.

If you're interested, I'll send more things for bouncing off walls and things
it's really simple.

I'm gone until next Monday April whatever.
-- 
 -Alan Kilian kilian@cray.com                  612.683.5499
  Cray Research, Inc.           | If you were plowing a field what would you
  655 F Lone Oak Drive          | rather use? 2 strong oxen or 1024 chickens?
  Eagan  MN,     55121          | -Seymour Cray (On massivly paralell machines)

nujy@vax5.cit.cornell.edu (03/28/91)

In article <165722.22170@timbuk.cray.com>,
kilian@cray.com (Alan Kilian) writes:
> In article <1991Mar26.125744.623@stat.appstate.edu>,
>     c_s245010114@stat.appstate.edu writes:
>>
>> My question is, what's the best way to devise the arc it will follow using t
>> pixels as the "bomb?" I will generate a random x-position at the bottom of t
>> screen and a random angle of elevation.  The "speed" will be a fixed value
>> which I must mess around with.  "Gravity" will also be a problem.
>>
>>   BITNET:  C_S245010114@appstate.bitnet             Scott E. Schnegelberger
>> INTERNET:  C_S245010114@conrad.appstate.edu Appalachian State University
>
>
> This is easy do do really fast. Here's how:
>
  < psuedocode deleted >
>
> --
>  -Alan Kilian kilian@cray.com                  612.683.5499
>   Cray Research, Inc.           | If you were plowing a field what would
>   655 F Lone Oak Drive          | rather use? 2 strong oxen or 1024 chicke`ns
>   Eagan  MN,     55121          | -Seymour Cray (On massivly paralell machine


I thought you might be interested in more specific initial conditions.
Using Alan's program set:
  xv=cos(angle)*speed
  yv=sin(angle)*speed

Also, in case you're interested, you can make the "bomb" go through two
points on the 'ground' (y=0) and reach a particular height (y=hmax) with
the following equations for yv and gravity.  x0 is the initial x coordinate,
x2 is the final x coordinate, and hmax is the maximum altitude (which
occurs at (x0+x2)/2.  By the way, you can use any xv but it looks best
when it is 1 because the "bomb" moves exactly one pixel horizontally at
each step.

Given:  float xv, x0, x2, hmax

h       = -4.0*hmax / ((x0-x2) * (x0-x2))  /* only used for gravity and yv */
gravity = -2.0*xv*xv*h
yv      = ((x0-x2)*xv + xv*xv) * h
x  = x0
y  = 0.0

Of course, you can optimize this for xv=1.0

  Chris Schoeneman                  |  "I was neat, clean, shaved, and sober,
   nujy@vax5.cit.cornell.edu        |    and I didn't care who knew it."
                                    |               - Raymond Chandler

jwn727@unhd.unh.edu (Jason W Nyberg) (04/02/91)

Here is a description of a phisical model for projectile motion that I
have used for this kind of thing,  as well as simulating orbits, atomic-
particle interaction, and other fun stuff with little bits flying around.
I'd like to say its mine, but it's pretty basic so that would be a tad
presumptuous. (Though Macintosh would have no problem with it! :-)*0.5 )

each particle (in your case 1) has a coordinate, (x,y,z...) a velocity 
vector (vx,vy,vz,...), and an updated coordinate (nx,ny,nz...)

here's a little algorithm:

while (some_condition) {
	for_each_particle {          //  Part A
		display_particle();      // do your own...

	for_each_particle {          //  Part B
		nx=x+vx; ny=y+vy; nz=z+vz; ... // update coordinate
		}

	for_each_particle {          //  Part C
		erase_particle();

	for_each_particle {          //  Part D
		x=nx; y=ny; z=nz;...

	vy=vy-G;                  // Part E:  G=gravity constant (fudgeable)
	}

Initialy (before the while) you set your coordinates and velocity vectors
at the position and velocity you want, and enter the loop.

You only need part D if your particles interact somehow (like a bunch of
planets floating around) and yours don't (doesn't!) so you can get rid
of nx,ny,nz, part D, and modify part B so that x+=vx, etc...

If you want to do more funky stuff with the particles, Modify part E so that
the velocity gets modified in other ways...

EX:
The 1/d^2 gravity law: Assign each particle a mass.  for each particle, 
use every other particle's mass and coordinate to modify the selected
particle's velocity (here you need the nx,ny,nz). Believe it or not, I
wrote a (very!) simple version of this for my Sinclair ZX-81, with 1k mem.
(my first neat program ever, on my first computer, in 1984-85.)

I've got a water drop simulator using this stuff, running on X, if anyone 
is interested I'll mail it. (send me mail) (not very optimized, in C++ kindof)
It lets drops of water hang from the "ceiling" and drop and splash and bounce
off walls and all kinds of fun stuff.  Its the drop's molecules that are 
simulated.

Jason Nyberg                jwn727@unhd.unh.edu
                         or jwn@vandam.cs.unh.edu