我希望为2D游戏创建平滑的路径。看CatmullRomSpline
这就是我需要的东西。每个帖子,即使是SE
上的帖子,也会给它一个类型并传递所有控制点,并使用构造函数传递Boolean
。这似乎已经过时了,CatmullRomSpline
不再接受任何类型参数,如果没有它,它只能用于V3
路径。构造函数也不接受控制点列表。
cp = new Vector2[]
{
new Vector2(0,100), new Vector2(100,600), new Vector2(300,300), new Vector2(600, 400)
};
CatmullRomSpline<Vector2> path = new CatmullRomSpline<Vector2>(cp, true);
这会出现以下错误:The type CatmullRomSpline is not generic; it cannot be parameterized with arguments <Vector2>
。
我错过了什么,或者现在CatmullRomSpline的工作方式有何不同,以及如何?
这是badlogic的CatmullRomSpline类。肯定看起来事情发生了变化,我从“import com.badlogic.gdx.math.CatmullRomSpline;”获得这个课程。
公共类CatmullRomSpline实现Serializable {private static final long serialVersionUID = -3290464799289771451L;私人的 List controlPoints = new ArrayList(); Vector3 T1 = new Vector3(); Vector3 T2 = new Vector3();
/ **添加一个新的控制点* * @param指向该点* / public void add(Vector3 point){controlPoints.add(point); }
/ ** @return所有控制点* /公共列表 getControlPoints(){return controlPoints; }
/ **返回numPoints每两个控制点之间的路径 生成并且控制点本身也被添加。 * 第一个和最后一个控制点被省略。如果小于4 控制点返回一个空路径。 * * @param numPoints 为段返回的点数* @return路径* / public List getPath(int numPoints){ArrayList points = new ArrayList();
if (controlPoints.size() < 4) return points; Vector3 T1 = new Vector3(); Vector3 T2 = new Vector3(); for (int i = 1; i <= controlPoints.size() - 3; i++) { points.add(controlPoints.get(i)); float increment = 1.0f /
(numPoints + 1); float t = increment;
T1.set(controlPoints.get(i + 1)).sub(controlPoints.get(i -
1))MUL(0.5F)。 T2.set(controlPoints.get(i + 2))子(controlPoints.get(i))的MUL(0.5F);
for (int j = 0; j < numPoints; j++) { float h1 = 2 * t * t * t - 3 * t * t + 1; // calculate basis // function 1 float h2 = -2 * t * t * t + 3 * t * t; // calculate basis // function 2 float h3 = t * t * t - 2 * t * t + t; // calculate basis // function 3 float h4 = t * t * t - t * t; // calculate basis function 4 Vector3 point = new Vector3(controlPoints.get(i)).mul(h1); point.add(controlPoints.get(i + 1).tmp().mul(h2)); point.add(T1.tmp().mul(h3)); point.add(T2.tmp().mul(h4)); points.add(point); t += increment; } } if (controlPoints.size() >= 4)
points.add(controlPoints.get(controlPoints.size() - 2));
return points; }
/ **返回numPoints每两个控制点之间的路径 生成并且控制点本身也被添加。 * 第一个和最后一个控制点被省略。如果小于4 控制点返回一个空路径。 * * @param指出 用于在* @param numPoints中存储路径的Vector3实例数组 为段* / public void getPath返回的点数 (Vector3 [] points,int numPoints){int idx = 0;如果 (controlPoints.size()&lt; 4)return;
for (int i = 1; i <= controlPoints.size() - 3; i++) { points[idx++].set(controlPoints.get(i)); float increment = 1.0f
/(numPoints + 1); float t = increment;
T1.set(controlPoints.get(i + 1)).sub(controlPoints.get(i -
1))MUL(0.5F)。 T2.set(controlPoints.get(i + 2))子(controlPoints.get(i))的MUL(0.5F);
for (int j = 0; j < numPoints; j++) { float h1 = 2 * t * t * t - 3 * t * t + 1; // calculate basis // function 1 float h2 = -2 * t * t * t + 3 * t * t; // calculate basis // function 2 float h3 = t * t * t - 2 * t * t + t; // calculate basis // function 3 float h4 = t * t * t - t * t; // calculate basis function 4 Vector3 point = points[idx++].set(controlPoints.get(i)).mul(h1); point.add(controlPoints.get(i + 1).tmp().mul(h2)); point.add(T1.tmp().mul(h3)); point.add(T2.tmp().mul(h4)); t += increment; } } points[idx].set(controlPoints.get(controlPoints.size() - 2)); }
/ **返回路径中各点的所有切线。与语义相同 的getPath。 * * @param numPoints为a返回的点数 segment * @return路径* / public中的点的切线 List getTangents(int numPoints){ArrayList tangents = new ArrayList();
if (controlPoints.size() < 4) return tangents; Vector3 T1 = new Vector3(); Vector3 T2 = new Vector3(); for (int i = 1; i <= controlPoints.size() - 3; i++) { float
increment = 1.0f /(numPoints + 1); float t = increment;
T1.set(controlPoints.get(i + 1)).sub(controlPoints.get(i -
1))MUL(0.5F)。 T2.set(controlPoints.get(i + 2))子(controlPoints.get(i))的MUL(0.5F);
tangents.add(new Vector3(T1).nor()); for (int j = 0; j < numPoints; j++) { float h1 = 6 * t * t - 6 * t; // calculate basis function 1 float h2 = -6 * t * t + 6 * t; // calculate basis function 2 float h3 = 3 * t * t - 4 * t + 1; // calculate basis function 3 float h4 = 3 * t * t - 2 * t; // calculate basis function 4 Vector3 point = new Vector3(controlPoints.get(i)).mul(h1); point.add(controlPoints.get(i + 1).tmp().mul(h2)); point.add(T1.tmp().mul(h3)); point.add(T2.tmp().mul(h4)); tangents.add(point.nor()); t += increment; } } if (controlPoints.size() >= 4) tangents.add(T1.set(controlPoints.get(controlPoints.size() -
1))。sub(controlPoints.get(controlPoints.size() - 3)) .mul(0.5F).cpy()也不());
return tangents; }
/ **返回a中各点的2D空间中所有切线的法线 路径。控制点必须位于此*的x / y平面中 工作。与getPath相同的语义。 * * @param numPoints的数量 为段返回的点* @返回点的切线 在路径* / public List getTangentNormals2D(int numPoints){ArrayList tangents = new ArrayList();
if (controlPoints.size() < 4) return tangents; Vector3 T1 = new Vector3(); Vector3 T2 = new Vector3(); for (int i = 1; i <= controlPoints.size() - 3; i++) { float
increment = 1.0f /(numPoints + 1); float t = increment;
T1.set(controlPoints.get(i + 1)).sub(controlPoints.get(i -
1))MUL(0.5F)。 T2.set(controlPoints.get(i + 2))子(controlPoints.get(i))的MUL(0.5F);
Vector3 normal = new Vector3(T1).nor(); float x = normal.x; normal.x = normal.y; normal.y = -x; tangents.add(normal); for (int j = 0; j < numPoints; j++) { float h1 = 6 * t * t - 6 * t; // calculate basis function 1 float h2 = -6 * t * t + 6 * t; // calculate basis function 2 float h3 = 3 * t * t - 4 * t + 1; // calculate basis function 3 float h4 = 3 * t * t - 2 * t; // calculate basis function 4 Vector3 point = new Vector3(controlPoints.get(i)).mul(h1); point.add(controlPoints.get(i + 1).tmp().mul(h2)); point.add(T1.tmp().mul(h3)); point.add(T2.tmp().mul(h4)); point.nor(); x = point.x; point.x = point.y; point.y = -x; tangents.add(point); t += increment; } } return tangents; }
/ **使用切线返回切线的法线并提供 矢量做十字产品。 * * @param numPoints的数量 每段的点数* @param up up vector * @return列表 正切法线* / public List getTangentNormals(int numPoints,Vector3 up){List tangents = getTangents(为NumPoints); ArrayList normals = new 的ArrayList();
for (Vector3 tangent : tangents) normals.add(new
的Vector3(正切).crs(向上).nor());
return normals; }
public List getTangentNormals(int numPoints,List up){List tangents = getTangents(numPoints); ArrayList normals = new ArrayList();
int i = 0; for (Vector3 tangent : tangents) normals.add(new
的Vector3(正切).crs(up.get(I ++)),也不());
return normals; } }