Game Development Reference
In-Depth Information
Implementing the SplineMgr class
Recall from our requirements that our NPC needs to be able to walk about the world
along an arbitrary path. A convenient way to author the curve information would be by
placing waypoints in the scene in the Unity3D Editor. Then, by interpolating between
these points, we can apply smooth motion to the NPC object. It turns out that our
SplineMgr is able to generate a curve that interpolates through all of the waypoints
(or ControlPoints ) in a set. Great news!
There are numerous types of splines in the mathematical world; each one has its own
unique properties. Some of them don't interpolate any but get really close. Others will
interpolate the first and the last, and approximate the middle two. By using a specific
type of spline in our calculations (Catmull-Rom to be precise—named after the sci-
entists who invented the formulation), we can guarantee that our curve will always
interpolate all waypoints; the math inside our GetPointOnCurve() function returns
a point on the Catmull-Rom spline curve.
We can implement the SplineMgr class using the following steps:
1. Our SplineMgr keeps a list of control points / waypoints for processing.
These can be added to this list individually or in a batch via splineNodeRoot
(this is simply an empty GameObject with a collection of control points in its
hierarchy—a convenient way to encapsulate control point data).
2. The SplineMgr class has two types of playback. One of them evaluates the
curve at a constant frame rate, and the other at a constant (more or less) arc
length. The first way is good for curves that need to start and stop at a precise
time. The tradeoff is that the curve may accelerate and decelerate as the con-
trol points move closer and farther away from one another.
3. The second playback mode is useful when the nature of the motion needs to
be of constant velocity. However, the tradeoff with this playback type is that
the total time to evaluate the curve is stretched:
public enum ePlaybackType
{
invalid = -1,
none = 0,
const_dt = 1, //interpolate spline at
Search WWH ::




Custom Search