Jak osiągnąć jednolitą prędkość ruchu na krzywej Beziera?

22

Próbuję przesunąć obraz wzdłuż krzywej Beziera. Tak to robię:

- (void)startFly
{    
 [self runAction:[CCSequence actions:
             [CCBezierBy actionWithDuration:timeFlying bezier:[self getPathWithDirection:currentDirection]],
             [CCCallFuncN actionWithTarget:self selector:@selector(endFly)],
             nil]];

}

Moim problemem jest to, że obraz porusza się nierównomiernie. Na początku porusza się powoli, a następnie stopniowo przyspiesza, a na koniec bardzo szybko. Co powinienem zrobić, aby pozbyć się tego przyspieszenia?

Andrey Chernukha
źródło

Odpowiedzi:

27

Możliwe jest przybliżenie rozwiązania tego problemu dla większości trajektorii parametrycznych. Pomysł jest następujący: jeśli powiększysz wystarczająco głęboko krzywą, nie możesz odróżnić samej krzywej od jej stycznej w tym punkcie.

Przyjmując to założenie, nie ma potrzeby wstępnego obliczania niczego więcej niż dwóch wektorów (trzy dla sześciennych krzywych Beziera itp. .).

Dla krzywej M(t) obliczamy jej wektor styczny dMdt w punkciet. Normą tego wektora jestdMdTa zatem odległość przebytą przez czasΔtmożna określić w przybliżeniu jakodMdTΔt. Wynika z tego, że przebywa się odległośćLna czasL÷dMdT.

Zastosowanie: kwadratowa krzywa Beziera

Jeśli punktami kontrolnymi krzywej Beziera są A , B i C , trajektorię można wyrazić jako:

M(t)=(1t)2A+2t(1t)B+t2C=t2(A2B+C)+t(2A+2B)+A

Więc pochodna to:

dMdt=t(2A4B+2C)+(2A+2B)

Musisz po prostu przechowywać wektory v1=2A4B+2C a v2=2A+2B gdzieś. Następnie, dla danego t , jeśli chcesz przejść o długość L , robisz:

t=t+Llength(tv1+v2)

Sześcienne krzywe Beziera

To samo rozumowanie dotyczy krzywej z czterema punktami kontrolnymi A , B , C i D :

M(t)=(1t)3A+3t(1t)2B+3t2(1t)C+t3D=t3(A+3B3C+D)+t2(3A6B+3C)+t(3A+3B)+A

Pochodna to:

dMdt=t2(3A+9B9C+3D)+t(6A12B+6C)+(3A+3B)

Wstępnie obliczamy trzy wektory:

v1=3A+9B9C+3Dv2=6A12B+6Cv3=3A+3B

and the final formula is:

t=t+Llength(t2v1+tv2+v3)

Accuracy issues

If you are running at a reasonable framerate, L (which should be computed according to the frame duration) will be sufficiently small for the approximation to work.

However, you may experience inaccuracies in extreme cases. If L is too large, you can do the computation piecewise, for instance using 10 parts:

for (int i = 0; i < 10; i++)
    t = t + (L / 10) / length(t * v1 + v2);
sam hocevar
źródło
1
Hi. I am reading your answer,but I can't understand what is L. What do you mean by "which should be computed according to the frame duration"?
Michael IV
Is L = curve segment length?
Michael IV
L is the curve length, i.e. the distance you want to travel during the current frame.
sam hocevar
OK,I see now. And you think this approximation is as good as curve splitting technique from the answer below?
Michael IV
When L is sufficiently small, this approximation is actually always more accurate than the answer below, yes. It also uses less memory (because it uses the derivative instead of storing all point values). When L starts to grow, you can use the technique I suggest at the end.
sam hocevar
6

You need to reparamaterize the curve. The easiest way to do this is to calculate the arc lengths of several segments of the curve and use these to figure out where you should sample from. For example, maybe at t=0.5 (halfway through), you should pass s=0.7 to the curve to get the "halfway" position. You need to store a list of arc lengths of various curve segments to do this.

