-
Notifications
You must be signed in to change notification settings - Fork 20
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.
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: