CatmullRomSplines和其他平滑路径

时间:2013-09-05 16:11:58

标签: java math libgdx curve catmull-rom-curve

我一直在寻找一个二维平面上的物体,以遵循由几个控制点定义的平滑曲线。从我发现的,我正在寻找一个Catmull-Rom-Spline

我一直在使用LibGDX作为我的项目,它有自己的Catmull-Rom-Spline实现,但我无法理解它是如何工作的,因为我在查找文档或其他源代码时遇到了麻烦使用LibGDX实现Catmull-Rom-Splines。

我正在寻找LibGDX Catmull-Rom-Spline实现的解释或者实现使用Catmull-Rom-Splines或其他方法实现控制点的平滑路径的另一种方法。我正在寻找的是能够生成路径并传回该路径上某点的x和y坐标。如果有人有任何建议或指示,将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:10)

libgdx Path类(包括CatmullRomSpline)适用于2D和3D。因此,在创建CatmullRomSpline时,您必须指定要使用的Vector(Vector2或Vector3):

CatmullRomSpline<Vector2> path = new CatmulRomSpline<Vector2> ( controlpoints, continuous );

例如:

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);

现在,您可以使用valueAt方法获取路径上的位置(范围从0到1):

Vector2 position = new Vector2();
float t = a_vulue_between_0_and_1;
path.valueAt(position, t);

例如:

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
}

以下是一个示例:https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/PathTest.java