Game Development Reference
In-Depth Information
velocity, or braking. The
omegaE
field is the engine turnover rate in
rpm
. The names of the other
Car
class fields are self-explanatory.
The
Car
constructor is used to initialize the fields in the
Car
,
DragProjectile
,
SimpleProjectile
,
and
ODE
classes. The first thing the constructor does is to call the
DragProjectile
class constructor.
// The Car constructor calls DragProjectile constructor and
// then initializes the car-specific variables.
public Car(double x, double y, double z,
double vx, double vy, double vz,
double time, double mass, double area,
double density, double Cd, double redline,
double finalDriveRatio, double wheelRadius,
int numberOfGears) {
super(x, y, z, vx, vy, vz, time, mass, area,
density, Cd);
The
Car
constructor then initializes the fields declared in the
Car
class with values passed to
the constructor. The size of the
gearRatio
array is set according to the value of the
numberOfGears
field. The gear ratios are initially given dummy values of 1.
// Initialize some fields based on values passed
// to the constructor.
this.redline = redline; // Redline rpm
this.finalDriveRatio = finalDriveRatio; // Final drive ratio
this.wheelRadius = wheelRadius; // Wheel radius
this.numberOfGears = numberOfGears; // Number of gears
// Initialize the array that stores the gear ratios.
// The array is shifted so the first index in the
// array correpsonds to first gear and so on.
// Give all gear ratios the dummy value of 1.0
gearRatio = new double[numberOfGears + 1];
gearRatio[0] = 0.0;
for(int i=1; i<numberOfGears+1; ++i) {
gearRatio[i] = 1.0;
}
Some of the
Car
field values will be set to the same value for all car classes including the
fields that represent the coefficient of rolling friction, initial engine
rpm
, starting gear number,
and mode.
// Set some fields the same for all cars.
muR = 0.015; // Coefficient of rolling friction
omegaE = 1000.0; // Engine rpm
gearNumber = 1; // Gear the car is in
mode = "accelerating"; // Accelerating, cruising, or
// braking
}