Catmull Rom Spline - Constant Speed

Given the equations found in the answer here:

Determine arc-length of a Catmull-Rom spline

How would one A) Apply this to a 3D Catmull-Rom Spline, and B) write A out programmatically (for the math illiterate)?

-
The generalisation of the formulas to 3D splines is straightforward; each time you see an expression with `x` and `y`, just add a 3rd term with `z`. – sam hocevar Jan 14 '13 at 7:49
@Sam You could post that as an answer. – Anko Feb 18 '13 at 12:53

There are a few ways to move along at a constant speed along a path whose "segments" are not a constant length - and it's not trivial to make them that way.

I would approach the problem by making a "Mover" of sorts which follows a "Path".

``````public interface Path<T> {
public T getPoint(float delta);
}
public class Mover<T> {
public Path<T> path;
public T position;
public float delta;
public float speed;
public void init() { ... }
public void update(float dt) { ... }
}
``````

(sorry if you haven't learned about generics/templating, just replace T with Vector3)

Most "path" implementations like Catmull-Rom spline can give you a point given a delta value that ranges from 0 to 1. Based off of this the Mover moves along the path looking "lookaheadDelta" ahead and moving towards that point. The smaller this value the smoother the movement, but if it's too small you might make to many path calculations per update.

The update method is important, it tries to move speed*dt units along the path. If the distance between lookahead and the current position is less than this value than you need to iteratively calculate the new lookahead based on the lookaheadDelta and continually move towards it.

It would look something like this:

``````public void update(float dt) {
float move = dt * speed; // units to move
while (move > 0.0) {
// between current position and target
float room = distance( lookahead, position );
// how much we're actually moving this iteration
float actual = Math.min( move, room );
// the normalized vector between lookahead and position
float direction = (lookahead - position) / room;
// move my position accordingly
position += direction * actual;
// update move to be the remaining amount we need to move
move -= actual;
// reset target
if (actual != room) {
}
}
}
``````

And don't forget about initializing it!

``````public void init() {
position = path.getPoint(0.0);