Ooo*_*Ooo 4 java math curve libgdx catmull-rom-curve
我一直在寻找一个二维平面上的物体,以遵循由几个控制点定义的平滑曲线.从我发现的,我正在寻找一个Catmull-Rom-Spline.
我一直在使用LibGDX作为我的项目,它有自己的Catmull-Rom-Spline实现,但是我无法解决它的工作原理,因为我很难找到实现Catmull的文档或其他源代码 - 使用LibGDX的Rom-Splines.
我正在寻找LibGDX Catmull-Rom-Spline实现的解释或者实现使用Catmull-Rom-Splines或其他方法实现控制点的平滑路径的另一种方法.我正在寻找的是能够生成路径并传回该路径上某点的x和y坐标.如果有人有任何建议或指示,将不胜感激.谢谢.
Xop*_*ppa 10
libgdx Path类(包括CatmullRomSpline)适用于2D和3D.因此,在创建CatmullRomSpline时,您必须指定要使用的Vector(Vector2或Vector3):
CatmullRomSpline<Vector2> path = new CatmulRomSpline<Vector2> ( controlpoints, continuous );
Run Code Online (Sandbox Code Playgroud)
例如:
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
Vector2 cp[] = new Vector2[]{
new Vector2(0, 0), new Vector2(w * 0.25f, h * 0.5f), new Vector2(0, h), new Vector2(w*0.5f, h*0.75f),
new Vector2(w, h), new Vector2(w * 0.75f, h * 0.5f), new Vector2(w, 0), new Vector2(w*0.5f, h*0.25f)
};
CatmullRomSpline<Vector2> path = new CatmullRomSpline<Vector2>(cp, true);
Run Code Online (Sandbox Code Playgroud)
现在,您可以使用valueAt方法获取路径上的位置(范围从0到1):
Vector2 position = new Vector2();
float t = a_vulue_between_0_and_1;
path.valueAt(position, t);
Run Code Online (Sandbox Code Playgroud)
例如:
Vector2 position = new Vector2();
float t = 0;
public void render() {
t = (t + Gdx.graphics.getDeltaTime()) % 1f;
path.valueAt(position, t);
// Now you can use the position vector
}
Run Code Online (Sandbox Code Playgroud)
这是一个例子:https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/PathTest.java