Game Development Reference
In-Depth Information
After the constructor, the Car class declares a series of get/set methods to access or change
the value of the fields declared in the class. Only some of the get/set methods are shown here.
Download the complete code listing from the Apress website to see all of the get/set methods.
// These methods return the value of the fields
// declared in this class.
public double getMuR() {
return muR;
}
public double getFinalDriveRatio() {
return finalDriveRatio;
}
// Other get methods not shown ...
public void setOmegaE(double value) {
omegaE = value;
}
// Other set methods not shown ...
One of the features of this car simulation is that you can shift gears. This functionality is
implemented in the shiftGear method. The first thing the method does is to determine whether
the desired shift is outside the possible range of gear numbers, in which case the method
returns. If the shift is possible, the value of the gearNumber field is changed, and the new engine
turnover rate is computed by multiplying the old turnover rate by the ratio of the new gear ratio
to the old gear ratio.
// This method simulates a gear shift.
public void shiftGear(int shift) {
// If the car will shift beyond highest gear, return.
if ( shift + getGearNumber() > getNumberOfGears() ) {
return;
}
// If the car will shift below 1st gear, return.
else if ( shift + getGearNumber() < 1 ) {
return;
}
// Otherwise, change the gear and recompute
// the engine rpm value.
else {
double oldGearRatio = getGearRatio();
setGearNumber(getGearNumber() + shift);
double newGearRatio = getGearRatio();
setOmegaE(getOmegaE()*newGearRatio/oldGearRatio);
}
return;
}
Search WWH ::




Custom Search