Optimized Collision Detection - noooway/love2d_arkanoid_tutorial GitHub Wiki

function collisions.check_rectangles_overlap( a, b )
   local x_overlap, x_shift = overlap_along_axis( a.position_x, b.position_x,
						  a.width, b.width )
   local y_overlap, y_shift = overlap_along_axis( a.position_y, b.position_y,
						  a.height, b.height )
   local overlap = x_overlap > 0 and y_overlap > 0
   return overlap, x_shift, y_shift
end
local function overlap_along_axis( a_pos, b_pos, a_size, b_size )
   local diff = b_pos - a_pos
   local dist = math.abs( diff )
   local overlap = ( a_size + b_size ) / 2 - dist
   return overlap, diff / dist * overlap
end

This allows to avoid creating intermediate tables:

function collisions.ball_platform_collision( ball, platform )
   local overlap, shift_ball_x, shift_ball_y
   overlap, shift_ball_x, shift_ball_y =
      collisions.check_rectangles_overlap( platform, ball )   
   if overlap then
      ball.rebound( shift_ball_x, shift_ball_y )
   end      
end

function collisions.ball_walls_collision( ball, walls )
   local overlap, shift_ball_x, shift_ball_y
   for _, wall in pairs( walls.current_level_walls ) do
      overlap, shift_ball_x, shift_ball_y =
      	 collisions.check_rectangles_overlap( wall, ball )
      if overlap then
	 ball.rebound( shift_ball_x, shift_ball_y )
      end
   end
end

and so on.