libgdx vector3 lerp问题

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

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

  1. public void update(float delta) {
  2. if (!this.pathQueue.isEmpty() && this.currentDestination == null) {
  3. this.currentDestination = this.pathQueue.poll();
  4. this.alpha = 0;
  5. }
  6. Vector3 position = new Vector3();
  7. position = this.model.transform.getTranslation(position);
  8. if (this.currentDestination != null) {
  9. this.alpha += this.speed * delta;
  10. if (this.alpha >= 1) {
  11. this.currentDestination = this.pathQueue.poll();
  12. this.alpha = 0;
  13. }
  14. System.out.println(alpha);
  15. //position.interpolate(this.currentDestination.getPosition(), this.alpha, Interpolation.linear);
  16. position.lerp(this.currentDestination.getPosition(), this.alpha);
  17. //I have tried interpolate and lerp, same effect.
  18. this.model.transform.setTranslation(position.x, 0, position.z);
  19. }
  20. }

谢谢!
编辑:
我将代码改为更简单的问题,并使用一个固定的新向量3(5,0,5)向量:

  1. public void update(float delta) {
  2. if (!this.pathQueue.isEmpty() && this.currentDestination == null) {
  3. this.currentDestination = this.pathQueue.poll();
  4. this.alpha = 0;
  5. }
  6. if (this.currentDestination != null) {
  7. this.alpha += this.speed * delta;
  8. this.currentPosition.lerp(new Vector3(5,0,5), this.alpha);
  9. this.model.transform.setTranslation(this.currentPosition.x, 0, this.currentPosition.z);
  10. }
  11. }

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

v1uwarro

v1uwarro1#

我认为你的问题是:

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

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

相关问题