Game Development Reference
In-Depth Information
// restore values
playbackMode = pbm;
Playbacktype = pbt;
vOut = 0.5f * ((2.0f*p1) +
(-p0+p2)*t + (2.0f*p0 - 5.0f*p1 +
4.0f*p2 - p3)*t2 + (-p0 + 3.0f*p1 -
3.0f*p2 + p3)*t3);
Tip
The
PointOnCurve()
method is the workhorse of the
SplineMgr
class. It takes four control points as input (
p0
,
p1
,
p2
,
p3
), and given a value of
t
from (0, 1), it returns a point on
the curve. The polynomial equation in this function is derived from
the Catmull-Rom spline basis matrix (other splines would have a
different formula for computing a point on the curve; the important
point is that our formula derived from that matrix).
9. In the
FixedUpdate()
method, we call a custom
eval()
method, which
evaluates the spline. We call this during
FixedUpdate()
to simplify the
t
calculations in
PointOnCurve()
, eliminating the need to multiply by
elapsed time. Had we decided to evaluate the spline during
Update()
, we
would need to evaluate the curve on a point that was also a factor of the
elapsed time:
// Update is called once per frame
void FixedUpdate ()
{
if ((playbackMode !=
ePlaybackMode.computingDebugPath) &&
(playbackMode != ePlaybackMode.none))
eval ();
}