Menu:

¯
RSS  RSS 2.0

Tweening along the path

Some time ago Zeh Fernando, author of Tweener, made great post demonstrating how Bezier curves support can be used for tweening things along the path. However, Bezier spline does not pass through most of its control points, and so specifying the path may be a bit tricky. One solution to that would be to implement Catmull-Rom spline, but we do not really have to do all that from scratch. Instead, we can have some fun doing math.

A thing to note is that Catmull-Rom spline is C¹ continuous, which means that it is actually Bezier spline constrained to have no discontinuities in the tangent magnitude. Therefore, we can use this to find Bezier control points from Catmull-Rom control points, that are actually raw path data, and so reuse existing Tweener code. I have posted code example on Tweener mailing list, so if you're here looking for code, you may stop reading and go there. If you want to know how it's done, start by looking at spline expressions in matrix form:


Bezier: [ 1 0 0 0 ] [ P1 ] [ -3 3 0 0 ] [ P2 ] f(t) = [1, t, t², t³] [ 3 -6 3 0 ] [ P3 ] [ -1 3 -3 1 ] [ P4 ] Catmull-Rom: [ 0 2 0 0 ] [ P1 ] [ -1 0 1 0 ] [ P2 ] f(t) = [1, t, t², t³] · 0.5 · [ 2 -5 4 -1 ] [ P3 ] [ -1 3 -3 1 ] [ P4 ]

The relationship between two is also illustrated in the picture above for your convenience :) Solving obvious linear equation for Bezier control points, we find:

                 [ P1 ]     [  0  6  0  0 ] [ P1 ]
                 [ P2 ]   1 [ -1  6  1  0 ] [ P2 ]
                 [ P3 ] = - [  0  1  6 -1 ] [ P3 ]
                 [ P4 ]   6 [  0  0  6  0 ] [ P4 ]

This equation is almost enough to find most of Bezier control points for your path. To complete the task, we need to extrapolate our path one step on each side. Ways of doing so are all up to you. If you do not have any idea about that, simply repeat endpoints (or, in 3D case, use __extrav function from my old Pipe code).

For other cubic splines relation to Bezier, check great '98 article by Sean Baxter.