过控制点的数学曲线

Catmull-Rom线,会通过相关的控制点,3D数学里的一种算法。可用在跑酷,赛车之类的路径寻觅。记得在位移路径添加两个辅助点,便于人物顺利位移

CR-Spline需要至少4个控制点,首尾两个控制点为辅助点,曲线不会穿过,其余控制点在t=[0,1]之间曲线会穿过(我喊作路径点),t=0时值等于第2个顶点,而t=1时值等于倒数第2个顶点

 

 
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
);
}
posted @ 2016-10-13 17:05  carsonche  阅读(993)  评论(0编辑  收藏  举报