Projectile maths challenge!

BlitzMax Forums/BlitzMax Programming/Projectile maths challenge!

Grey Alien(Posted 2007) [#1]
OK then, I'm using this page:

http://en.wikipedia.org/wiki/Trajectory_of_a_projectile

To work out how to fire a projectile from a given point on the screen. I want it to land on a known coordinate (could be at a different height) and to take a FIXED amount of time, say 500 millisecs.

This should be possible right? Say the destination coords were moreorless on the same level and quite far away, you'd have to fire the projectile and a low fast arc to get there in 500ms (forget it bouncing at the destination). If the destination was close, you'd have to fire it up in the air and let it nicely arc down to get there in 500ms.

So basically t (time) is known and d (distance) is known. Also g (gravity is known).

Therefore you can work out the x velocity as d/t. But what should the y velocity be, taking into account g, d and t, and the x velocity?

I'm gonna eat some lunch now and ponder...


Damien Sturdy(Posted 2007) [#2]
Hmmm, Simple task.

I don't have my solution here with me (I used this in a worms clone I made) but i remember it being quite simple.

I'd like to see what others come up with too since I got mine working with a complete fluke :)


Grey Alien(Posted 2007) [#3]
Path of projectile is easy enough, but it's the constraining it to a fixed time-frame that I'm finding harder to sort out.


Grey Alien(Posted 2007) [#4]
well initial vertical velocity for a FLAT ground would be:

v = (t*g)/2

and this works fine but how to work it out if the destination y coord is a bit further down i.e. below the flat ground?


Chris C(Posted 2007) [#5]
If you want a real challenge try calculating shot leading to hit a moving target, I fudged it once but was never really happy with the fudge

assuming a fixed bullet velocity and no air resistance, and that the target is moving on a fixed path and velocity, sounds easy but it aint...


Damien Sturdy(Posted 2007) [#6]
I'll fish out my code later. It takes into consideration the start and end coordinates. You can work out the amount of time it'll take by taking velocity/distance, and then add (Ystartspeed-(gravity/2)) or so (depends on what youre altering the yspeed by). Then just "tween" the movement you apply to the projectile.



I appologise for not actually having the solution here- someone else is bound to give you what you need but if not, when i get back tonight i'll go find it.


Grey Alien(Posted 2007) [#7]
Chris C: Did that quite a few years ago for a shoot'em up in DOS using my A-Level maths book and a quadratic equation.

Cyg: yeah so I want to use a FIXED time that is known. Well anyway, post what you have later, that would be cool thanks :-)


Grey Alien(Posted 2007) [#8]
Lush, cracked it!

dy = 0-(((0.5*gravity*(time*time))+starty-endy)/time)


Works perfectly, looks lush :-)


Damien Sturdy(Posted 2007) [#9]
That looks pretty close to what I have. :)

Grey, my solution would give you fixed time if you "tweened" it right ;)

Anyway yours looks like a slightly more efficient version of mine, so nice one!!!


Grey Alien(Posted 2007) [#10]
time is number of frames btw. then I move by dx and dy each from and natural do dy:+gravity as well. Note:

dx = (end-startx)/time

so dx was always easy.


Schwang(Posted 2008) [#11]
Does anyone have the solution sample code for this? I'd love to see how this works.


Schwang(Posted 2008) [#12]
Seriously, how'd you lot get this working?

Do you use sin and cos to work the x and y coords as per usual?

If so, is dx and dy the velocity which you then add to or multiply with the 'standard' sin and cos calculations?

Can this be used from any point onscreen and fired at any other point, any distance?

How would the formula change if the time requirement was removed?


Warpy(Posted 2008) [#13]
Grey Alien's solution comes from one of the equations of motion,
s = ut + 1/2 * a * t^2

where s is distance travelled, u is the original velocity, a is acceleration per unit time, and t is time elapsed.
We know s, a and t, and we want to find u, so rearranging, we get

u = (s - 1/2 * a * t^2) / t

which is Grey Alien's equation.

Schwang - if the time requirement is removed, there is no longer a unique solution to the equation, which is quite clear if you think about throwing a ball so it hits a point on the ground - you can throw the ball directly at the point, taking the minimal amount of time, or you can throw it high in the air, taking a longer amount of time.