过控制点的数学曲线
Catmull-Rom线,会通过相关的控制点,3D数学里的一种算法。可用在跑酷,赛车之类的路径寻觅。记得在位移路径添加两个辅助点,便于人物顺利位移
CR-Spline需要至少4个控制点,首尾两个控制点为辅助点,曲线不会穿过,其余控制点在t=[0,1]之间曲线会穿过(我喊作路径点),t=0时值等于第2个顶点,而t=1时值等于倒数第2个顶点
![](https://images2015.cnblogs.com/blog/1001900/201610/1001900-20161009113814273-1325740286.png)
private Vector3 Interp(Vector3[] pts , float t)
{
t = Mathf.Clamp(t,0.0f,2.0f);
int numSections = pts.Length - 3;
int currPt = Mathf.Min(Mathf.FloorToInt(t * numSections), numSections - 1);
float u = t * numSections - currPt;
Vector3 a = pts[currPt];
Vector3 b = pts[currPt + 1];
Vector3 c = pts[currPt + 2];
Vector3 d = pts[currPt + 3];
return .5f * (
(-a + 3f * b - 3f * c + d) * (u * u * u)
+ (2f * a - 5f * b + 4f * c - d) * (u * u)
+ (-a + c) * u
+ 2f * b
);
}
如果您觉得本文对你有用,不妨帮忙点个赞,或者在评论里给我一句赞美,小小成就都是今后继续为大家编写优质文章的动力,carson拜谢! 欢迎您持续关注我的博客:)
作者:carsonche
出处:https://home.cnblogs.com/u/carsonche/
版权所有,欢迎保留原文链接进行转载:)