I should really be able to figure this one out myself but it is driving me mad - my maths knowledge seems to be just too rusty.
I basically want to throw a ball from any height (position x,y,z in 3d space) onto a target point in 3D space. On top of this, I am checking that the ball with the calculated parabola does not hit a net with a certain height. I came up with the correct calculations to hit the target point based on this site: Trajectories
So I am defining a velocity and calculating the appropriate angle.
But now I am checking whether it hits the net, and if it does want to change the angle and/or velocity so it goes higher.
But I am having a real problem changing the equation from the “where will it land” part so that I can adjust the height to go over the net height. How can I reverse the equation from x and time? I have the start x and y, the target x and y and need to determine the angle and velocity (I can always make up one of them, just need to get the velocity). The one I came up with is obviously wrong, I am missing the x and y part - I can only adjust the throwing velocity correctly if the start and end point are on the same y level.
If both objects are at the same height, things are easier. If they have different heights, however, the equation becomes too complicated, so complicated that I was not able to get the inverse function. But I used a simple trick to correct the trajectory when the height difference is relatively small compared to the horizontal distance (how much is “small” depends on the elevation angle). The idea is to aim closer or farther than the target distance in such a way that the trajectory passes through its actual position:
This trick works fine for relatively small differences, as I said, but since it relies on a straight line approximation of the trajectory near to the zero height point, the error grows with the height difference.
You can see my answer with the complete ballistic calculation function in this question:
EDITED: The equation you have is interesting because it takes the precise height difference into account, while mine just approximates it. Unfortunately, it returns the angle, not the velocity, and this complicate things a lot!
My function does the opposite - returns a velocity vector for a given angle - but isn’t so precise when the heights are different. Maybe you could combine both and try a numeric solution: find an initial angle based on the fence height and distance from the target, use my function to find the necessary velocity, refine the angle with your equation (the second one - the first always returns the higher angle) and check if the fence is being hit: if yes, increase the initial angle (multiply it by 1.10, for instance) and repeat the process.
Just as a convenience, I posted the function BallisticVel below:
function BallisticVel(target: Transform, angle: float): Vector3 {
var dir = target.position - transform.position; // get target direction
var h = dir.y; // get height difference
dir.y = 0; // retain only the horizontal direction
var dist = dir.magnitude ; // get horizontal distance
var a = angle * Mathf.Deg2Rad; // convert angle to radians
dir.y = dist * Mathf.Tan(a); // set dir to the elevation angle
dist += h / Mathf.Tan(a); // correct for small height differences
// calculate the velocity magnitude
var vel = Mathf.Sqrt(dist * Physics.gravity.magnitude / Mathf.Sin(2 * a));
// return the complete velocity vector
return vel * dir.normalized;
}