Game Development Reference
In-Depth Information
// Find the speed corresponding to the random
// number using the Gaussian distribution with a
// mean value of 0 and a standard deviation of 1.
t = Math.sqrt( Math.log(1.0/(x*x)) );
grp1 = 2.515517 + 0.802853*t + 0.010328*t*t;
grp2 = 1.0 + 1.432788*t + 0.189269*t*t +
0.001308*t*t*t;
speed = -t + grp1/grp2;
Equation (15.3) assumes that the mean value is equal to 0 and the standard deviation is
equal to 1. The computed speed is corrected to the user-specified mean and standard devia-
tion values using Equation (15.4). The
setSpeed
method is then called to update the value of the
speed
field in each
Soldier
object. Because the speeds are determined randomly using the
Gaussian probability function, each
Soldier
object will have a different
speed
value.
// Shift the converted speed to the proper
// mean and standard deviation value.
speed = mean + speed*sigma;
// Update the value of the speed field for
// each Soldier object.
soldier[j].setSpeed(speed);
The soldiers are constrained to only move vertically, so the y-location of each
Soldier
object is updated based on the previous y-location and the speed of the
Soldier
object. The
time step,
dt
, is set to be equal to 1 second. Once the y-locations have been updated, the GUI
display is updated to show the new positions of each
Soldier
object.
// Update the y-location of each soldier.
// If they reach the bottom of the panel, they stop.
newY = soldier[j].getYLocation() + dt*soldier[j].getSpeed();
if ( newY > drawingPanel.getHeight() - 1 ) {
newY = drawingPanel.getHeight() - 1;
}
soldier[j].setYLocation(newY);
}
// Update the display.
updateDisplay();
As with some of the other example games in this topic, when you first run the Soldier Game,
you may have to press the Reset button to get the GUI properly rendered. Press the Start button
and see what happens. The individual soldiers will move down the screen independently. Try
changing the value of the standard deviation and see what happens. Increasing the standard
deviation will increase the range of speeds that the soldiers are likely to have, resulting in a
more chaotic charge. Lowering the standard deviation value essentially instills more discipline
in the army in that soldiers will tend to run at more or less the same speed.