For a game engine I'm writing, I need a function that returns the initial velocity and angle of a projectile launch, such that the affected object reaches a given maximum height, and ends up in a specified coordinate. The world is 2D from the side, for the sake of calculations.
So the following is known:
- Starting X
- Starting Y
- Target X
- Target Y
- Required maximum height
And I need to calculate the following:
- Initial velocity
- Launch angle
(Or the initial velocity in X and the initial velocity in Y.)
I've searched around for the formula that would give me this, but I cannot find one. So I decided to deduce it myself, with my limited mathematical equation skills.
Wikipedia's page on projectile motion was my go-to for most of my efforts. For a handful of the formulae provided there, I can convert them, but only to things that would return the launch angle when the velocity is known, or to return the velocity when the launch angle is known (among other known variables). I cannot figure out exactly how to get a formula that will return me two unknowns, nor how to mangle existing formulae to suit those needs.
I have tried grabbing the formula for the maximum distance of a projectile, from that page and the maximum height formula, also from the same page, and feed those into Wolfram Alpha, asking it to solve me for the velocity and angle. The output seems like nonsense to me, and even refers to a new variable,
n. Not to mention this answer would also not let me use the target height, and when I tried to plug the formula from Wikipedia that did, Wolfram Alpha wouldn't even understand my input. So I abandoned the idea.
No approach would even consider the intended maximum height requirement. Eventually, among many searches, I stumbled upon this answer, and I was able to implement it. I could use this intermediate coordinate as X = (target X + starting X) / 2, and Y = required maximum height. This works perfectly, but only if the target Y is the same as the starting Y... This can be seen in the post's GIF, when the intermediate point is at half distance horizontally, but the target point and starting point have different heights -- the trajectory goes higher than the intermediate point.
This looks like my best bet, but I have no idea how to grab that approach and force the height to reach the intended level. If I try to change the Y component of the initial velocity to the value given by the formula of maximum height: $\sqrt(2gy)$ , that will break the entire calculation and make the projectile miss its target (again, when the starting height is different from the target height).
(Note: I am aware that some trajectories may be impossible -- I can handle these cases and purposely make the projectile fall short of the target.)
This code is needed to allow players to throw objects such that they land exactly where the player aims, but each object has a maximum height it can reach, forcing the player to use the right objects for the job.
After spending days on this, I am at a complete loss as to how to tackle this problem. Is it even a possible formula, or is it mathematically impossible to determine?