Skip to content

Optimized Collision Detection

noooway edited this page Aug 7, 2017 · 7 revisions

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.

    Home
    Acknowledgements
    Todo

Chapter 1: Prototype

  1. The Ball, The Brick, The Platform
  2. Game Objects as Lua Tables
  3. Bricks and Walls
  4. Detecting Collisions
  5. Resolving Collisions
  6. Levels

    Appendix A: Storing Levels as Strings
    Appendix B: Optimized Collision Detection (draft)

Chapter 2: General Code Structure

  1. Splitting Code into Several Files
  2. Loading Levels from Files
  3. Straightforward Gamestates
  4. Advanced Gamestates
  5. Basic Tiles
  6. Different Brick Types
  7. Basic Sound
  8. Game Over

    Appendix C: Stricter Modules (draft)
    Appendix D-1: Intro to Classes (draft)
    Appendix D-2: Chapter 2 Using Classes.

Chapter 3 (deprecated): Details

  1. Improved Ball Rebounds
  2. Ball Launch From Platform (Two Objects Moving Together)
  3. Mouse Controls
  4. Spawning Bonuses
  5. Bonus Effects
  6. Glue Bonus
  7. Add New Ball Bonus
  8. Life and Next Level Bonuses
  9. Random Bonuses
  10. Menu Buttons
  11. Wall Tiles
  12. Side Panel
  13. Score
  14. Fonts
  15. More Sounds
  16. Final Screen
  17. Packaging

    Appendix D: GUI Layouts
    Appendix E: Love-release and Love.js

Beyond Programming:

  1. Game Design
  2. Minimal Marketing (draft)
  3. Finding a Team (draft)

Archive

Clone this wiki locally