Projectile Trajectory Equation

BlitzMax Forums/BlitzMax Programming/Projectile Trajectory Equation

Arabia(Posted 2009) [#1]
I've googled, searched in here etc. Found heaps of info, and heaps of maths that flies straight over my head :)

Isn't it a simple equation to work out the trajectory of a projectile - i.e. a ball thrown at x degree's and v velocity? Not interested in wind velocity or other factors, just the basic equasion if that makes sens.

The basic equation is what I want, but what I am aiming at doing is working out how a ball/baseball whatever would bounce.

i.e. It's thrown, lands, and then bounces again at less height... much like how a golf ball would bounce (not like I hit a golf ball <- along the ground)

Any hints would be appreciated. Thanks :)


Who was John Galt?(Posted 2009) [#2]
If a ball is thrown at velocity v, at an angle a measured anticlockwise from a line pointing horizontally to the right, its x (horizontal) and y (vertical) speeds are:

xv= v * cos(a)
yv = v* sin(a)

where

xv=x speed
yv=y speed

This is it's speed the instant you throw it.

To get a moving ball, each loop you do:

x=x+xv*dt
y=y+yv*dt

dt is your timestep. Make it a fixed number. You can always experiment to see what value works best.

For gravity, the y speed must change by a fixed amount each frame... this is the ball accelerating down.

yv=yv-g*dt

each frame

g is your gravity. Experiment with values, but start small, say 0.01 to begin.

To make the ball bounce off the bottom of the screen, say y=600,

if (y>=600)
y=600
vy=-c*vy
endif

c is a constant that decides what fraction of the ball's vertical speed remains after the collision. The value should be a fraction less than 1.0

All untested, make sure you use floats for all the variables.


Arabia(Posted 2009) [#3]
Thanks - will give it a go tomorrow night :)

Is there any pre-done code that I can just run (sorry being lazy) and see how it works. I'm just doubting my maths to blitz skills :)


Arabia(Posted 2009) [#4]
I should have said - I thought the path a ball thrown follows is a simple quadratic equation, one that forms a parabola?


Warpy(Posted 2009) [#5]
It is. The code John Galt gave is a discrete timestep model of motion, which is more useful in a game.

The relevant equation of motion is
s = ut + (1/2) * a* (t^2)

where s is the total distance travelled, u is the initial velocity, a is the acceleration, and t is time elapsed.

You can then break that up into x- and y-components, like so:

ux = u*cos(angle)
uy = u*sin(angle)

ax = 0
ay = gravity


Who was John Galt?(Posted 2009) [#6]
Is there any pre-done code that I can just run (sorry being lazy) and see how it works
Probably, but I'm too lazy to search for it. What I gave you is pretty much the code. Just stick it in a loop.


ImaginaryHuman(Posted 2009) [#7]
It's easier to do if you use vectors instead of angles. Then you have an x position, y position, x adder and y adder. The x and y adders are how much you add to the x and y position each frame. And then all you have to do is add (or subtract) a constant value to the y adder to emulate gravity. You shoot your projectile and other than moving in a straight line the gravity will cause it to fall in a parabolic arc, if I understand that correctly.


Nate the Great(Posted 2009) [#8]
here is some very simple code:

I use it for all of my projects that need this sort of thing as well as in my physics engine. The comments make it self explanatory so here it is...

SuperStrict
Graphics 640,480,0,60

Local x:Float = 100	'x and y position of projectile
Local y:Float = 400

Local dx:Float = 5.3	'distance the projectile travels in the x axis in one frame
Local dy:Float = -5	'distance the projectile travels in the y axis in one frame

Local gravity:Float = .1	'gravity strength

WaitKey()	'press a key to begin simulation

While Not KeyDown(key_escape)	'main loop
	
	'add cls here to take off the trail
	
	x = x + dx	'move the x and y coordinates along the x and y axis to their position in the next frame
	y = y + dy
	
	dy = dy + gravity		'this makes the gravity a factor. take this out and the ball moves in a straight line.
	
	DrawOval x-5,y-5,10,10	'draw the projectile
	
	Flip
Wend
End



Arabia(Posted 2009) [#9]
Thanks everyone. I will have a look at it when I get home from work tonight :)


Arabia(Posted 2009) [#10]
Had a little bit of a play around with the info you guys gave me tonight, thanks for the help.

Mainly used the info John Galt posted - thanks for that.

I'll mess around a bit futher in the next couple of days, and post some code for others that might like similar sorts of stuff when I've worked it out 100% (i.e. worked out what I am trying to achieve)

Great help available from many people in here when you need it :D