The libgdx Path class classes (including CatmullRomSpline) are suitable for both 2D and 3D. Therefore, when creating CatmullRomSpline, you must specify which vector (Vector2 or Vector3) to use:
CatmullRomSpline<Vector2> path = new CatmulRomSpline<Vector2> ( controlpoints, continuous );
For instance:
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);
Now you can get the location on the path (from 0 to 1) using the valueAt method:
Vector2 position = new Vector2(); float t = a_vulue_between_0_and_1; path.valueAt(position, t);
For instance:
Vector2 position = new Vector2(); float t = 0; public void render() { t = (t + Gdx.graphics.getDeltaTime()) % 1f; path.valueAt(position, t);
Here is an example: https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/PathTest.java
Xoppa source share