Game Development Reference
In-Depth Information
double time = getS();
double vx0 = getQ(0);
double x0 = getQ(1);
double vy0 = getQ(2);
double y0 = getQ(3);
double vz0 = getQ(4);
double z0 = getQ(5);
// Update the xyz locations and the z-component
// of velocity. The x- and y-velocities don't change.
double x = x0 + vx0*dt;
double y = y0 + vy0*dt;
double vz = vz0 + G*dt;
double z = z0 + vz0*dt + 0.5*G*dt*dt;
// Update time;
time = time + dt;
// Load new values into ODE arrays and fields.
setS(time);
setQ(x, 1);
setQ(y, 3);
setQ(vz,4);
setQ(z, 5);
}
Because the SimpleProjectile class is a subclass of ODE , it has to provide an implemen-
tation of the getRightHandSide method. If you remember from the previous chapter, this
method is used to compute the right-hand side of the ODEs that will be solved. The
SimpleProjectile class doesn't solve any ODEs, so the method is written to return a dummy
array.
// Because SimpleProjectile extends the ODE class,
// it must implement the getRightHandSide method.
// In this case, the method returns a dummy array.
public double[] getRightHandSide(double s, double Q[],
double deltaQ[], double ds, double qScale) {
return new double[1];
}
}
In the next section, the SimpleProjectile class will be used to create a golf game.
The Golf Game
Let's use the SimpleProjectile class to create a golf game. The objective of the game is to hit a
golf ball onto the green. A sample screen shot for the Golf Game is shown in Figure 5-4. Like the
other games in this topic, the Golf Game is based on a simple Java GUI. The graphics in the
game are a bit primitive, but the physics in the game is real. The GUI consists of text field
Search WWH ::




Custom Search