This is first-year physics homework but since I’ve forgotten that and we have computers what I would do would forward-solve iterations until they converge on a hit, assuming you’re not doing this too often per frame.
I understand that this is a physics problem. The problem is the equation I have is returning NaN, so it appears either I am missing something, or the equation is not doing what it is suppose to do. I am looking for someone with a suggestion on projectile trajectory. I am not sure what you mean Troy with forward solve iterations, I am dealing with velocity, angle and forces. Therefore I need to shoot the projectile at a certain angle in order to hit a target.
The current Equation I have:
where
G = -9.8
H = height difference
L = distance from target
V = initial velocity
For some reason returns me NaN - which generally means the velocity is not high enough, but it doesn’t matter how much I change v, it still doesn’t return a number?
By forward solve I mean run a fake shot simulation and see where the shot misses and then iteratively adjust the aiming angle until you converge on a solution.
This is a bit more complicated with a black-box physics engine like PhysX.
NaN does not mean zero, it means the number has gone bad, likely by dividing zero over zero.
If you want to solve the problem analytically, the common approach is to find the time of flight to the target:
dy = t * vy - 0.5g(t*t)
dy is target y - firer y
vy is the initial vertical velocity of the shot.
and solve for t
With the time to target and horizontal components (x,z) of the velocity vector you can work out the horizontal error (position of target - (horizontal velocity * time to target + position of firer).
Working on the horizontal plane, then apply geometry to solve the angle of the triangle you have.
fwiw this is covered in Chapter 4 of Halliday Resnick, which unfortunately I took 23 years ago.
Okay so using the equation on the wiki is correct. It is the angle with the inverse tangent. It will give you two angles, one being the larger arc where it passes between two points, and the other where the first point it passes is the target.