官术网_书友最值得收藏!

Time for action – hitting the ball with the paddles

We will use an approach, similar to that of checking the boundary, to check the collision:

  1. Open the js/pingpong.js file that we used in the previous section.
  2. In the moveBall function, we have already reserved the place to put the collision detection code there. Find the line with // check paddles here.
  3. Let's put the following code there. The code checks whether the ball is overlapping with either paddle and bounces the ball away when they overlap:
    // Variables for checking paddles
    var ballX = ball.x + ball.speed * ball.directionX;
    var ballY = ball.y + ball.speed * ball.directionY;
        
    // check moving paddle here, later.
    // check left paddle
    if (ballX >= pingpong.paddleA.x && ballX < pingpong.paddleA.x + pingpong.paddleA.width) {
      if (ballY <= pingpong.paddleA.y + pingpong.paddleA.height && ballY >= pingpong.paddleA.y) {
        ball.directionX = 1;
      }
    }
    
    // check right paddle
    if (ballX + pingpong.ball.radius >= pingpong.paddleB.x && ballX < pingpong.paddleB.x + pingpong.paddleB.width) {
      if (ballY <= pingpong.paddleB.y + pingpong.paddleB.height && ballY >= pingpong.paddleB.y) {
        ball.directionX = -1;
      }
    }
  4. Test the game in a browser and the ball will now bounce away after hitting the left or right paddle. It will also reset to the center of the playground when it hits the left or right edge of the playground.

What just happened?

We have modified the ball by making it bounce away when it overlaps with the paddles. Let's see how we check the collision between the ball and the left paddle.

At first, we check whether the ball's x position is less than the left paddle's right edge. The right edge is the left value plus the width of the paddle.

Then, we check whether the ball's y position is between the top edge and bottom edge of the paddle. The top edge is the top value and the bottom edge is the top value plus the height of the paddle.

We bounce the ball away if the ball's position passes both the checks. This is how we check it, and it is just a basic collision detection.

We determine that the two objects are overlapped by checking their position and width/height. This type of collision detection works well in rectangle objects but is not good for circles and other shapes. The following screenshot illustrates the issue. The collision areas shown in the following graph are false positive. Their bounding box collides but the actual shapes do not overlap each other. This is a classic and efficient way to check for collisions. It may not be very accurate but its calculation is fast.

For special shapes, we will need more advanced collision detection techniques, which we will discuss later.

Have a go hero

We have placed two paddles on the playground. How about we make the game more challenging by having an alternative paddle in the middle field? It's like having the goalkeeper and forwards in a soccer machine.

主站蜘蛛池模板: 东乡族自治县| 德钦县| 老河口市| 江达县| 布尔津县| 班戈县| 宁明县| 神农架林区| 金门县| 镇巴县| 阿拉善盟| 安宁市| 南通市| 禹州市| 榆林市| 绥芬河市| 隆回县| 永宁县| 威远县| 罗甸县| 徐水县| 河池市| 乌鲁木齐县| 宁德市| 丰镇市| 蚌埠市| 赣州市| 岳西县| 北川| 小金县| 深州市| 宜阳县| 来安县| 屯留县| 科技| 综艺| 犍为县| 张家港市| 玉山县| 乌拉特前旗| 昌平区|