0

Пробую работать со сплайнами и хочу решить для себя вопрос, возникший после прочтения вот этого гайда на гитхабе. Вот код, который мне удалось собрать по тому гайду:

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();
    }
}

Вот изображение того.что я понял: MyCatmullRomSpline

Вопрос 1:

Методы: 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);
}

В чем их разница?

P.S: Я слаб в дифференциальной математике.

1
  • Одна тема - один вопрос, логично ?
    – MBo
    14 янв 2019 в 15:38

1 ответ 1

1

Оба метода проводят некую нормализацию параметра и промежутка, затем каждый вызывает соответствующий метод с тремя параметрами, а уже внутри тех происходит вычисление значения (точки на сплайне) либо производной (вектора направления)

3
  • Ага, понял, недосморел. Спасибо, и разница между методами мне стала понятнее. 14 янв 2019 в 15:07
  • Я могу уточнить, что метод derivativeAt(out, i, u); вычисляет производную, т.е. в каком направлении строить кривую? 14 янв 2019 в 16:01
  • Этот метод даёт вектор, который определяет направление кривой в данной точке (и у него еще есть амплитуда, влияющая на форму)
    – MBo
    14 янв 2019 в 16:13

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.