Resolving Collisions - noooway/love2d_arkanoid_tutorial GitHub Wiki

Ok, we can detect collisions. Now it's time to deal with how our objects react on them, i.e. with the collision resolution.

When two objects overlap, it turns out convenient to return from the collisions.check_rectangles_overlap not only the fact of the overlap but also a displacement necessary to resolve the overlap. I suppose that the shape b is the one that has to be displaced, and the a should remain in place. If the center of the b is to the right from the center of the a, the b is shifted right to resolve the overlap, in the other case - left ( shift is negative ). Same for y-axis.

function collisions.check_rectangles_overlap( a, b )
   local overlap = false
   local shift_b_x, shift_b_y = 0, 0
   if not( a.x + a.width < b.x  or b.x + b.width < a.x  or
           a.y + a.height < b.y or b.y + b.height < a.y ) then
      overlap = true
      if ( a.x + a.width / 2 ) < ( b.x + b.width / 2 ) then
         shift_b_x = ( a.x + a.width ) - b.x                    --(*1a)
      else 
         shift_b_x = a.x - ( b.x + b.width )                    --(*1b)
      end
      if ( a.y + a.height / 2 ) < ( b.y + b.height / 2 ) then
         shift_b_y = ( a.y + a.height ) - b.y                   --(*2) 
      else
         shift_b_y = a.y - ( b.y + b.height )                   --(*2)
      end      
   end
   return overlap, shift_b_x, shift_b_y                         --(*3)
end

(*1a): b to the right from the center of a; shift b to the right
(*1b): b to the left from a; shift to the left.
(*2): same for y axis.
(*3): shift is returned along with the fact of the overlap

Now, in collision-resolution functions we need to check for overlap, and if it happens - react on in. For platform-ball collision, there is no need to modify the platform, but it is necessary to change the ball direction. ball.rebound is responsible for this.

function collisions.ball_platform_collision( ball, platform )
   local overlap, shift_ball_x, shift_ball_y
   local a = { x = platform.position_x,
               y = platform.position_y,
               width = platform.width,
               height = platform.height }
   local b = { x = ball.position_x - ball.radius,
               y = ball.position_y - ball.radius,
               width = 2 * ball.radius,
               height = 2 * ball.radius }
   overlap, shift_ball_x, shift_ball_y =
      collisions.check_rectangles_overlap( a, b )   
   if overlap then
      ball.rebound( shift_ball_x, shift_ball_y )       --(*1)
   end      
end

(*1): if there is an overlap between the ball and the platform, make ball rebound.

In ball.rebound function the overlap values are passed. I determine the minimal one of them, zero the other one and shift the ball by the nonzero value (see the figure; todo: redraw it). Besides, the speed direction along the shift axis is reversed.

function ball.rebound( shift_ball )
   local min_shift = math.min( math.abs( shift_ball.x ),
                               math.abs( shift_ball.y ) )
   if math.abs( shift_ball.x ) == min_shift then
      shift_ball.y = 0
   else
      shift_ball.x = 0
   end
   ball.position = ball.position + shift_ball
   if shift_ball.x ~= 0 then
      ball.speed.x = -ball.speed.x
   end
   if shift_ball.y ~= 0 then
      ball.speed.y = -ball.speed.y
   end
end

Ball-wall collisions are resolved in a similar fashion. Platform-wall is the same, but there is no need to change the velocity of the platform.

For the ball-brick collision it is necessary to delete the brick on collision. Brick reaction on the collision is described by bricks.brick_hit_by_ball function. For now, the only reaction is to remove the brick from the bricks.current_level_bricks table. This is done by executing table.remove. To implement this, it is necessary to pass the index of the brick into bricks.current_level_bricks.

function collisions.ball_bricks_collision( ball, bricks )
   local overlap, shift_ball_x, shift_ball_y
   .....
   for i, brick in pairs( bricks.current_level_bricks ) do   
      .....
      overlap, shift_ball_x, shift_ball_y =
         collisions.check_rectangles_overlap( a, b )
      if overlap then    
         ball.rebound( shift_ball_x, shift_ball_y )
         bricks.brick_hit_by_ball( i, brick,                     --(*1)
                                   shift_ball_x, shift_ball_y )
      end
   end
end

function bricks.brick_hit_by_ball( i, brick, shift_ball_x, shift_ball_y )
   table.remove( bricks.current_level_bricks, i )                --(*2)
end

(*1): brick reaction on the collision is stored in the bricks.brick_hit_by_ball function.
(*2): brick is removed from the bricks.current_level_bricks