Calculating velocity and angle to hit target

The closest i’ve found so far is the equation on this page, but for some reason i can’t get it to translate properly to the physics in Corona. I’m not sure if i need to convert gravity or the points or something.

http://nial.me/2012/09/calculating-bullet-trajectories/

Did you see my comments further down on that tutorial page? It got a bit butchered (didn’t see any formatting in the reply part… I could fix it up here or whatever, if necessary), but you can get an angle (two, actually) out of that. Speed would be solving for “v”, instead (given time, angle, etc.)

Joshua,

the trajectory is described by a second order equation of the form y=ax^2+bx+c (1)

where a,b and c are constants that depend on position and velocity. that means you have to solve 3 equations for 3 unknowns to find a, b and c. example: say we want the object to start at point {100,200}, end at point {300,150} and also pass through point {200,50}. substituting these values for x and y into (1) we get
10000*a+100*b+c=200
90000*a+300*b+c=150

40000*a+200*b+c=50

solving these 3 simultaneous equations for a,b,c we get a=0.0125, b=-5.25, c=600

google solving simultaneous linear equations to solve them online

create an x increment

[lua]local timeSteps=100
local dx=(300-100)/timeSteps [/lua]
and use a timer to adjust x and y

[lua]–inside the timer function
object.x=object.x+dx
object.y=0.0125*math.pow(object.x,2)+(-5.25)*object.x+600[/lua]

if instead of an end point condition we had an initial upward velocity of 50, we’d replace the second equation above by 200*a+b=-50 (2ax+b is the velocity at any point) and solve the equations again for a,b,c
 

 

@StarCrunch Yeah i saw your comments, but it’s a bit hard to decipher how that’s supposed to work. I want to try and get a version working with the example on that page so everyone can see how it would work easier.

Pretty sure I developed some code for this back when I was researching the projectile trajectory code on the exchange. I’ll try to dig it out later, but here’s some references for reading:

http://answers.unity3d.com/questions/49195/trajectory-of-a-projectile-formula-does-anyone-kno.html

http://answers.unity3d.com/questions/242624/mathematicians-wanted-trajectory-target-hitting-qu.html

http://hyperphysics.phy-astr.gsu.edu/hbase/traj.html#tra7

Sorry I can’t be any more help right now, but the day job calls. I’ll see if I can find my working code later.

@horacebury Thanks that’d be great. In the mean time i’ll look over the links you posted, but i believe i went through these before and still had trouble getting it to work properly.

In case it’s any help, I’ll see if I can elaborate on what I posted, in particular the stuff in this chunk

 local dx, dy, y = target[1] - point[1], target[2] - point[2], target[3] - point[3] local x2 = dx \* dx + dy \* dy local a1, a2, t1, t2 -- If the target is above or below, do special vertical case. if x2 \< 1e-5 then a1, a2, t1, t2 = VerticalLaunch(y, gravity, v0, get\_times) -- x(t) = v0 \* cos(alpha) \* t -- y(t) = v0 \* sin(alpha) \* t - g \* t^2 / 2 -- t = x / (v0 \* cos(alpha)) -- y = v0 \* sin(alpha) \* x / (v0 \* cos(alpha)) - g \* x^2 / (2 \* v0^2 \* cos^2(alpha)) -- phi = angle of sight to target -- Apply various transformations to get: -- sin(2 \* alpha - phi) = right-hand side (RHS) -- If RHS \> 1, the sine argument was invalid: the shot will miss.

target is the point you’re clicking, and point where you’re starting from. It may be relevant (i.e. it’s a  reasonable oversight to make) to note that y is a relative position, not absolute.

In the above, you can just pretend dy isn’t there; this was ported from 3D code (a ball game). Anyhow, that first check is just how far in the “x” direction we go… if it’s 0 we’re firing straight up and can fall back to VerticalLaunch (just special-cased version of what follows).

Then you’ll have those two x and y equations (note again these are relative to point), which you’ll see in trajectory formulae when you use the angle alpha (which we don’t know). The first one can be arranged in terms of t, and can then be substituted into the y equation where we have t.

phi, the “line of sight” angle, is the easy one to find. It’s the one formed by the x-axis and the point -> target segment. For the rest of the process, seethe excerpt in the Mechanics book (pp. 188-9, if the link didn’t quite hit it). It looks like anything else I’d say is in there.

Ok, well, I think it’s fair to say that I’ve failed, I’m afraid. I’ve kept various versions of my code as I read around the internet and have put all the links in the file. You can download it if you want to read up or try to rearrange some of my math, just in case anything jumps out at you:

https://www.dropbox.com/s/kiicnw8agunfhfn/main.lua

I have noticed that I got solutions which appear to be close, but that converting between pixels and Box2D units was required. I eventually used the fromMKS and toMKS functions (physics.* library) to do this, but still wound up with incorrect values.

Sorry that I couldn’t help more. I would love to solve this problem and if you do it I would very much encourage you to post the code here, to the Code Exchange (with a link in this thread) or even write a tutorial on it and let Brent/Rob know (so they can put it on the blog.)

I will keep thinking about this problem (as a code dog, I can’t stop chasing solution cars) and if I figure it out I’ll post here, of course.

Good luck.

Thanks for everyone’s help. I’m still trying to get this to work correctly. I’m going to keep doing research and tests, I’ll update when/if i get it working right.

Quick update guys. I was still having a lot of trouble figuring it out so i ended up hiring a physics tutor to help me out. It took us three hours, but we ended up figuring it out. Once i get a chance i will port it over to a sample app and put it up for you guys to use.

Wow, cool, really glad you made it. That’s quite a commitment, well done!