Get the required angle for projectile motion

61 Views Asked by At

I want to be able to figure out what angle the projectile should be fired from to hit a certain target which may not be at the same height as the cannon. Since the height is variable, I cant use the simpler equation of

dist = (v^2 * sin(2*theta)) / g

I need an equation for when the initial height is different.

I am trying to create a game (like pocket tanks) where the enemy AI will fire a pellet at you. but im not sure how to do the calculation to make that happen. so the AI would be aware of the height difference, the x-axis distance, the gravity and the initial velocity.

I saw the equation on wikipedia to find the distance given the angle, but i'm not sure how to rearrange the formula to get the angle given the distance.

I plugged the equation into wolframalpha and asked it to solve it for me, but I got like 10 different equations and im really confused on what I should do here other than just brute forcing it and sweeping through every angle until im able to get a hit.