Code: Select all
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1
end
EDIT: now i know how to use, but sometimes he dont collide, here is the code
it is in the update function
Code: Select all
--BALL
if(ball.x + xa + (ball.w / 2) > love.window.getWidth()) then
xa = -1
elseif(ball.x + xa - (ball.w / 2) < 0) then
xa = 1
end
if(ball.y + ya + (ball.h / 2) > love.window.getHeight()) then
ya = -1
elseif(ball.y - (ball.h / 2) + ya < 0) then
ya = 1
end
if(CheckCollision(ball.x,ball.y,ball.w,ball.h, paddle1.x,paddle1.y,pWidth,pHeight)) then
xa = 1;
end
if(CheckCollision(ball.x,ball.y,ball.w,ball.h, paddle2.x,paddle2.y,pWidth,pHeight)) then
xa = -1;
end
ball.x = ball.x + xa * dt * ball.spd
ball.y = ball.y + ya * dt * ball.spd