If you ever end up with an equation like
f(x) = g(x)
and you're unable to isolate x, there are many fast ways to get good approximations for x.
Newton's Method
Rearrange it so that you have
f(x) - g(x) = 0
Now your problem is reduced to finding the zeroes of f(x)-g(x). This is still difficult, unless you're clever, like Newton was. If you take a tangent to this function at some point x1, then find the 0-intercept of that tangent, the x-position of that intercept is probably closer to the zero of (f[x] - g[x]) than your start point was. Call the tangent's intercept x2. Repeat this process from x2. Unless you picked your start position poorly, you should iteratively get closer.
Code example:
Code:
float fcn(float v,float d,float h,float t)
{ return 0.5*sin(2*t)*d - (G*d*d)/(2*v*v) - h*cos(t)*cos(t);
}
float der(float v,float d,float h,float t)
{ return cos(2*t)*d + 2*h*cos(t)*sin(t);
}
float getAngle(float v,float d,float h)
{
// function: f[t] = 0.5*sin(2t)*d - gdd/2vv - hcoscos
// derivative: f'[t] = cos(2t)*dist + 2h*cos*sin
float xn = PI/8; // I start at 22.5 degrees because I know the solution will be somewhere nearby
for(int x=0;x<5;x++)
{
xn = xn - fcn(v,d,h,xn)/der(v,d,h,xn);
}
return xn;
}
In this code, I get close enough in just 5 iterations, and I could probably cut back on the iteration count. Note that this does not include any air resistance or spin or anything.
Also check out the wikipedia article on this: http://en.wikipedia.org/wiki/Newton's_method