Can anyone expound this Projectile Motion computation please? I have this formula for projectile motion that I'm using in Unity game engine for getting the low or high Velocity of a projectile depending on how close or distant the target is (as long as it is within optimal range), and it works. However, I'd love to fully understand the equation.
I wrote the snippet of code below in C# language and I'm using Vector3 to represent the velocity in x, y, z which gives me the direction and movement of a rigidbody (physics-influenced object).
So..

*

*Why was force raised to the power of 4?

*How to understand what's happening in P?

*For the low and high vectors, how did it manage to get the right angles?

*Ultimately, what formula/equation in Projectile Motion was this derived from?

Vector3 dir = target - origin;

float v2 = force * force;
float v4 = v2 * v2;
float g = -Physics.gravity.y;
float x = dir.x;
float y = dir.y;
float P = v4 - g * (g * x * x + 2f * y * v2);

float sq = Mathf.Sqrt(P);

low = new Vector3(g * x, v2 - sq);
high = new Vector3(g * x, v2 + sq);

Not sure if the below image is a correct equivalent of the formula (for the y of the vector), but I hope someone can recognize.
$$u^2\pm\sqrt{[u^4-g(gx^2+2yu^2)]}$$
Apologies as I'm not well-versed with many math equations especially physics, even though I'm a programmer.
I hope you understand my plight and thanks in advance.
 A: For a projectile trajectory $\left( x(t), y(t) \right)$ to pass through two points $(0,0)$ and $(x,y)$, what is the launching velocity $v_0$ and angle $\theta$?
Write down two equations of the trajectory:
\begin{align}
x = & v_0 \cos\theta\, t; \tag{1}\\
y = & v_0 \sin\theta\, t - \frac{1}{2} g t^2. \tag{2}
\end{align}
rewrite these two equations:
\begin{align}
x = & v_0 \cos\theta\, t; \tag{3}\\
y + \frac{1}{2} g t^2 = & v_0 \sin\theta \,t. \tag{4}
\end{align}
Divide Eq.(4) with Eq.(3)
$$
 \tan\theta = \frac{y + \frac{1}{2} g t^2}{x}; \,\, \text{ or }\,\,\, x\tan\theta = y +\frac{1}{2} g t^2. \tag{5}
$$
Replace the variable $t$ in Eq.(5) by Eq.(3) $t = \frac{x}{v_0 \cos\theta}=\frac{x}{v_0}\sec\theta$
$$
  x\tan\theta = y + \frac{g x^2}{2 v_0^2} \sec^2\theta = y + \frac{g x^2}{2 v_0^2} \left(1+ \tan^2\theta\right).
$$
We have a quadratic equation for $\tan\theta$
$$
   \tan^2\theta -\frac{2 v_0^2}{g x} \tan\theta +\frac{2 v_0^2 y}{g x^2} +1 = 0.
$$
The solution for $\tan\theta$:
$$
\tan\theta = \frac{v_0^2}{g x} \pm \sqrt{ \left(\frac{v_0^2}{g x} \right)^2 -\left( 1 + \frac{2 v_0^2 y}{g x^2} \right)}
$$
$$
\tan\theta_\pm = \left( \frac{1}{g x} \right) \left\{v_0^2 \pm \sqrt{ v_0^4 -g \left( g x^2 + 2 v_0^2 y \right) } \right\}
$$
The term inside the curry braket resembles your equation. Therefore, your Vectors objects low and high record the denominator and numerator of $\tan\theta_\mp$.
After determined the angle $\tan\theta$, we again subtitute $t=\frac{x}{v_0\cos\theta}$ into Eq.(5) to calculate the speed $v_0$:
$$
  x\tan\theta = y +\frac{1}{2} g \frac{x^2}{v_0^2\cos^2\theta}.
$$
The launching speed $v_0$ expresses in term of launch angle $\tan\theta_\pm$:
$$
 v_{0\pm} = \sqrt{\frac{g x^2\left(1 + \tan^2\theta_\pm\right)}{ 2 \left(x\tan\theta_\pm - y\right)}}.
$$
