三次贝塞尔曲线上的等速

本文关键字:曲线 三次 | 更新日期: 2023-09-27 18:21:54

我试图解决的问题是,我似乎无法以恒定的速度沿着三次贝塞尔曲线移动2D点。

我遵循了本教程:http://catlikecoding.com/unity/tutorials/curves-and-splines/最初实现了曲线,效果非常好。但当试图以恒定的速度接近该点时,它离目标很远

根据我到目前为止所读到的内容,你应该在曲线上迭代,计算每一步的弧长和间隔距离。然后,将这些距离与目标距离(弧长*时间)进行比较,以找到最接近的距离。有了足够高的分辨率,这应该不会有什么错误,而且足够准确,满足我的需求。

这是我迄今为止的代码:

点计算:

public static Vector3 GetPoint (Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) 
{
    t = Mathf.Clamp01(t);
    float oneMinusT = 1f - t;
    return
        oneMinusT * oneMinusT * oneMinusT * p0 +
            3f * oneMinusT * oneMinusT * t * p1 +
            3f * oneMinusT * t * t * p2 +
            t * t * t * p3;
}

在恒定时间计算点的可悲尝试:

private float GetApproximatedTime(float u)
{
    int resolution = 100;
    float ratio = 1.0f / resolution;
    float arcLength = 0.0f;
    Vector3 p0 = SelectedSpline.Evaluate(0.0f);
    List<MultiCurveUtility.ArcTimeLength> arcTimeLengthMap = new List<MultiCurveUtility.ArcTimeLength>();
    arcTimeLengthMap.Add(new MultiCurveUtility.ArcTimeLength(0.0f, 0.0f));
    for (int i = 1; i <= resolution; i++)
    {
        float t = ((float)i) * ratio;
        Vector3 p1 = SelectedSpline.Evaluate(t);
        arcLength += Vector3.Distance(p0, p1);
        arcTimeLengthMap.Add(new MultiCurveUtility.ArcTimeLength(t, arcLength));
        p0 = p1;
    }
    float target = u * arcLength;
    int low = 0;
    int high = 1;
    float min = 0.0f;
    float max = 0.0f;
    for (int i = 1; i < arcTimeLengthMap.Count; i++)
    {
        max = arcTimeLengthMap[i].ArcLength;
        if (target > min && target < max) 
        {
            high = i;
            low = i - 1;
            break; 
        }
        min = max;
    }
    float p = (target - min) / (max - min);
    float lowTime = arcTimeLengthMap[low].ArcTime;
    float highTime = arcTimeLengthMap[high].ArcTime;
    float lowHighDelta = highTime - lowTime;
    return arcTimeLengthMap[low].ArcTime + (lowHighDelta * p);
}

在上面的代码中,我输入了0和1之间的时间(u),以便返回一个时间,该时间可用于评估三次贝塞尔曲线上的一个点,该曲线表示以恒定速率进行的x轴移动。

结果是:三次贝塞尔图像

红点表示通过使用bezier公式计算原始时间返回的法线点。黄点表示经过近似时间后的"恒定"速度位置。它看起来相当准确,直到我开始把切线改得相当夸张。我也试过增加间隔时间,但没有任何帮助。

不管怎样,任何帮助都将是美妙的。我还不太擅长阅读公式(我确定问题是从哪里来的),所以如果能使用代码示例获得一些帮助,那就太好了。:>

谢谢!

三次贝塞尔曲线上的等速

我没有看到这种方法中有任何明显的错误,所以将分辨率提高到1000或10000会有所帮助。

这不是你所要求的,但为了提高效率,以防这是一些高性能游戏的一部分,其中有大量的图形和苛刻的性能要求

a) 将值一步存储在表中,然后在单独的步骤中访问它们,这样对于该曲线,您就不必每次都重新计算100(0(0))点

b) 使用二进制搜索或线性估计正确区间的下一个猜测,而不是逐步遍历值

此外,你可能想用C而不是Python编写它,但很明显,这是关于Python的。

好吧,我似乎自己找到了答案。

TLDR;对于二维曲线,不要使用弧长来计算目标距离。仅使用水平(x轴)长度。

快速注意:如果你的曲线可以沿着x轴向后走,这个解决方案可能对你不起作用。我的没有。

更详细地说,目标距离是时间和弧长的乘积,这个值用来近似我应该在曲线上寻找的位置。弧长不准确,因为它考虑了y轴距离。我只关心水平运动,所以y距离是不必要的。

这是我更新的代码:

private float GetApproximatedTime(float u)
{
int resolution = 25 * SelectedSpline.CurveCount; // Factor in additional curves.
float ratio = 1.0f / resolution;
float arcLength = 0.0f;
Vector3 p0 = SelectedSpline.Evaluate(0.0f);
List<MultiCurveUtility.ArcTimeLength> arcTimeLengthMap = new List<MultiCurveUtility.ArcTimeLength>();
arcTimeLengthMap.Add(new MultiCurveUtility.ArcTimeLength(0.0f, 0.0f));
for (int i = 1; i <= resolution; i++)
{
    float t = ((float)i) * ratio;
    Vector3 p1 = SelectedSpline.Evaluate(t);
    arcLength += Mathf.Abs(p1.x - p0.x); // Only use the x-axis delta.
    arcTimeLengthMap.Add(new MultiCurveUtility.ArcTimeLength(t, arcLength));
    p0 = p1;
}
float target = u * arcLength;
int low = 0;
int high = 1;
float min = 0.0f;
float max = 0.0f;
for (int i = 1; i < arcTimeLengthMap.Count; i++)
{
    max = arcTimeLengthMap[i].ArcLength;
    if (target > min && target < max) 
    {
        high = i;
        low = i - 1;
        break; 
    }
    min = max;
}
float p = (target - min) / (max - min);
float lowTime = arcTimeLengthMap[low].ArcTime;
float highTime = arcTimeLengthMap[high].ArcTime;
float lowHighDelta = highTime - lowTime;
return arcTimeLengthMap[low].ArcTime + (lowHighDelta * p);
}

请注意,这里有两个显著的变化:

arcLength += Mathf.Abs(p1.x - p0.x);

int resolution = 25 * SelectedSpline.CurveCount;

第二个更改是为了确保添加曲线时分辨率不会降低。否则,您可能会注意到返回时间的准确性存在错误。我发现每条曲线间隔25是非常准确和快速的。也就是说,在这段代码中有一些明确的优化,但是,如果你也不能弄清楚这一点,它应该对你有效。

这是结果的屏幕截图。黄点是我使用新时间评估的点。绿点代表我的高点和低点。

IMAGE-结果图-三次贝塞尔曲线上的恒定时间