三次贝塞尔曲线上的恒定速度

时间:2015-11-15 02:31:30

标签: c# unity3d bezier curves cubic

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

我遵循了本教程: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轴运动的点。恒定费率。

结果如下: Cubic Bezier Image

红点表示通过仅使用贝塞尔公式计算原始时间而返回的法线点。黄点代表常数&#39;经过近似时间后的速度位置。在我开始将切线变得相当夸张之前,它似乎非常准确。我也试过增加间隔时间并没有任何帮助。

无论如何,任何帮助都会很精彩。我还没有非常擅长阅读公式(我确定问题的来源),所以请使用代码示例获得一些帮助会很棒。产品:&gt;

谢谢!

2 个答案:

答案 0 :(得分:0)

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

这不是你提出的要求,但要提高效率,以防万一这是高性能游戏的一部分,其中包含大量图形和严格的性能要求

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

b)使用二分搜索或线性估计正确间隔的下一个猜测,而不是单步调整值

此外,您还想用C语言而不是Python语言编写它,但显然这是关于Python的。

答案 1 :(得分:-2)

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

TLDR;对于2D曲线,请勿使用弧长来计算目标距离。仅使用水平(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 - Resulting Graph - Constant Time Over Cubic Bezier Curve

相关问题