Game Development Reference
In-Depth Information
The actionPerformed method itself is quite simple. The current time is incremented, and then
the x- and z-locations of the beanbag are computed according to Equations (4.17) and (4.18).
The display is then updated. If the beanbag has hit the ground, the simulation is stopped. The
ground in this game is defined at a height of 1.4 m because the line that indicates the ground in
the GUI is drawn 140 pixels below the top of the display panel.
// This ActionListener is called by the Timer
class GameUpdater implements ActionListener {
public void actionPerformed(ActionEvent event) {
// Update the time and compute the new position
// of the beanbag.
double timeIncrement = 0.05;
time += timeIncrement;
// There is no force in the x-direction, so the
// new x location is the initial x location plus
// the product of the horizontal velocity and time.
x = x0 + vx0*time;
// The z-location is influenced by the acceleration
// due to gravity.
double g = -9.81;
z = z0 + vz0*time + 0.5*g*time*time;
// Update the display
updateDisplay();
// If the beanbag hits the ground, stop
// the simulation.
if ( z <= 1.4 ) {
gameTimer.stop();
}
}
}
When you play around with the Beanbag Game, try adjusting the initial horizontal and
vertical velocities and see what happens. The beanbag is our first attempt at modeling the flight
of a projectile, a subject we'll cover in much more detail in the next chapter.
Solving Ordinary Differential Equations
In the beginning of this section we learned that if the forces applied to an object are constant,
the velocity and location of the object at any time can be computed from simple algebraic
equations. Many times the forces acting on an object will not be constant but instead will vary
depending on the velocity or location of the object. For example, if a projectile is flying through
the air, it will be subject to the force of aerodynamic drag, which is a function of the square of the
velocity of the object. The forces on the projectile will vary during its flight, and the resulting
equations of motion cannot be directly solved.
Search WWH ::




Custom Search