PS2 Linux Programming
AI Spline Based Racing Car Demo
This application introduced an AI car which traverses round the track under computer control. A Catmull Rom Spline is used to define the motion of the AI car. Control points are positioned on the track and a Catmul Rom Spline is used to interpolate between the control points and defined the position of the car each frame.
In addition to this, different camera views can be defined: a view from behind the user controlled car, a view from behind the AI car, a “helicopter view” from above the track looking at the AI car and a user controlled view point using the analogue stick to move around. The view selection is toggled using the TRIANGLE button on the control pad.
Catmull Rom Spline
The shape of the curve between the control points P1 and P2 is defined by the four control points shown in the diagram above. One of the features of the Catmull Rom spline is that the specified curve will pass through all of the control points. This is a convenient feature when it comes to moving cars round a track since it is possible to ensure that the car does not leave the track. To calculate a point between P1 and P2 on the curve, the two points P0 and P3 on either side of the inner control points are required. The interpolated, or desired point is specified by a value t, that defines the portion of the distance between the two inner control points.
Given the four control points and parameter t, the interpolated point is obtained using the formulae:
This equation is repeated for each of the required dimensions being used. In the case of this application, two dimensions, x and z, require to be interpolated.
While the spline segment is defined using four control points, there may be any number of additional control points to define the curve. This results in a continuous chain of segments, each defined by the two control points that form the endpoints of the segments, plus the additional control point on either side of the endpoints. It is also possible to create a continuous loop, (as required in a race track) by making the fist and last point in the chain of control points be the same point in space.
The Example Code
In the example code a spline class has been created to handle the interpolation along the spline. Control points are positioned judiciously on the track, this information being hard coded into splines.cpp. Within the splice class, methods are available to interpolate the AI car along the splice and position the car so that it is tangential to the spline. This gives the impression of the car moving and turning smoothly round the track.
As the car moves round the track, the control points are traversed and the appropriate four control points are used to interpolate the spline and define the position and orientation of the car.
Running The Applications
On starting the application, the user car is controlled with the control pad – CROSS to move, left and right DPADS to turn. Press the TRIANGLE to change the camera view. The camera view will toggle between the AI car, Player car, helicopter camera, and user controlled camera. For the user controlled camera, the analogue sticks control the position and orientation of the camera.
This application illustrates the use of a Catmull Rom spline to control the movement of an AI car round a track. The use of various camera views in a games application is also introduced.
Dr Henry S Fortuna
University of Abertay Dundee