LibGDX Path (CatmullRomSpline) Constant Speed

前端 未结 1 399
日久生厌
日久生厌 2021-01-19 17:53

I’m trying to achieve constant speed on a path using the LibGDX CatmullRomSpline and I’m having problems getting it to work. I’ve tried researching on this topic a lot inclu

相关标签:
1条回答
  • Since the derivative is the rate of change of the spline position it is indeed the 'speed', and when the spline is bending away from the underlying data points it has to 'speed up' to make the calculated spline reach the next data point in time, you must divide out this speed to perceive a visual constant speed.

    You aren't getting a constant speed because you are still incrementing your time variable by delta instead of delta divided by the rate of change (derivative). You should be adding a variable amount to the percent variable each frame, instead you were modifying everything by the derivative of a single point along the Catmull-Rom spline.

    Instead of:

    catmull.derivativeAt(previousDerivative, percent);
    time += delta;
    percent = ((time / cycle) / previousDerivative.len() ) % 1;
    

    You should:

    catmull.derivativeAt(previousDerivative, percent);
    percent += derivativeAverage / cycle * delta / previousDerivative.len();
    percent %= 1;
    

    you should use the average derivative divided by cycle now since you can't use cycle alone as a percent per second variable anymore.

    Iterating over the spline to find the average value of the derivativeAverage:

     int samples = 100; //the higher the more accurate, however slower
     float derivativeAverage = 0;
     Vector2 out = new Vector2();
     for (float i=0;i<1;i+=1f/samples) {
         catmull.derivativeAt(out, i);
         derivativeAverage += out.len();
     }
     derivativeAverage /= samples;
    
    0 讨论(0)
提交回复
热议问题