跟随CatmullRomSpline以恒定速度移动身体

时间:2018-08-14 09:43:31

标签: libgdx

我需要设置一个box2body以恒定的速度跟随CatmullRomSpline,但是我已经看过thisthis,但是我不确定如何用我的代码来实现。我的catmullromspline的某些点在相机视图之外很远,有些是基于相机的位置。因此,取决于玩家的位置,它要么快闪,要么慢蜗牛。我需要它以恒定的速度移动一定的时间。

这是我的代码:

public CatmullRomSpline<Vector2> batPath;
public Vector2 batTargetPosition = new Vector2();

if (batIsHunting) {
            batTime += Gdx.graphics.getDeltaTime();
            final float maxHuntTime = 1.7f;
            float f = batTime / maxHuntTime;
            if (f <= 1.0f) {
                Vector2 bodyPosition = b2body.getWorldCenter();
                batPath.valueAt(batTargetPosition, f);
                Vector2 positionDelta = (new Vector2(batTargetPosition)).sub(bodyPosition);
                b2body.setLinearVelocity(positionDelta.scl(10));
            }

以下是CatmullRomSpline上的要点:

 batPath = new CatmullRomSpline<Vector2>(new Vector2[] {
            new Vector2(new Vector2(550 / RadiationPigeon.PPM, 25)),
            new Vector2(new Vector2(550 / RadiationPigeon.PPM, 25)),
            new Vector2(500 / RadiationPigeon.PPM, pigeoncam.position.y + pigeoncam.viewportHeight / 2 + (20 / RadiationPigeon.PPM)),
            new Vector2(pigeonBodyPositionX, pigeonBodyPositionY),
            new Vector2(-550 / RadiationPigeon.PPM, pigeoncam.position.y + pigeoncam.viewportHeight / 2 + (20 / RadiationPigeon.PPM)),
            new Vector2(-550 / RadiationPigeon.PPM, pigeoncam.position.y + pigeoncam.viewportHeight / 2 + (20 / RadiationPigeon.PPM)),

    }, false);

0 个答案:

没有答案