Qbasicnews.com

Full Version: Projectile trajectory calculation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Does anyone know how I can calculate the trajectory of
a projectile so I get the x,y coordinates
after s seconds if I shoot the projectile
with the angle a, velocity v, and gravity g?
Maybe I should take a look at gorillas...

But if anyone feels like answering, please do! Smile
you ever take a physics class? i havent.
Aliright, let's see if I remember my kinetic physics course...

the way to do this is to extract two velocities (horizontal and vertical) from the g and v values. You use trigonometry to do this.

given a right triangle:

Code:
v    _  .- `|
         _ .- `
a ...:....................|   y
               x
if you can tell from my crude drawing, a is the angle at the bottom left, x the length of the bottom, y the length of the right side, and v the angle of the diagonal side. imagine v, x, and y to be forces acting away from the point at angle a...

you can determine the amount of diagonal velocity(v) that is going into both horizontal (x) and vertical (y) directions using these formulas:

SIN(a) = opposite/hypoteneuse = y/v
COS(a) = adjacent/hypoteneuse = x/v

therefore,

y = v * sin(a), and x = v * cos(a)

remember, the gravity force (g) only affects the VERTICAL velocity (y). x should remain unchanged unless you want to take air friction into consideration, which I would not.

Take a look at the code below and see if you follow it.

Code:
'*** set up graphics
CLS
SCREEN 13

'*** a = angle in degrees
'*** then convert it from degrees to radians
INPUT "Enter angle (0-90): ", a
a = a * (3.14159265# / 180)

'*** mp = meters/pixel
'*** v = velocity in direction a in meters/second
'*** convert v to pixels / second
INPUT "Enter pixels/meter [try 10]: ", pm
INPUT "Enter velocity: ", v
v = v / pm

'*** determine starting velocities
'*** x = horizontal velocity
'*** y = vertical velocity
x = v * COS(a)
y = v * SIN(a)

'*** set starting location
'*** xloc = starting horizontal position
'*** yloc = starting vertical position
xloc = 0
yloc = 0

'*** set gravity in meters / second
'*** convert to pixels/second
g = -9.8 'this is earth's gravity
g = g / pm

CLS
PRINT "a = "; a
PRINT "x = "; x
PRINT "y = "; y
PRINT "g = "; g
PRINT "xloc = "; xloc
PRINT "yloc = "; yloc
DO: LOOP UNTIL INKEY$ <> ""

'*** determine loops/second
'*** ls = loops/second
t = TIMER
ls = 0
DO
     ls = ls + 1
LOOP UNTIL TIMER > t + 1

CLS
DO
          '*** move object
          y = y + g / ls
          yloc = yloc + y / ls
          xloc = xloc + x / ls

          '*** draw object (we have to invert y since it's upside down on screen)
          PSET (xloc, 199 - yloc), 15
LOOP UNTIL yloc < 0

DO: LOOP UNTIL INKEY$ <> ""
SYSTEM

Let me know if you have any questions.

*peace*

Meg.

*edit: had to fix the triangle diagram multiple times... :oops:
I notice that you're looking for the trajectory of the projectile after seconds (s). I didn't read that in my first lecture Smile

Okay, so this is easy after all that.

You are given a, g, v, and s.

you can change a and v into x and y (see Lecture 1) by using these formulas:

SIN(a)=y/v
COS(a)=x/v

therefore:

y = v*SIN(a)
x = v*COS(a)

now, gravity doesn't affect horizonal motion at all, so x will stay unchanged.

you've got an object that starts out moving y m/s UP, and x m/s ACROSS. Every second that passes, y decreases by g m/s, right? 'Cause gravity is acting down on it.

therefore, after s seconds:

xnew = x (unchanged)
ynew = y + g*s (original value plus the effect of gravity/second after s seconds)

NOTES:

* x, y, v, xnew, ynew, and g are NOT LOCATIONS of the object. They are velocities (x, y, xnew, ynew) and accelerations (g). Velocities are in meters/second and accelerations are in (meters/second)/second. So when you get xnew and ynew, they don't tell you where the projectile IS, just the direction and speed that it's currently moving at. To determine the location of the object, you'd need to use another formula. Let me know if this is what you want.

* keep in mind that g should be a NEGATIVE number, so that it's being subtracted from y.

* note that y will begin positive, meaning the object is rising in height (provided you entered an angle that throws the projectile UPwards, i.e. -10 will throw it downwards). The object will reach a peak, and then start falling, as y becomes negative.

* finally, it's important to note that the SIN and COS functions in QBasic take their parameters in RADIANS, not DEGREES. You have to input a (probably in degrees) and then convert it to radians before passing it to the SIN and COS functions. Since 360 degrees (once around the unit circle) equals 2 PI radians (once around the unit circle), the conversion goes like this:

360 degrees = (2 * PI) radians, once around the unit circle
180 degrees = PI radians, halfway around the unit circle
(180 degrees/PI radians) = 1

so to convert from angle to radians, multiply by PI/180
to convert from radians to angle, multiply by 180/PI

You can see where I used this formula in the code above, right underneath the input of angle a.

Let me know if this all makes sense.

*peace*

Meg.
I read your first post earlier today and understood how to do.
The cos/sin/gravity was as I guessed myself, but the thing
was to add the velocity (so if you shoot fast then the projectile
won't fall down as fast...)

Well I got it now. Thanks!

(It is for my first basic game for my ti83+SE)