There are probably better ways, but here's some very simple C# code I wrote to do this in my game. It should be easy to port to Objective C:

public sealed class CurveMap<TCurve> where TCurve : struct, ICurve
{
    private readonly float[] _arcLengths;
    private readonly float _ratio;
    public float length { get; private set; }
    public TCurve curve { get; private set; }
    public bool isSet { get { return !length.isNaN(); } }
    public int resolution { get { return _arcLengths.Length; } }

    public CurveMap(int resolution)
    {
        _arcLengths = new float[resolution];
        _ratio = 1f / resolution;
        length = float.NaN;
    }

    public void set(TCurve c)
    {
        curve = c;
        Vector2 o = c.sample(0);
        float ox = o.X;
        float oy = o.Y;
        float clen = 0;
        int nSamples = _arcLengths.Length;
        for(int i = 0; i < nSamples; i++)
        {
            float t = (i + 1) * _ratio;
            Vector2 p = c.sample(t);
            float dx = ox - p.X;
            float dy = oy - p.Y;
            clen += (dx * dx + dy * dy).sqrt();
            _arcLengths[i] = clen;
            ox = p.X;
            oy = p.Y;
        }
        length = clen;
    }

    public Vector2 sample(float u)
    {
        if(u <= 0) return curve.sample(0);
        if(u >= 1) return curve.sample(1);

        int index = 0;
        int low = 0;
        int high = resolution - 1;
        float target = u * length;
        float found = float.NaN;

        // Binary search to find largest value <= target
        while(low < high)
        {
            index = (low + high) / 2;
            found = _arcLengths[index];
            if (found < target)
                low = index + 1;
            else
                high = index;
        }

        // If the value we found is greater than the target value, retreat
        if (found > target)
            index--;

        if(index < 0) return curve.sample(0);
        if(index >= resolution - 1) return curve.sample(1);

        // Linear interpolation for index
        float min = _arcLengths[index];
        float max = _arcLengths[index + 1];
        Debug.Assert(min <= target && max >= target);
        float interp = (target - min) / (max - min);
        Debug.Assert(interp >= 0 && interp <= 1);
        return curve.sample((index + interp + 1) * _ratio);
    }
}

Edit: It's worth noting that this won't give you the exact arc length, since it's impossible to get the arc length of a cubic curve. All this does is estimate the length of the various segments. Depending on how long the curve is, you may need to increase the resolution to prevent it from changing speeds when it reaches a new segment. I usually use ~100, which I have never had a problem with.

Robert Fraser
źródło
0

A very lightweight solution is to approximate the speed rather than approximating the curve. Actually this approach is independent of the curve function and enables you to use any exact curve instead of using derivatives or approximations.

Here is the code for C# Unity 3D:

public float speed; // target linear speed

// determine an initial value by checking where speedFactor converges
float speedFactor = speed / 10; 

float targetStepSize = speed / 60f; // divide by fixedUpdate frame rate
float lastStepSize;

void Update ()
{   
    // Take a note of your previous position.
    Vector3 previousPosition = transform.position;

    // Advance on the curve to the next t;
    transform.position = BezierOrOtherCurveFunction(p0, p1, ..., t);

    // Measure your movement length
    lastStepSize = Vector3.Magnitude(transform.position - previousPosition);

    // Accelerate or decelerate according to your latest step size.
    if (lastStepSize < targetStepSize) 
    {
        speedFactor *= 1.1f;
    }
    else
    {
        speedFactor *= 0.9f;
    }

    t += speedFactor * Time.deltaTime;
}

Although the solution is independent of the curve function, I wanted to note it here as I was also looking for how to achieve constant speed on a Bezier curve, and then I come up with this solution. Considering the popularity of the function this may be helpful here.

Guney Ozsan
źródło
-3

I don't know anything about cocos2, but a bezier curve is a kind of parametric equation, so you should be able to get your x and y values in terms of time.

Jebbles
źródło
4
Add an example + more explanation and this would be a good answer.
MichaelHouse