I'm learning Lua and thought I should try my hand at making a Breakout clone. It is pretty much playable, but there seems to be odd intersection logic at some points, specifically the ball-brick collisions.
In case you can't tell, the ball is behind 2 bricks (left-most light blue and dark brown). Also, most of the bricks that are missing seem to have been detected correctly.
I cannot seem to figure out why this is happening. Here is the code:
for i=1,rows do for j=1,cols do if bricks[i][j].x~=nil then --ball is between left and right edges if ball.x+dirx>bricks[i][j].x-radius and ball.x+dirx<bricks[i][j].x+length+radius then --ball is between top and bottom edges if ball.y+diry>bricks[i][j].y-radius and ball.y+diry<bricks[i][j].y+8+radius then --Ball hit top/bottom? if ball.y<bricks[i][j].y or ball.y>bricks[i][j].y+8 then diry=-diry -- Reverse Y velocity end --Ball hit side? if ball.x<bricks[i][j].x or ball.x>bricks[i][j].x+length then dirx=-dirx --Reverse X velocity end
I've kind of been doing this Lua thing on-the-fly so if I am doing any bad practices I'd appreciate knowing how it should be done.