Find Direction of Bezier Curve

17973-hep.png

I’ve been googling around for a bit, but I haven’t found anything useful. So my question is how do you get the two r points where the green line lies along the orange line?

Well, you might have heard about the Derivative in math. Actually that’s all you need :wink:

If you take a look at the Bezier wiki page at the bottom of the cubic bezier curves section theres the first and second derivative equation. This equation gives you the tangent vector at the given “t” value. So if your control points are in worldspace, you get a worldspace vector

edit
I just re-read your question and it’s not really clear what you actually want (and for what purpose).

The “helper points and lines” do not exist in the “combined” bezier equation. A bezier curve point can be calculated “manually” by doing a 3 step lerp like this:

// C#
public static Vector3 CalculateBezier(Vector3 P1, Vector3 P2, Vector3 P3, Vector3 P4, float t)
{
    Vector3 A1 = Vector3.Lerp(P1,P2,t);
    Vector3 A2 = Vector3.Lerp(P2,P3,t);
    Vector3 A3 = Vector3.Lerp(P3,P4,t);
    
    Vector3 B1 = Vector3.Lerp(A1,A2,t);
    Vector3 B2 = Vector3.Lerp(A2,A3,t);
    
    return Vector3.Lerp(B1,B2,t);
}

The points A1,A2,A3 are the corners of the orange lines and B1 and B2 are the corners of the green line. Since Vector3.Lerp is a function it’s quite costy. That’s why you usually simplify the equation.

public static Vector3 CalculateBezier(Vector3 P1, Vector3 P2, Vector3 P3, Vector3 P4, float t)
{
    t = Mathf.Clamp01(t);
    Vector3 A1 = P1 + (P2-P1) * t;
    Vector3 A2 = P2 + (P3-P2) * t;
    Vector3 A3 = P3 + (P4-P3) * t;
    
    Vector3 B1 = A1 + (A2-A1) * t;
    Vector3 B2 = A2 + (A3-A2) * t;
    
    return B1 + (B2-B1) * t;
}

This is what “logically” happens inside Lerp, however the way i did it here it’s actually worse than Vector3.Lerp :wink: Since we work with Vectors all operations are also function calls.

Usually you use the combined equation for a cubic bezier curve which looks like this:

// C#
private static float cubeBezier(float P1, float P2, float P3, float P4, float t)
{
    float rt = 1.0f-t;
    float rtt = rt * t;
    return rt*rt*rt*P1 + 3*rt*rtt*P2 + 3*rtt*t*P3 + t*t*t*P4;
}

private static Vector3 cubeBezier(Vector3 P1, Vector3 P2, Vector3 P3, Vector3 P4, float t)
{
    t = Mathf.Clamp01(t);
    return new Vector3(
        cubeBezier(P1.x, P2.x, P3.x, P4.x, t),
        cubeBezier(P1.y, P2.y, P3.y, P4.y, t),
        cubeBezier(P1.z, P2.z, P3.z, P4.z, t),
    );
}

This directly calculates the point on the curve. Usually no one is interested in the “helper lines”.

I’ve simplified the equation for the last two helper points into this:

public static float ControlPoint(float P1, float P2, float P3, float t)
{
    return (P1+P3-2*P2)*t*t + (P2-P1)*2*t + P1;
}
public static Vector3 ControlPoint(Vector3 P1, Vector3 P2, Vector3 P3, float t)
{
    t = Mathf.Clamp01(t);
    return new Vector3(
        ControlPoint(P1.x,P2.x,P3.x,t),
        ControlPoint(P1.y,P2.y,P3.y,t),
        ControlPoint(P1.z,P2.z,P3.z,t)
    );
}

To get the first of the two points do this:

ControlPoint(P1,P2,P3,t);

to get the second point do this:

ControlPoint(P2,P3,P4,t);

Now there shouldn’t be anything left :wink: