我正在尝试使用样条曲线,我想自己解决在 github 上阅读本指南后出现的问题。这是我设法从该指南中收集的代码:
public class MyCatmullRomSpline {
private int mCountSections; //кол-во делений(точек) между основными точками указанными в dataPoints
private Vector2[] mSections; //массив делений
private ShapeRenderer mShapeRenderer;
private CatmullRomSpline<Vector2> mSpline;
private Vector2 out;
private float speed; //скорость перемещения чего-либо по кривой
public GCatmullRomSpline(Vector2[] dataPoints, boolean closed, int countSections){
speed = dataPoints.length - 1;
mSpline = new CatmullRomSpline<Vector2>(dataPoints, closed);
mSections = new Vector2[countSections];
mCountSections = countSections;
mShapeRenderer = new ShapeRenderer();
initSpline();
}
private void initSpline(){
out = new Vector2();
mSpline.valueAt(out, Gdx.graphics.getDeltaTime()); //?????
mSpline.derivativeAt(out, Gdx.graphics.getDeltaTime()); //?????
for (int i = 0; i < mCountSections; ++i){
mSections[i] = new Vector2();
mSpline.valueAt(mSections[i], ((float)i)/((float)mCountSections-1));//?????
}
}
float current = 0;
public void render(SpriteBatch batch, Texture texture){
mShapeRenderer.begin(ShapeRenderer.ShapeType.Line);
float derivativeAverage = 0;
for (float i = 0; i < 1; i += 1f/mCountSections) {
mSpline.derivativeAt(out, i); //?????
derivativeAverage += out.len();
}
derivativeAverage /= mCountSections;
mSpline.derivativeAt(out, current);
current += derivativeAverage / speed * Gdx.graphics.getDeltaTime() / out.len();
current %= 1;
if (current >= 1) current -= 1;
mSpline.valueAt(out, current);
batch.begin();
batch.draw(texture, out.x - 18, out.y - 18, 36, 36);
batch.end();
for (int i = 0; i < mCountSections-1; ++i){
mShapeRenderer.line(
mSpline.valueAt(mSections[i], ((float)i)/((float)mCountSections-1)),
mSpline.valueAt(mSections[i+1], ((float)(i+1))/((float)mCountSections-1)));
}
mShapeRenderer.end();
}
}
问题一:
方法: mSpline.valueAt(out, Gdx.graphics.getDeltaTime()); 和 mSpline.derivativeAt(out, Gdx.graphics.getDeltaTime()); - 包含相同的正文和语法。
public T valueAt (T out, float t) {
final int n = spanCount;
float u = t * n;
int i = (t >= 1f) ? (n - 1) : (int)u;
u -= i;
return valueAt(out, i, u);
}
public T derivativeAt (T out, float t) {
final int n = spanCount;
float u = t * n;
int i = (t >= 1f) ? (n - 1) : (int)u;
u -= i;
return derivativeAt(out, i, u);
}
他们有什么区别?
PS:我的微分数学很弱。

两种方法都对参数和区间进行了一定的归一化,然后分别用三个参数调用相应的方法,并且已经在其中计算了值(样条上的点)或导数(方向向量)