Building a Race Track from a Curve

Our method of Catmull-Rom curve interpolation works well for moving objects along a smooth path, but can also be used for many other things. In this example, we will build a race track from a curve and have a car follow that path at the player's command. The RaceTrack class will take care of creating and rendering the track:

public class RaceTrack
{
// List of control points
List<Vector2> positions;
// Vertex and index buffers
VertexBuffer vb;
IndexBuffer ib;
int nVertices, nIndices;
// Rendering variables
GraphicsDevice graphicsDevice;
BasicEffect effect;
Texture2D texture;
// Total length of the track
float trackLength;
}

Note that we store the positions as a list of instances of Vector2, as the track is flat ...

Get 3D Graphics with XNA Game Studio 4.0 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.