Skip to content

Optimized Collision Detection

noooway edited this page Aug 5, 2017 · 7 revisions
function collisions.check_rectangles_overlap( a, b )
   local overlap = false
   local shift_b_x, shift_b_y = 0, 0
   local dist_x = b.position_x - a.position_x
   local dist_y = b.position_y - a.position_y
   if math.abs( dist_x ) <= a.halfwidth + b.halfwidth and
      math.abs( dist_y ) <= a.halfheight + b.halfheight then
	 overlap = true
	 shift_b_x = dist_x / math.abs(dist_x) * (
	    ( a.halfwidth + b.halfwidth ) - math.abs(dist_x) )
	 shift_b_y = dist_y / math.abs(dist_y) * (
	    ( a.halfheight + b.halfheight ) - math.abs(dist_y) )
   end
   return overlap, shift_b_x, shift_b_y
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