Game Development Reference
In-Depth Information
Figure 6-9. A typical Paddle Game screen shot
Implementing the collision model for the Paddle Game is quite simple. Because the paddle
face and game walls are horizontal or vertical surfaces, the line-of-action vector for the collisions
will always be parallel to the horizontal or vertical axis. For the purposes of the collision modeling,
the walls and paddle faces are assumed to be immovable objects, so the post-collision velocity
of the ball along the line of action can be determined from Equation (6.15). The ball will be
modeled as being perfectly elastic, so the coefficient of restitution, e , will be equal to 1.
The Paddle Game is implemented inside a class named PaddleGame . The first thing the
class does is to declare a number of fields that represent the GUI components for the game and
the location and velocity components of the ball and paddle. The ballX and ballZ fields repre-
sent the x- and z-locations of the center of the ball.
import javax.swing.*;
import java.awt.*;
import javax.swing.border.BevelBorder;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.Timer;
public class PaddleGame extends JFrame
implements ActionListener, ChangeListener
{
// GUI component declarations not shown …
// These fields store ball data and the coefficient
// of restitution.
private double ballVx;
private double ballVz;
private double ballX;
private double ballZ;
private double paddleZ;
private int paddleHeight;
private double ballRadius; // Radius of ball
Search WWH ::




Custom Search