0
\$\begingroup\$

I am currently developing small 2D game in Unity, in which player is using swipe to create objects paths. I already did almost everything, but I hit a wall which i cannot pass. The thing is, that when I am using Bezier Curve I have to specify all points to have a base for my curve, but I'd like my object to move further after passing end point. Look at the image.

enter image description here

I've created a curve based on points A, B and C. The curve is green, but it doesn't reach the screen end, so I'd like to extend it with the red line. This my code to get a point on a curve.

public static Vector3 GetPoint(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
{
    t = Mathf.Clamp01(t);
    float oneMinusT = 1f - t;
    return
        oneMinusT * oneMinusT * oneMinusT * p0 +
        3f * oneMinusT * oneMinusT * t * p1 +
        3f * oneMinusT * t * t * p2 +
        t * t * t * p3;
}

I heard that passing parameter T greater than 1 should acutally do the thing, but it does not always work with my curves. Sometimes the object starts to go backwards or goes into some random direction. I would be grateful if someone could help me solve this problem. Thank you.

\$\endgroup\$
  • \$\begingroup\$ Your diagram shows 3 points, A/B/C (sufficient for a quadratic Bézier curve), but your code shows 4 points, p0-p3 (sufficient for a cubic).Are you doubling one of the points? That may be the cause of the unpredictable behaviour when overshooting. If you only need 3 points, consider using a Bézier curve of lower degree. \$\endgroup\$ – DMGregory Oct 7 '16 at 15:27
0
\$\begingroup\$

You could continue the Bezier curve with another cubic curve to accomplish what you want. Basically add a new curve that starts at C and ends where the line crossing points C & B intersects the screen border. To ensure that this extended curve is C2 continuous with the original Bezier curve (i.e. is smooth in both direction and velocity), you should have the same tangent (both length & direction) at C for both curves. For the end point tangent you could use direction of C & B line and length of the start tangent.

This would be easiest to accomplish using cubic Hermite curves instead, which are controlled with end point positions and tangents. If you want to keep your original Bezier curve evaluation though, you could evaluate what the tangent for the curve at C and create Hermite curve for the extension. I think if you want to use Bezier curve also for the curve extension you could create control points at 2C-B and another at X+(B-C) where X is the intersection between the screen border and C-B line to ensure C2 continuity in addition to C and X control points.

Also another easier solution would be to simply just continue the curve to the direction of the tangent at the end of the curve until it intersects the screen border. You just need to be careful how you interpolate this line segment to ensure C2 continuity.

\$\endgroup\$
  • \$\begingroup\$ I thought about continuing the curve to the direction of the tangent but unfortunetly I don't know how to achieve that to not experience rapid direction change :/ I know that tangent is first derivative, but when I tried to apply it just didn't work. Maby Cubic Hermite is the answer, I'll give it a shot. \$\endgroup\$ – Joey Grast Oct 7 '16 at 13:47
  • \$\begingroup\$ Thank you, the advice with creating 4th point based on 2nd and 3rd point totally worked! \$\endgroup\$ – Joey Grast Oct 9 '16 at 13:56
  • \$\begingroup\$ Great! You can mark this as the answer to let others know your question has been answered \$\endgroup\$ – JarkkoL Oct 9 '16 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.