![]() |
OO Game Programming tutorial - Part 7 |
Paddle/wall collisionsOkay, it's not too good if the paddles can fly off the screen so we'll be adding some collision detection with the walls to stop that. To detect the collision we check if the edge of the paddle will move past the edge of the wall. When this happens we'll have to let the computer know what we'll want to happen, which in this case, the paddle stops next to the wall.
By looking at the diagram we can see the edges we need to check. For the top wall we check: if (Paddle.y > (topWall.y - topWall.height)) and the bottom wall: if ((Paddle.y - Paddle.height) < bottomWall.y) When the collision occurs, we set the Paddle.y position to that edge of the wall. This is done every game loop for both player1 and 2 in the collision section of Game_Main(). Project: Pong07.zip
|