logo.jpg (7978 bytes)

OO Game Programming tutorial - Part 10



Ball/paddle collisions

More collisions! If you mastered the last section this collision isn't much different.

Instead of working with ball.yi we work on ball.xi. We only need to check the front edge of the paddle (we can do the side edges of the paddle but it's not crucial).

We have to have an extra check in this collision. First we have the same as the wall only on the x-axis, then we have to check ball's y position is in position to hit the paddle.

if ((ball.x - ball.radius < player1.x + player1.width) &&
  (ball.y < player1.y) &&
  (ball.y > player1.y - player1.height))
ball.xi = -ball.xi;

if ((ball.x + ball.radius > player2.x) &&
  (ball.y < player2.y) &&
  (ball.y > player2.y - player1.height))
ball.xi = -ball.xi;

It looks tough, but it is just the same as the other collisions with an extra check.

Cool, we nearly have a complete game!

Project: Pong10.zip

 

Next page

Back to main page