Example usage for com.badlogic.gdx.math CatmullRomSpline CatmullRomSpline

List of usage examples for com.badlogic.gdx.math CatmullRomSpline CatmullRomSpline

Introduction

In this page you can find the example usage for com.badlogic.gdx.math CatmullRomSpline CatmullRomSpline.

Prototype

public CatmullRomSpline(final T[] controlPoints, final boolean continuous) 

Source Link

Usage

From source file:releasethekraken.path.SeaCreaturePath.java

/**
 * Constructs a new SeaCreaturePath/*from ww  w  .j a  va  2 s.c o  m*/
 * @param id The path's ID, from the level editor
 * @param parent The path that leads to this path, or null if this is the first path
 * @param nextPaths A list of paths that can be gone on after this path.  The list is empty if there are none.
 * @param polyline The Polyline object that holds the path data
 */
public SeaCreaturePath(int id, SeaCreaturePath parent, Array<SeaCreaturePath> nextPaths, Polyline polyline) {
    this.id = id;
    this.parent = parent;
    this.nextPaths = nextPaths;

    final float scale = 1 / 16F; //Scale it down to match the world units

    //Scale the polyline to match the world units.
    //This has to be done manually because Polyline.setScale() doesn't seem to work.
    float[] vertices = polyline.getTransformedVertices();
    for (int i = 0; i < vertices.length; i++)
        vertices[i] *= scale;
    polyline = new Polyline(vertices);

    this.polyline = polyline;

    vertices = polyline.getVertices(); //Get an array of vertices, in x1, y1, x2, y2,... format
    Vector2[] points = new Vector2[vertices.length / 2]; //Make an array of Vector2 objects

    //Convert the vertices to Vector2 objects
    for (int i = 0; i < points.length; i++) {
        float x = vertices[i * 2];
        float y = vertices[(i * 2) + 1];
        points[i] = new Vector2(x, y);
    }

    //TODO: Fix this!
    //Add additional starting and ending points so that all of the original points are used, due to how catmull rom spline works
    Array<Vector2> pointsList = new Array<Vector2>(points);
    Vector2 firstPoint = points[0].cpy().add(points[0].cpy().sub(points[1]));
    pointsList.insert(0, firstPoint);
    Vector2 lastPoint = points[points.length - 1].cpy()
            .add(points[points.length - 1].cpy().sub(points[points.length - 2]));
    pointsList.insert(pointsList.size, lastPoint);

    //Make the smooth path with the points
    this.path = new CatmullRomSpline<Vector2>(pointsList.toArray(), false);

    Gdx.app.log("SCPath " + this.toString(true), "Points: " + Arrays.toString(points));
}