Game Development Reference
In-Depth Information
As seen in Figure 8.2, our vehicles will be drawn as boxes using Label and TextBox
controls. The position of a vehicle is the leading edge of the box. Inside each box
is a number showing the current speed of the vehicle. The box will have a white
background if the AI did not change speed the last time it thought. If the AI
slowed down, the background will be reddish; if the AI accelerated, the back-
ground will be greenish. Projecting in front of each vehicle is a headlight—a
narrow beam that projects forward two seconds' worth of travel distance at the
current speed. This is similar to the feelers or probes often seen in flocking
demos. The vehicle ignores anything ahead of it beyond the reach of the head-
light. Above each box is the vehicle's name and its desired speed. All vehicles have
a length, in pixels. (The bikes are too long, but a shorter bike body will not hold a
two-digit number.) The sport and the exotic vehicles are the minimum size to
hold a three-digit number.
Alongside the road is a ''pixel marker,'' equivalent to a mile-marker road sign.
You can see it above the scrollbar and below and between Bike B and Truck in
Figure 8.2. When the simulation is running, the display is centered on a reference
vehicle, which is Bike B in Figure 8.2. The marker goes flying by at the equivalent
of the road speed of the reference vehicle. When the marker falls off the left edge,
it is redrawn at the right. The vehicles travel left to right; a wide-format monitor
enables you to see more of them.
Position is stored in absolute pixels. This makes the motion and display math
easy to understand. In each animation frame, the vehicles are moved and then
drawn. For movement, the internal position of the vehicles is updated according
to their speed in pixels per second, and the frame rate in frames per second. Lane
changes take place on AI think time, not animation time. To draw, the three
labels that make up a vehicle have their Top property set according to the selected
lane and their Left property set to a value reflecting the vehicle's relative position
to the reference vehicle. Setting properties of labels and text boxes is rather
rudimentary, but it is sufficient for our animation needs.
Two timers are used to control the AI and the animation. (Do not expect this
method to be commonly used in commercial games.) The AI timer typically
fires every half second to run the AI code. The animation timer typically fires
6 to 12 times a second, depending on the desired frame rate. This gives us
the equivalent of complex, multi-threaded game code without the coding
complexity. Performance will depend on the number of vehicles, but it will also
depend greatly upon whether the code is run in the debugger or as an
Search Nedrilad ::

Custom Search