libgdx vector3 lerp问题

jjhzyzn0  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(227)

我正在使用libgdx并尝试在两个向量之间进行lerp。。。然而,当我这样做的时候,它不是线性的,它以指数的方式缓和它。我不想要这个,我想要纯线性的!
这样我的模型就可以跟踪一组向量,就像跟踪一条路径一样。
我的更新方法的代码片段如下:

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

谢谢!
编辑:
我将代码改为更简单的问题,并使用一个固定的新向量3(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);
    }
}

它仍然会引起问题。同样的事情也会发生!我太吃惊了。

v1uwarro

v1uwarro1#

我认为你的问题是:

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

你正在从一个位置更新到另一个目标,在每一帧你都更新了位置,所以在下一帧,你有效地从你的新位置插值到最后(因此,当您越来越接近目标时,要插值的位置之间的差异就越小。)
我觉得你在更新 alpha 正确,所以您希望在每一帧上从起点到终点进行插值。所以,我认为在开始和结束之间插值之前,将“位置”向量重置为开始应该会给你想要的行为。

相关问题