Game Development Reference
In-Depth Information
// Determine if ball collides with the top wall.
// If it does, change the z-velocity of the ball.
if ( ballVz > 0.0 && ballZ + ballRadius >= height ) {
ballVz = -ballVz;
}
// Determine if ball collides with the bottom wall.
// If it does, change the z-velocity of the ball.
if ( ballVz < 0.0 && ballZ - ballRadius <= 0.0 ) {
ballVz = -ballVz;
}
// Determine if ball collides with paddle.
// If it does, change the x-velocity of the ball.
if ( ballVx < 0.0 && ballX - ballRadius <= 20.0 ) {
if ( ballZ - ballRadius >= paddleZ - paddleHeight/2 &&
ballZ + ballRadius <= paddleZ + paddleHeight/2 ) {
ballVx = -ballVx;
}
}
If the paddle misses and the ball reaches the left-hand edge of the game area, the simulation
stops and you lose.
// If ball travels off the left edge of the game
// area, stop the simulation.
if ( ballX <= 0.0 ) {
gameTimer.stop();
}
After it has been determined whether any collisions have taken place, the location of the
ball is updated based on the current x- and z-velocities of the ball, and the GUI display is updated.
// Compute the new location of the ball.
double timeIncrement = 0.07;
ballX = ballX + timeIncrement*ballVx;
ballZ = ballZ + timeIncrement*ballVz;
// Update the display.
updateDisplay();
}
}
}
Play around with the Paddle Game. The ball velocity components can be adjusted to change
the speed at which the ball travels inside the GUI. Crank the velocity up real high and see how
long you can keep the game going.
Search WWH ::




Custom Search