我希望为我的 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; 私有列表 controlPoints = new ArrayList(); 矢量3 T1 = 新矢量3(); 矢量3 T2 = 新矢量3();
/** 添加一个新的控制点 * * @param point 点 */ public void add (Vector3 point) { controlPoints.add(point); }
/** @return 所有控制点 */ public List 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); 浮动 t = 增量;
T1.set(controlPoints.get(i + 1)).sub(controlPoints.get(i -
1)).mul(0.5f); T2.set(controlPoints.get(i + 2)).sub(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 指向 Vector3 实例的数组以存储路径 * @param numPoints 为段返回的点数 */ public void getPath (Vector3[] points, int numPoints) { int idx = 0; if (controlPoints.size() < 4) 返回;
for (int i = 1; i <= controlPoints.size() - 3; i++) { points[idx++].set(controlPoints.get(i)); float increment = 1.0f
/ (numPoints + 1); 浮动 t = 增量;
T1.set(controlPoints.get(i + 1)).sub(controlPoints.get(i -
1)).mul(0.5f); T2.set(controlPoints.get(i + 2)).sub(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 为段返回的点数 * @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
增量 = 1.0f / (numPoints + 1); 浮动 t = 增量;
T1.set(controlPoints.get(i + 1)).sub(controlPoints.get(i -
1)).mul(0.5f); T2.set(controlPoints.get(i + 2)).sub(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().nor());
return tangents; }
/** 返回路径中点在 2D 空间中的所有切线法线。控制点必须位于 x/y 平面上才能使 * 起作用。与 getPath 的语义相同。* * @param numPoints 为段返回的点数 * @return 路径中点的切线 */ 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
增量 = 1.0f / (numPoints + 1); 浮动 t = 增量;
T1.set(controlPoints.get(i + 1)).sub(controlPoints.get(i -
1)).mul(0.5f); T2.set(controlPoints.get(i + 2)).sub(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 向量 * @return 切线法线列表 */ public List getTangentNormals (int numPoints, Vector3 up) { List tangents = getTangents(numPoints); ArrayList 法线 = 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 法线 = new ArrayList();
int i = 0; for (Vector3 tangent : tangents) normals.add(new
Vector3(切线).crs(up.get(i++)).nor());
return normals; } }