ball_placement.asm - 89erik/NES-breakout GitHub Wiki
Tree: root /src/loop/ball_placement.asm
This file contains the BallPlacement subroutine which moves the ball one time instance. The subroutine has several sub tasks organized with local labels (labels prefixed with @):
###@hold_ball The hold_ball task is entered if the holding_ball variable is asserted. Its purpose is to allow the player to hold the ball on the racket, which is signaled from outside of this file through the holding_ball variable. The task works by setting the velocity vector of the ball to zero, and the position to just above the center of wherever the racket is currently located. The rest of the subroutine is skipped at the end of the hold_ball task.
###@move_ball This task adds the velocity vector to the position vector of the ball.
###@check_x_edge This task checks whether the ball hit a wall. If so, the horizontal velocity is inverted causing the ball to bounce on the wall.
###@check_y_edge This task checks whether the ball hit the roof or floor. If the ball is moving upwards, the task works like check_x_edge. If the ball is moving downwards, a check is first made made to see whether the ball is at racket height or below. If it is below the racket, a check for racket hit is not made. Instead it checks whether the ball has fallen out of the game area. If it has, a call is made to FlipperNoHit which retracts one point and resets the game.
If the ball is not above the racket but still high enough to be caught, a check is made to see whether the ball hit the racket, using the subroutine in check_hit_flipper.asm. This subroutine returns the distance between the ball and the center of the racket. This difference is then checked with the width of the racket plus a miss tolerance to determine whether there was an actual hit. A miss is ignored, it will not be caught until the ball falls completely out of the game area. If there was a hit, the vertical speed is inverted (causing a vertical bounce) and the difference is added to the horizontal speed. This makes a narrow hit cause the ball to bounce with an angle, and a center hit to bounce predictably. The difference is divided by 8 (three right shifts) to avoid extreme bounce angles.