我要解决的问题是我似乎无法以恒定速度沿三次贝塞尔曲线移动 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 轴以恒定速率移动。
结果是这样的: 三次贝塞尔图像
红点表示仅使用贝塞尔公式评估原始时间返回的法线点。黄点代表经过近似时间后的“恒定”速度位置。在我开始将切线更改为相当夸张之前,它似乎非常准确。我也尝试过增加间隔,但这无济于事。
无论如何,任何帮助都会很棒。我还不是很擅长阅读公式(我确定问题出在哪里),所以请使用代码示例获得一些帮助会很棒。:>
谢谢!