How to find distance mid point of bezier curve?

I am making game in Unity engine, where car is moving along the bezier curve by percentage of bezier curve legth.

On this image you can see curve with 8 stop points (yellow spheres). Between each stop point is 20% gap of total distance.

enter image description here

On the image above everything is working correctly, but when I move handles, that the handles have different length problem occurs.

enter image description here

As you can see on image above, distances between stop points are not equal. It is because of my algorithm, because I am finding point of segment by multiplying segment length by interpolation (t). In short problem is that: if t=0.5 it is not in the 50% percent of the segment. As you can see on first image, stop points are in half of segment, but in the second image it is not in half of segment. This problem will be fixed, if there is some mathematical formula, how to find distance middle point.

enter image description here

As you can see on the image above, there are two mid points. T param mid point can be found by setting t to 0.5 (it is what i am doing now), but it is not half of the distance.

How can I find distance mid point (for cubic bezier curve, that have handles in different distance)?

Upvotes: 1

Views: 1214

Answers (1)

Dominik Mokriš
Dominik Mokriš

Reputation: 1169

You have correctly observed that the parameter value t=0.5 is generally not the point in the middle of the length. That is a good start but the difficulty lies in the mathematics beneath.

Denoting the components of your parametric curve by x(t) and y(t), the length of the curve between t=0 (the beginning) and a chosen parameter value t = u is equal to

Formula for the arc length

What you are trying to do is to find u such that l(u) is one half of l(1). This is sometimes possible but often difficult or impossible. So what can you do?

One possibility is to approximate the point you want. A straightforward way is to approximate your Bezier curve by a piecewise linear curve (simply by choosing many parameter values 0 = t_0 < t_1 < ... < t_n = 1 and connecting the values in these parameters by line segments). Now it is easy to compute the entire length (Pythagoras Theorem is your friend) as well as the middle point (walk along the piecewise linear curve the prescribed length). The more points you sample, the more precise you will be and the more time your computation will take, so there is a trade-off. Of course, you can use a more complicated numerical scheme for the approximation but that is beyond the scope of the answer.

The second possibility is to restrict yourself to a subclass of Bezier curves that will let you do what you want. These are called Pythagorean-Hodograph (shortly PH) curves. They have the extremely useful property that there exists a polynomial sigma(t) such that

hodograph equation

This means that you can compute the integral above and search for the correct value of u. However, everything comes at a price and the price here is that you will have less freedom, where to put the control points (for me as a mathematician, a cubic Bézier curve has four control points; computer graphics people often speak of "handles" so you might have to translate into your terminology). For the cubic case you can find the conditions on slide 15 of this seminar talk by Vito Vitrih.

Denote:

  • p0 ... p3 the control points,
  • Li the distance between pi and pi+1
  • enter image description here;

then the Bézier curve is a PH curve if and only if hodograph condition.

It is up to you to figure out, if you can enforce this condition in your situation or if it is too restrictive for your application.

Upvotes: 1

Related Questions