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
1 changed files with 81 additions and 39 deletions

120
main.lua
View File

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