CatmullRomSplines and Other Smooth Paths

I was looking for an object on a two-dimensional plane to follow a smooth curve defined by several control points. From what I found, I am looking for Kathmullah Rum Spline .

I use LibGDX for my project, and it has its own implementation of Catmull-Rom-Spline, but I have problems with how it works, because I had problems finding documentation or other source code implementing Catmull-Rom-Splines with using libgdx.

I’m either looking for an explanation of the LibGDX Catmull-Rom-Spline implementation, or another way to implement a smooth path that implements breakpoints using Catmull-Rom-Splines or another method. All I'm looking for is the ability to generate a path and transmit the x and y coordinates of a point along that path. If anyone has suggestions or pointers, this will be appreciated. Thank you

+4
source share
1 answer

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); // Now you can use the position vector } 

Here is an example: https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/PathTest.java

+10
source

Source: https://habr.com/ru/post/1500770/


All Articles