-
Notifications
You must be signed in to change notification settings - Fork 17
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.
Feedback is crucial to improve the tutorial!
Let me know if you have any questions, critique, suggestions or just any other ideas.
Chapter 1: Prototype
- The Ball, The Brick, The Platform
- Game Objects as Lua Tables
- Bricks and Walls
- Detecting Collisions
- Resolving Collisions
- Levels
Appendix A: Storing Levels as Strings
Appendix B: Optimized Collision Detection (draft)
Chapter 2: General Code Structure
- Splitting Code into Several Files
- Loading Levels from Files
- Straightforward Gamestates
- Advanced Gamestates
- Basic Tiles
- Different Brick Types
- Basic Sound
- 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
- Improved Ball Rebounds
- Ball Launch From Platform (Two Objects Moving Together)
- Mouse Controls
- Spawning Bonuses
- Bonus Effects
- Glue Bonus
- Add New Ball Bonus
- Life and Next Level Bonuses
- Random Bonuses
- Menu Buttons
- Wall Tiles
- Side Panel
- Score
- Fonts
- More Sounds
- Final Screen
- Packaging
Appendix D: GUI Layouts
Appendix E: Love-release and Love.js
Beyond Programming: