2

I am trying to interpolate between 4 points using a Hermite spline. However my spline seems to always start on the second point and only interpolate to the 3rd point. I have tried this with several differnt calculations and keep getting the same result.

Can anyone give me insight on this? Here is my code.

public ControlPoint Get(float t)
{
    //speed multiplyer
    //t = t * 10;

    return new ControlPoint(
        new Vector3(Hermite(points[0].pos.x,  points[1].pos.x, points[2].pos.x, points[3].pos.x, t)
        , Hermite(points[0].pos.y, points[1].pos.y, points[2].pos.y, points[3].pos.y, t)
        , Hermite(points[0].pos.z, points[1].pos.z, points[2].pos.z, points[3].pos.z, t)
        ),
        new Quaternion(Hermite(points[0].rot.x, points[1].rot.x, points[2].rot.x, points[3].rot.x, t)
        , Hermite(points[0].rot.y, points[1].rot.y, points[2].rot.y, points[3].rot.y, t)
        , Hermite(points[0].rot.z, points[1].rot.z, points[2].rot.z, points[3].rot.z, t)
        , Hermite(points[0].rot.w, points[1].rot.w, points[2].rot.w, points[3].rot.w, t)
        )
        );
}

float Hermite(
    float y0, float y1,
    float y2, float y3,
    float mu,
    float tension = 0,
    float bias = 0)
{
    float m0, m1, mu2, mu3;
    float a0, a1, a2, a3;

    mu2 = mu * mu;
    mu3 = mu2 * mu;
    m0 = (y1 - y0) * (1 + bias) * (1 - tension) / 2;
    m0 += (y2 - y1) * (1 - bias) * (1 - tension) / 2;
    m1 = (y2 - y1) * (1 + bias) * (1 - tension) / 2;
    m1 += (y3 - y2) * (1 - bias) * (1 - tension) / 2;
    a0 = 2 * mu3 - 3 * mu2 + 1;
    a1 = mu3 - 2 * mu2 + mu;
    a2 = mu3 - mu2;
    a3 = -2 * mu3 + 3 * mu2;

    return (a0 * y1 + a1 * m0 + a2 * m1 + a3 * y2);
}
4

1 回答 1

0

无论如何,我都不是 Hermite Splines 专家,但据我所见,预期的行为将是在第二点和第三点之间进行插值。在我看来,您只是在Get函数的每个坐标中硬编码,因此当 Hermite Spline 是一个函数时,您只得到一个插值是有道理的。将第二个和第三个点视为您要在其间进行插值的两个点,而第一个和第四个点只是有助于创建更好的曲线。

由于看起来您总共只有四个点,因此要在第一个和第二个点以及第三个和第四个点之间进行插值,请尝试重复您的第一个坐标和最后一个坐标。

//Interpolate between 1st and 2nd points' x coord Hermite(points[0].pos.x, points[0].pos.x, points[1].pos.x, points[2].pos.x);

//Interpolate between 3rd and 4th points' x coord Hermite(points[2].pos.x, points[3].pos.x, points[4].pos.x, points[4].pos.x);

在第一个点和第二个点之间进行插值points[0]重复两次,因为没有points[-1]. 对于第三点和第四点之间的插值,points[4]由于没有points[5].

重申一下,除非您只想要一个插值,否则不要在坐标中进行硬编码。你必须修改你的Get函数并调用它几次来调整你想要的行为。看看 Snea 如何在他的DrawGraph函数中实现 Hermite Spline,它帮助我更好地理解 Hermite Spline 的行为:Cubic Hermite Spline 表现异常

于 2017-07-19T23:39:59.323 回答