LibGDX Vector3 Lerp问题

时间:2015-01-15 04:16:25

标签: java vector libgdx linear-interpolation

我正在使用LibGDX并尝试在两个Vector3之间进行渲染...但是,当我这样做时,它不是线性的,它以指数方式缓和它。我不想要这个,我希望它纯粹是线性的!

这样我的模型可以遵循一组Vector3,就像跟随路径一样。

我的更新方法的代码段在这里:

public void update(float delta) {
    if (!this.pathQueue.isEmpty() && this.currentDestination == null) {
        this.currentDestination = this.pathQueue.poll();
        this.alpha = 0;
    }

    Vector3 position = new Vector3();
    position = this.model.transform.getTranslation(position);

    if (this.currentDestination != null) {
        this.alpha += this.speed * delta; 
        if (this.alpha >= 1) {
            this.currentDestination = this.pathQueue.poll();
            this.alpha = 0;
        }
        System.out.println(alpha);

        //position.interpolate(this.currentDestination.getPosition(), this.alpha, Interpolation.linear);
        position.lerp(this.currentDestination.getPosition(), this.alpha);
        //I have tried interpolate and lerp, same effect.
        this.model.transform.setTranslation(position.x, 0, position.z);
    }
}

谢谢!

编辑:

我已经将我的代码更改为更简单的问题,并使用了一个FIXED新的Vector3(5,0,5)向量:

    public void update(float delta) {

    if (!this.pathQueue.isEmpty() && this.currentDestination == null) {
        this.currentDestination = this.pathQueue.poll();
        this.alpha = 0;
    }

    if (this.currentDestination != null) {
        this.alpha += this.speed * delta; 

        this.currentPosition.lerp(new Vector3(5,0,5), this.alpha);
        this.model.transform.setTranslation(this.currentPosition.x, 0, this.currentPosition.z);
    }
}

它仍会导致问题。同样的事情发生!我很高兴。

1 个答案:

答案 0 :(得分:3)

我认为你的问题是:

position.lerp(this.currentDestination.getPosition(), this.alpha);

您正在更新位置,在更新位置的每一帧之后,所以在下一帧,您有效地从新位置插入到结尾。 (因此,当您靠近目的地时,您之间的位置之间的差异会更小。)

我认为您正确更新alpha,因此您希望在每个帧上从开头到目的地进行插值。所以,我认为重置"位置"在开始和结束之间插值之前的向量应该给你你想要的行为。

相关问题