【问题标题】:LibGDX Vector3 Lerp issueLibGDX Vector3 Lerp 问题
【发布时间】:2015-01-15 13:43:17
【问题描述】:

我正在使用 LibGDX 并试图在两个 Vector3s 之间徘徊......但是,当我这样做时,它不是线性的,它会以指数方式缓和它。我不想要这个,我想要它纯粹的线性!

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

我的更新方法的代码sn-p在这里:

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) 向量:

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

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

【问题讨论】:

    标签: java vector libgdx linear-interpolation


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 是的,你是对的,我也认为它是这样的。但即使我这样做: Vector3 newPosition = this.previousDestination.lerp(this.currentDestination.getPosition(), this.alpha); this.model.transform.setTranslation(newPosition.x, 0, newPosition.z);它不起作用。似乎每次调用 lerp 时都会更新 this.previousDestination,这可能是正常行为?
    • 我认为修改了previousDestination。您可能需要将先前的目的地复制或克隆到适当的位置(也许?)。在您的 println() 中打印当前/上一个/位置以查看它们是否/如何变化...
    • 我已经编辑了我的问题以获得更简单的问题版本。现在是早上 6 点,我明天继续做这个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    相关资源
    最近更新 更多