O'Reilly logo

3D Graphics with XNA Game Studio 4.0 by Sean James

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required