- Home /
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?
Answer by Bunny83 · Feb 18, 2020 at 03:52 PM
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.
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.
edit
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.
I was going to post one that I made but it's literally the same thing. I just never refreshed this page. Oops!
Oh, and thank-you, by the way.
Thank you so much, this brings a lot of clarity into what's the problem to solve. I think the definition of variable b in the CalculateMiddleTime method is wrong, it's using aStart and aEnd isn't used anywhere in the calculation.
Yes, it was most likely a copy & paste error ^^. I fixed it. Thanks.
I don't quite get what you want to ask. You can try out all those desmos simulations yourself. There is no K in any of my calculations here in C#. So what K do you refer to? This was about calculating a reasonable middle control point in order to construct a curve that passes through a given middle point. Have you checked my latest desmos example I had linked in my comment? The "k" in those examples is calculated from the given 3 points. You can drag those points around to see the curve change.
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 deter$$anonymous$$e 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.
If you run your code in unity when a / (a+b) is 1 it gives an error probably cuz you can't divide by 0
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Raycast Down with Approximation Search 1 Answer