Fix collisions

- Edge block collisions
 - Corner collisions
This commit is contained in:
Lovetocode999 2021-03-25 11:13:26 -06:00
parent baa4c739e5
commit aa6adf2ec0

120
main.lua
View File

@ -786,13 +786,23 @@ function love.update( dt )
for i = 1,10,1 for i = 1,10,1
do do
if leftBlocks[i] > 0 then if leftBlocks[i] > 0 then
if ball.y > ( math.sqrt( 2 ) * playerDistance ) + ( blockLength * ( i - 1 ) ) - 10 and local blockY = ( math.sqrt( 2 ) * playerDistance ) + ( blockLength * ( i - 1 ) )
ball.y < ( math.sqrt( 2 ) * playerDistance ) + ( blockLength * i ) - 10 then if ball.y > blockY - 20 and
ball.direction = 180 - ball.direction ball.y < blockY + blockLength then
ball.x = 15 if ball.y < ( blockY - 15 ) or ball.y > ( blockY + blockLength - 5 ) then
leftBlocks[i] = leftBlocks[i] - 1 ball.direction = 360 - ball.direction
player2.score = player2.score + 1 if ball.y < ( blockY - 15 ) then
break ball.y = blockY - 20
else
ball.y = blockY + blockLength
end
else
ball.direction = 180 - ball.direction
ball.x = 15
end
leftBlocks[i] = leftBlocks[i] - 1
player2.score = player2.score + 1
break
end end
end end
end end
@ -804,15 +814,25 @@ function love.update( dt )
for i = 1,10,1 for i = 1,10,1
do do
if rightBlocks[i] > 0 then if rightBlocks[i] > 0 then
if ball.y > ( math.sqrt( 2 ) * playerDistance ) + ( blockLength * ( i - 1 ) ) - 10 and local blockY = ( math.sqrt( 2 ) * playerDistance ) + ( blockLength * ( i - 1 ) )
ball.y < ( math.sqrt( 2 ) * playerDistance ) + ( blockLength * i ) - 10 then if ball.y > blockY - 20 and
ball.direction = 180 - ball.direction ball.y < blockY + blockLength then
ball.x = love.graphics:getWidth() - 35 if ball.y < ( blockY - 15 ) or ball.y > ( blockY + blockLength - 5 ) then
rightBlocks[i] = rightBlocks[i] - 1 ball.direction = 360 - ball.direction
player1.score = player1.score + 1 if ball.y < ( blockY - 15 ) then
break ball.y = blockY - 20
end else
end ball.y = blockY + blockLength
end
else
ball.direction = 180 - ball.direction
ball.x = love.graphics.getWidth() - 35
end
rightBlocks[i] = rightBlocks[i] - 1
player1.score = player1.score + 1
break
end
end
end end
end end
-- Check collision with the top blocks -- Check collision with the top blocks
@ -822,14 +842,24 @@ function love.update( dt )
for i = 1,10,1 for i = 1,10,1
do do
if topBlocks[i] > 0 then if topBlocks[i] > 0 then
if ball.x > ( math.sqrt( 2 ) * playerDistance ) + ( blockLength * ( i - 1 ) ) - 10 and local blockX = ( math.sqrt( 2 ) * playerDistance ) + ( blockLength * ( i - 1 ) )
ball.x < ( math.sqrt( 2 ) * playerDistance ) + ( blockLength * i ) - 10 then if ball.x > blockX - 20 and
ball.direction = 360 - ball.direction ball.x < blockX + blockLength then
ball.y = 15 if ball.x < blockX - 15 or ball.x > blockX + blockLength - 5 then
topBlocks[i] = topBlocks[i] -1 ball.direction = 180 - ball.direction
player1.score = player1.score + 1 if ball.x < blockX - 15 then
break ball.x = blockX - 20
end else
ball.x = blockX + blockLength
end
else
ball.direction = 360 - ball.direction
ball.y = 15
end
topBlocks[i] = topBlocks[i] -1
player1.score = player1.score + 1
break
end
end end
end end
end end
@ -840,37 +870,49 @@ function love.update( dt )
for i = 1,10,1 for i = 1,10,1
do do
if bottomBlocks[i] > 0 then if bottomBlocks[i] > 0 then
if ball.x > ( math.sqrt( 2 ) * playerDistance ) + ( blockLength * ( i - 1 ) ) - 10 and blockX = ( math.sqrt( 2 ) * playerDistance ) + ( blockLength * ( i - 1 ) )
ball.x < ( math.sqrt( 2 ) * playerDistance ) + ( blockLength * i ) - 10 then if ball.x > blockX - 20 and
ball.direction = 360 - ball.direction ball.x < blockX + blockLength then
ball.y = playAreaHeight - 35 if ball.x < blockX - 15 or ball.x > blockX + blockLength - 5 then
bottomBlocks[i] = bottomBlocks[i] -1 ball.direction = 180 - ball.direction
player2.score = player2.score + 1 if ball.x < blockX - 15 then
break ball.x = blockX - 20
else
ball.x = blockX + blockLength
end
else
ball.direction = 360 - ball.direction
ball.y = playAreaHeight - 35
end
bottomBlocks[i] = bottomBlocks[i] -1
player2.score = player2.score + 1
break
end end
end end
end end
end end
-- Check collision with the edge -- Check collision with the edge
if ( ball.x > love.graphics:getWidth() - 20 or ball.x < 20 ) and -- Corners
( ball.y > playAreaHeight - 20 or ball.y < 20 ) then if ( ball.x > love.graphics:getWidth() - 20 or ball.x < 0 ) and
( ball.y > playAreaHeight - 20 or ball.y < 0 ) then
ball.direction = ball.direction + 180 ball.direction = ball.direction + 180
if ball.x > love.graphics:getWidth() - 20 then if ball.x > love.graphics:getWidth() - 20 then
ball.x = love.graphics:getWidth() - 20 ball.x = love.graphics:getWidth() - 20
else else
ball.x = 20 ball.x = 0
end end
if ball.y > playAreaHeight - 20 then if ball.y > playAreaHeight - 20 then
ball.y = playAreaHeight - 20 ball.y = playAreaHeight - 20
else else
ball.y = 20 ball.y = 0
end end
ball.color = { 1, 0, 1 }
elseif ball.x > love.graphics:getWidth() - 20 or ball.x < 0 then elseif ball.x > love.graphics:getWidth() - 20 or ball.x < 0 then
ball.direction = 180 - ball.direction ball.direction = 180 - ball.direction
-- Right edge -- Right edge
if ball.x > love.graphics:getWidth() - 20 then if ball.x > love.graphics:getWidth() - 20 then
if ball.y > math.sqrt( 2 ) * playerDistance and if ball.y > math.sqrt( 2 ) * playerDistance and
ball.y < playAreaHeight - ( math.sqrt( 2 ) * playerDistance ) ball.y < playAreaHeight - ( math.sqrt( 2 ) * playerDistance ) - 20
then then
player2.score = player2.score - 1 player2.score = player2.score - 1
screen:setShake( 40 ) screen:setShake( 40 )
@ -881,7 +923,7 @@ function love.update( dt )
end end
else -- Left edge else -- Left edge
if ball.y > math.sqrt( 2 ) * playerDistance and if ball.y > math.sqrt( 2 ) * playerDistance and
ball.y < playAreaHeight - ( math.sqrt( 2 ) * playerDistance ) ball.y < playAreaHeight - ( math.sqrt( 2 ) * playerDistance ) - 20
then then
player1.score = player1.score - 1 player1.score = player1.score - 1
screen:setShake( 40 ) screen:setShake( 40 )
@ -897,7 +939,7 @@ function love.update( dt )
if ball.y > playAreaHeight - 20 then if ball.y > playAreaHeight - 20 then
if ball.x > math.sqrt( 2 ) * playerDistance and if ball.x > math.sqrt( 2 ) * playerDistance and
ball.x < love.graphics:getWidth() - ball.x < love.graphics:getWidth() -
( math.sqrt( 2 ) * playerDistance ) then ( math.sqrt( 2 ) * playerDistance ) - 20 then
player1.score = player1.score - 1 player1.score = player1.score - 1
screen:setShake( 40 ) screen:setShake( 40 )
reset() reset()
@ -908,7 +950,7 @@ function love.update( dt )
else -- Top edge else -- Top edge
if ball.x > math.sqrt( 2 ) * playerDistance and if ball.x > math.sqrt( 2 ) * playerDistance and
ball.x < love.graphics:getWidth() - ball.x < love.graphics:getWidth() -
( math.sqrt( 2 ) * playerDistance ) then ( math.sqrt( 2 ) * playerDistance ) - 20 then
player2.score = player2.score - 1 player2.score = player2.score - 1
screen:setShake( 40 ) screen:setShake( 40 )
reset() reset()