How To Make Quadratic Bezier Curve Through 3 Points

My code:

private Vector3 GetPointInPath(Vector3 playerPos, Vector3 netPosTop, Vector3 aimPos, float t)
{
   float r = 1f - t;
   return (r * r * playerPos) + (2f * r * t * netPosTop) + (t * t * aimPos); 
 }

Any idea how to make Bezier Curve go through netPosTop, instead of being controlled by it?

You just can’t because there are infinitely many bezier curves that will go through those 3 points. Keep in mind that if you have point A, B and C and your curve should start at A and end at C and go through B, the curve could start and end at various angles and stil go through B. So there’s not a single solution for this problem. Have a look at this interactive Desmos drawing. I placed the middle control point at a position so the curve goes through the point B I have defined. Just try moving the middle control point around and you find infinitely many positions which all make your curve pass through point B.

Of course you could specify some constraints and extra conditions. However calculating the position of the middle conrol point is not that trivial depending on your extra conditions. For example if you want your thrid point being at t == 0.5 you can calculate the position of the middle control point by:

    B = 2*yourPointOnTheCurve − 0.5f*A−0.5*C;

You simply get that equation by plugging in 0.5 into the bezier equation and solving for B.

    p(t) = (1-t)² * A + 2f *(1-t) * t * B + t * t * C
    // plug in 0.5 and define that p(0.5) == yourPointOnTheCurve
    yourPointOnTheCurve = 0.25 * A + 0.5 * B + 0.25 * C
    // solve for B
    yourPointOnTheCurve - 0.25 * A - 0.25 * C =  0.5 * B
    2*yourPointOnTheCurve - 0.5 * A - 0.5 * C =  B

And there you have your answer. Of course if the point you want your curve to go through is extremely close to the first or last point you will get very weird curves since, remember, that point is at t==0.5. So the curve is bend too much. Here’s an example

edit

Based on my comments below you can create some helper methods like this:

    // untested code
    public static Vector2 CalculateMiddlePoint(Vector2 aStart, Vector2 aEnd, float aTime, Vector2 aPoint)
    {
        float t = aTime;
        float rt = 1f-t;
        return 0.5f*(aPoint - rt*rt*aStart - t*t*aEnd) / (t*rt);
    }
    
    public static float CalculateMiddleTime(Vector2 aStart, Vector2 aEnd, Vector2 aPoint)
    {
        float a = Vector2.Distance(aPoint, aStart);
        float b = Vector2.Distance(aPoint, aEnd);
        return a / (a+b);
    }
    
    public static Vector2 CalculateBestMiddlePoint(Vector2 aStart, Vector2 aEnd, Vector2 aPoint)
    {
        return CalculateMiddlePoint(aStart, aEnd, CalculateMiddleTime(aStart, aEnd, aPoint), aPoint);
    }

Using CalculateBestMiddlePoint and passing your two endpoints as the first and second argument and your third point as last parameter, this method should calculate the “best fitting” middle control point for your curve.

edit
Since the migration to Discourse all my comments to my answer has been “destroyed” / deleted as they were longer than the new limit for comments…
I quickly used the wayback machine to recover them. I copy them into my answer:

(Comment 1)
I just had some time and created this desmos example where you can drag around your 3 points and define the value “k” (slider on the left) to define where that third point should be on the curve. It will automatically calculate the 3rd control point. Now you can play around to see what the result will look like for certain positions and values for “k”. Note I had to use “k” because “t” is already used internally by desmos.

(Comment 2)
I just had the idea to calculate a “reasonable” k value we could simply use the distances between our 3 given points. The result looks like this. I will edit my answer and include that in my answer.

(Comment 3)
In my first example you could choose “k” manually. So you see what happens when you set k to 0 or 1. It can not determine any curve as it’s impossible to have the middle point at time 0 or 1 since those define the endpoints of the curve. They can’t be endpoints and middle point at the same time.

@Bunny83 What happens if K is 1?