java - LibGDX Vector3 Lerp 问题

标签 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 new Vector3(5,0,5) vector :

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

它仍然会导致问题。同样的事情发生了!我很困惑。

最佳答案

我认为你的问题是这样的:

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

您正在从位置更新到目的地,在每一帧更新位置之后,因此在下一帧上,您实际上是从新位置到结尾进行插值。 (因此,当您离目的地越近时,您要插入的位置之间的差异就越小。)

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

关于java - LibGDX Vector3 Lerp 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27956728/

相关文章:

java - Selenium:如何在不包含选项的情况下查找具有特定文本的元素

c++ - std::vector 的 setter

java - Libgdx 滑动暂停菜单

libgdx - 在 libgdx 中加载纹理图的区域会加载整个文件

java - 如何使用robotium调用另一个类中的方法

java - SwingWorker 中的 thread.sleep()

java - 将递归结果添加到 ArrayList

c++ - 如果使用 vector 在叠瓦式自身容器的析构函数中删除 char* 成员,则会崩溃

c++ - 多维动态内存 vector c++

java - 为什么原生 libmpg123 在带有 libgdx 的 Android 上花费这么长时间?