Game Development Reference
In-Depth Information
int p1 ¼ LimitPoints(interval);
int p2 ¼ LimitPoints(interval þ 1);
int p3 ¼ LimitPoints(interval þ 2);
// Scale t to the current segment
double scaledT ¼ (t - _segmentSize * (double)interval) / _segmentSize;
return CalculateCatmullRom(scaledT, _points[p0], _points[p1],
_points[p2], _points[p3]);
}
private Vector CalculateCatmullRom(double t, Vector p1, Vector p2,
Vector p3, Vector p4)
{
double t2 ¼ t*t;
double t3 ¼ t2 * t;
double b1 ¼ 0.5 * (-t3 þ 2 * t2 - t);
double b2 ¼ 0.5*(3*t3-5*t2 þ 2);
double b3 ¼ 0.5 * (-3 * t3 þ 4*t2 þ t);
double b4 ¼ 0.5 * (t3 - t2);
return (p1 * b1 þ p2 * b2 þ p3 * b3 þ p4 * b4);
}
}
This spline class is very simple to use. Any number of points can be added
and the spline will join them together. The line is indexed from 0 to 1; a
position on the line of 0.5 will return whatever point in space the middle of the
line crosses. This makes the spline very easy to use with the earlier tween
class. The spline requires all control points to be evenly spaced to give uniform
values of t.
Each enemy is given a new Path class that will guide it across the level. This
Path class is specific to the shooting game and should be created in the Shooter
project.
public class Path
{
Spline _spline ¼ new Spline();
Tween _tween;
public Path(List < Vector > points, double travelTime)
{
 
Search WWH ::




Custom Search