确保值在360度环绕时正确插值

时间:2019-06-19 07:16:49

标签: javascript typescript math

我具有以下Lerp功能

  public static headingPitchRollLerp(v1: HeadingPitchRoll, v2: HeadingPitchRoll, t: number): HeadingPitchRoll {
    Math.min(Math.max(t, 0), 1);
    const result = new Cesium.HeadingPitchRoll();
    result.heading = v1.heading + (v2.heading - v1.heading) * t;
    result.pitch = v1.pitch + (v2.pitch - v1.pitch) * t;
    result.roll = v1.roll + (v2.roll - v1.roll) * t;
    return result;
  }

当旋转不超过360'时效果很好。

但是,例如,我的标题为350',而我的v2的标题为10',而不是从350'到10'(仅20'),我的代码移回并重新旋转了一个完整的角度(340 ')。

我可以更改些什么以使旋转始终保持最小?

2 个答案:

答案 0 :(得分:1)

与问题无关,但据我所知

Math.min(Math.max(t, 0), 1);

实际上并没有更改“ t”的值。有必要重新分配

t = Math.min(Math.max(t, 0), 1);

答案 1 :(得分:0)

我修复了以下问题

  public static headingPitchRollLerp(v1: HeadingPitchRoll, v2: HeadingPitchRoll, t: number): HeadingPitchRoll {
    Math.min(Math.max(t, 0), 1);
    const result = new Cesium.HeadingPitchRoll();

    const v1H = ((v2.heading - v1.heading) > Math.PI) ? v1.heading += 2 * Math.PI : ((v2.heading - v1.heading) < -Math.PI) ? v1.heading -= 2 * Math.PI : v1.heading;
    const v1P = ((v2.pitch - v1.pitch) > Math.PI) ? v1.pitch += 2 * Math.PI : ((v2.pitch - v1.pitch) < -Math.PI) ? v1.pitch -= 2 * Math.PI : v1.pitch;
    const v1R = ((v2.roll - v1.roll) > Math.PI) ? v1.roll += 2 * Math.PI : ((v2.roll - v1.roll) < -Math.PI) ? v1.roll -= 2 * Math.PI : v1.roll;

    result.heading = v1H + (v2.heading - v1H) * t;
    result.pitch = v1P + (v2.pitch - v1P) * t;
    result.roll = v1R + (v2.roll - v1R) * t;
    return result;
  }