RAZORUNREAL 567 Report post Posted October 4, 2006 I'm attempting to find the angle to fire a projectile at such that it lands at a specific point. I have only done (in fact, I havn't even completed) high school level physics and calculus, so please bear with me. Here's what I've done so far: T is the angle fired at (for lack of a theta) Vx is initial horizontal velocity Vy is initial vertical velocity s is the initial speed of the projectile t is time d is horizontal distance to target h is difference in height of target Vx = cos(T).s Vy = sin(T).s d = Vx.t t = d/Vx t = d/cos(T)/s h = Vy.t + 1/2.a.t^2 h = sin(T).d/cos(T) + 1/2.a.(d/cos(T)/s)^2 h = sin(T).d/cos(T) + 1/2.a.d^2/cos(T)^2/s^2 h = tan(T).d + 1/2.a.d^2/cos(T)^2/s^2 So if I could solve that last equation and find T, I'd be pretty much done. But I'm not sure how to do that, and for all I know I'm barking up entirely the wrong tree anyway. Also, if it's not possible to hit the target point, it would be nice to find the angle that comes closest. But it's not essential, I can just set it to 45 degrees and call that good enough. Thanks. 0 Share this post Link to post Share on other sites
Darkstrike 206 Report post Posted October 4, 2006 Express the involved polynomials in cos(T) and sin(T) in terms of cos(2T) and sin(2T); you'll get a linear equation in those, which can be easily solved e.g. by converting it further using a "helper" angle.As to "the angle that comes closest", what exactly is your measure of closeness? The answer depends on this a lot, but in any case, you can calculate the locus of reachable point pretty easily and proceed from there. 0 Share this post Link to post Share on other sites
daniel_i_l 295 Report post Posted October 4, 2006 well, you could either sole the trig equation - for that you'd first have to decide on an initial speed (s). or to simplify you could decide on an initial horizontal velocity (Vx) and then use it to get the time (d = Vx*t) and use t to find Vy (h = Vy*t - 0.5g*t^2). with Vy and Vx you can get V and the angle. but if you need a certain V and you only want to get the angle you have to solve the trig equation. 0 Share this post Link to post Share on other sites
Necator 239 Report post Posted October 4, 2006 as Darkstrike wrote, you need to transform the sin & cos terms usingsin(2x)=2*sin(x)*cos(x) andcos(x/2)^2=(1+cos(x))/2When I calculated it, I used a different direction of the acceleration, but it's just to insert (-a) instead of a.s^2*h*cos(T)^2 = s^2*sin(T)*cos(T)*d - a*d^2/2(1+cos(2T))*h/2 = sin(2T)*d/2 - a*d^2/(2*s^2)(a*d^2+h*s^2)/s^2 + h*cos(2T) = d*sin(2T)Set (a*d^2+h*s^2)/s^2 = C.h*cos(2T) + C = d*sin(2T)cos(2T)*h/d + C/d = sin(2T)For the rest I made a very nice Paintbrush picture ^^T = (asin(C/sqrt(d^2+h^2)) + atan(h/d))/2I could have forgot some special case for asin or atan, but the basics should be correct. Probably a couple of typos too... =) 0 Share this post Link to post Share on other sites
NotAYakk 876 Report post Posted October 4, 2006 Sadly, what you described neglects air resistance/wind -- which are rather important, if you want realistic projectile motion.There is an alternative way to solve such problems. It isn't always best.Basically, you search for the right firing solution, instead of calculating it.Start out with sampling the "space" of possible firing options either randomly, or with some best guesses.Now use those samples to figure out a better set of samples. Repeat until you are close enough.If you have a simple enough problem, you can often get solutions that 'braket' your target -- one shot is to the left, the other to the right. In that case, a simple binary or dictionary search often rapidly produces an accurate solution...In your particular example, let's first solve for t in terms of h and Vy.a/2 t^2 + Vy t - h = 0t = -Vy +/- sqrt(Vy^2 + 2 a h) ---------------------------- aThere are two solutions to the above. So for a given Vy, we have t0 and t1.Next, see how far away horizontally the shot is at t0 and t1:d = Vx * td0 = t0 Vxd1 = t1 VxThat is pretty easy!Now, we start out by testing a bunch of different random firing solutions. We stop after testing a large number, or after we have 'bracketed' the target (got one to land on the far side, and one to land on the near side, of the target).Once we have 'bracketed' the target, we simply engage in a binary search through the firing space between the two options.If we haven't bracketed the target, it is possible the target cannot be hit. We take the closest shot to the target in t0, search the solutions near it, and give up if we stop getting closer. We then do the same with t1 solutions.All of the above does is solve the problem in a way that doesn't rely on the equations described being 'easy to solve' -- if you added tweaks to the equations (wind resistance), the above solution would be more robust (it wouldn't be guaranteed to work!) compared to the analytical solution. 0 Share this post Link to post Share on other sites
RAZORUNREAL 567 Report post Posted October 4, 2006 Thanks for your help guys! Especially Necator. It works perfectly.Quote:Original post by NotAYakkSadly, what you described neglects air resistance/wind -- which are rather important, if you want realistic projectile motion.I'm not worried about air resistance right now, but that technique does look useful. 0 Share this post Link to post Share on other sites