Calculate force necessary for 2D parabolic shot

Hi all,
I’m trying to make a 2D game, where a ball will be launched from a starting position towards the mouse cursor, in a parabolic motion, with the cursor acting as the vertex of the shot. As of right now, from looking around the forums and brainstorming a bit, I have come up with pseudo-codish way to create this.

By getting the position of the vertex and the ball when it starts, and taking into account the force of gravity, I can calculate the starting angle the ball will need to be shot at to pass through the vertex. I am currently thinking of doing this using AddForce(). However, where I see a problem occurring is knowing exactly how much force to add to the ball, so that it peaks exactly at the vertex, and doesn’t fly off into the distance.

(I have no code as of yet because I don’t want to spend the time doing this with such a glaring problem. Even a proper link to some physics website that would point me in the right direction would help).

Thanks for the help,
Epic

First, a question as something to consider:

What is the intended behavior if the mouse cursor is below the starting point? This would mean that the apex would implicitly be the starting point, so the cursor could no longer fulfill that role.


In any case where the mouse cursor is above (not level with) the character, however, there's something important to consider:

At a fundamental level, any basic trajectory calculation IS 2-dimensional. They’re broken up into a horizontal and vertical element which, combined, form the full trajectory. (Note: caveats and exceptions include multiple gravitational forces pulling in various directions, wind, drag, etc.)

With that in mind, let’s start with vertical, using my old post (comment) for reference:

Since gravity is an acceleration force, changing current velocity on a per-second basis, calculating the necessary vertical speed to reach the apex is fairly straightforward:

public static float CalculateForceToApex(float gravityStrength, float jumpHeight)
{
	//h = v^2/2g
	//2gh = v^2
	//sqrt(2gh) = v
	return Mathf.Sqrt(2 * gravityStrength * jumpHeight);
}

… where “gravityStrength” would be, for example, 9.81f, or Physics(2D).gravity.magnitude.

Once you have that vertical force, you can translate it into time by dividing it by that same gravity magnitude:

float verticalForce = CalculateForceToApex(Physics2D.gravity.magnitude, mouseWorldPos.y - character.y;
float timeToApex = verticalForce / gravityStrength;

Once you have the total time, horizontal speed becomes trivial:

float horizontalForce = horizontalDistance / timeToApex;