Discord
Login
Community
DARK THEME

Ball colliding with square

I am trying to make a brick breaker style game and cannot correctly make the ball bounce off the square. it'll sometimes bounce off, other times it'll go right through, and it never bounces off from the side. here's the code where b is the ball and k is the bricks:

if collision(b.x,b.y,b.width,b.height,k.x,k.y,k.width,k.height) then
        if (k.y >= b.y+b.height/2 or k.y <= b.y-b.height/2)then
          b.speedY -= b.speedY*2
        elsif k.x >= b.x+b.width/2 or k.x <= b.x-b.width/2 then
          b.speedX -= b.speedX*2
        end
end

Have you tried having only the ball check for collision, then have the ball tell the brick that it collided with it (Or do the logic for the brick)? Otherwise, sometimes one may detect it, but the other does not have a chance to, and not notice.

i just changed my system to be angle based and i fixed it another way

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community