libgdx: Calculate points along CatmullRomSpline that are same distance apart?

685 views Asked by At

I am building a 3D game in which the camera follows a sequence of predefined paths (which are imported from curves drawn in Blender).

At load time, I extract the Blender curve points and use these to create path splines using the CatmullRomSpline class.

Camera path

Screenshot indicates what I mean (I have shifted the camera aside to view the path - light blue boxes represent fixed points along the camera path).

At the moment, I am using Blender to define how many points should be in my imported splines - this in turn determines the speed at which the camera moves along the path (fixed points). This works OK, but there is inconsistency between different splines (different number of control points etc).

What I actually want is the ability to move along the spline by some fixed distance in each "tick", so that the overall camera movement is more consistent.

I've changed my path calculation logic to take the spline length into consideration, and then use a 'step length' to calculate the spline points.

However, I'm still getting inconsistent distances between spline points.

I'm fairly sure I'm missing something obvious - any ideas?

    //duplicate first and last control points
    //ref: http://stackoverflow.com/questions/29198881/drawing-a-catmullromspline-in-libgdx-with-an-endpoint-and-startpoint
    splineDataset[0] = splineDataset[1].cpy();
    splineDataset[splineDataset.length - 1] = splineDataset[splineDataset.length - 2].cpy();

    CatmullRomSpline<Vector3> workingCatMull = new CatmullRomSpline<Vector3>(splineDataset, false);

    //calculate spline length
    trackLength = workingCatMull.approxLength(NUM_SPLINE_SAMPLES_FOR_LENGTH_CALC);

    float DISTANCE_PER_PATH_TICK = 0.01f;

    int numPathTicks = (int)(trackLength / DISTANCE_PER_PATH_TICK);

    for(int i = 0; i < numPathTicks; ++i)
    {
        //calculate spline point
        Vector3 workingVector = new Vector3();
        float splinePercentage = ((float)i) / ((float)numPathTicks-1);
        workingCatMull.valueAt(workingVector, splinePercentage);

        //offset path point by specified value
        workingVector.add(positionOffset);

        //add spline point to camera path
        listOfPathPoints.add(workingVector);
    }
0

There are 0 answers