Game Development Reference
In-Depth Information
The new rpm value of the engine is computed using Equation (8.13). If the value exceeds
the redline value for the car, a warning message is displayed. If the rpm value exceeds 8000, the
engine is blown and the simulation stops.
// Compute the new engine rpm value.
double rpm = car.getVx()*60.0*car.getGearRatio()*
car.getFinalDriveRatio()/(2.0*Math.PI*car.getWheelRadius());
car.setOmegaE(rpm);
// If the rpm exceeds the redline value, put a
// warning message on the screen. First, clear the
// message text field of any existing messages.
messageTextField.setText("");
if ( car.getOmegaE() > car.getRedline() ) {
messageTextField.setText("Warning: Exceeding redline rpm");
}
if ( car.getOmegaE() > 8000.0 ) {
messageTextField.setText("You have blown the engine!");
gameTimer.stop();
}
Two rectangular markers are used to simulate the motion of the car. The car stays in a set
location in the GUI display, and the rectangular markers move from right to left. The location
of the markers is updated based on the velocity of the car. The factor of 10 is a scaling factor that
relates the size of the car image to the actual length of the car. After the new marker locations have
been determined, the GUI display is updated.
// Update the location of the rectangular markers.
rectangleOneX = rectangleOneX + 10.0*car.getVx()*timeIncrement;
rectangleTwoX = rectangleTwoX + 10.0*car.getVx()*timeIncrement;
// If the markers have gone off the display, move them
// back to zero.
if ( rectangleOneX > 401.0 ) {
rectangleOneX = 0.0;
}
if ( rectangleTwoX > 401.0 ) {
rectangleTwoX = 0.0;
}
// Update the display.
resetDisplay();
}
}
}
Play around with the Car Simulator. Switch the mode to “accelerate” and push the Start
button to start the simulation. Be sure to watch the rpm value and shift up before it hits the
redline value. When you get to sixth gear, let the car run and see what the maximum velocity of
Search WWH ::




Custom Search