I'm having some problems with AABB colision

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
edu
Prole
Posts: 7
Joined: Sat Apr 06, 2024 2:21 pm

I'm having some problems with AABB colision

Post by edu »

I'm making a pong game and the colision method im using is AABB, for some reason, if you hit the ball with the paddle from above, the ball enters the paddle, how can I solve it ?

also, here's the code:

Code: Select all

function love.load()

    love.window.setTitle("Pong by edu")

    texto = {fonte = love.graphics.newFont("assets/fontes/SHUTTLE-X.ttf", 70) }
    love.graphics.setFont(texto.fonte)

    alturaTela = love.graphics:getHeight()
    larguraTela = love.graphics:getWidth()

    love.window.setMode(1000,700)
    p1 = {x = 100, y = 350, h = 100, w = 40, ponto = 0}
    p2 = {x = 900, y = 350, h = 100, w = 40, ponto = 0}

    bola = {x = larguraTela / 2, y = alturaTela / 2, w = 30, h = 30, velY = 0, velX = 5}
end

function love.update(dt)
    bolaM()
    mover()
end

function love.draw()
    love.graphics.setBackgroundColor(.33, .33, .76)
    love.graphics.setColor(1, 1, 1)
    love.graphics.rectangle("line", bola.x, bola.y, bola.w, bola.h)
    love.graphics.rectangle("fill", p1.x, p1.y, p1.w, p1.h)
    love.graphics.rectangle("fill", p2.x, p2.y, p2.w, p2.h)
    love.graphics.setColor(0, 0, 0)
    love.graphics.print(""..p1.ponto, 200, 100)
    love.graphics.print(""..p2.ponto, 800, 100)
end

function mover()
    if p1.y > alturaTela then
        p1.y = 600
    end
    if p1.y < 0 then
        p1.y = 0
    end
    if p2.y < 0 then
        p2.y = 0
    end
    if p2.y > alturaTela then
        p2.y = 600
    end
    if love.keyboard.isDown("w") then
        p1.y = p1.y -9
    elseif love.keyboard.isDown("s") then
        p1.y = p1.y +9
    end

    if love.keyboard.isDown("up") then
        p2.y = p2.y -9
    elseif love.keyboard.isDown("down") then
        p2.y = p2.y +9
    end
end

function bolaM()

    if colisao(p1, bola) then
        bola.velX = bola.velX * -1
        bola.velY = -5
    elseif colisao(p2, bola) then
        bola.velY = 5
        bola.velX = bola.velX * -1
    end

    if bola.x > larguraTela + 150 then
        bola.velY = 0
        p1.ponto = p1.ponto + 1
        bola.x = larguraTela / 2
        bola.y = alturaTela / 2
        bola.velX = bola.velX * -1
    elseif bola.x < -100 then
        bola.velY = 0
        p2.ponto = p2.ponto + 1
        bola.x = larguraTela / 2
        bola.y = alturaTela / 2
        bola.velX = bola.velX * -1
    end

    bola.x = bola.x + bola.velX

    bola.y = bola.y + bola.velY

    if bola.y < 0 or bola.y > 700 then
        bola.velY = bola.velY * -1
    end
end

function colisao(r1, r2)
    return r1.x < r2.x + r2.w and
    r2.x < r1.x + r1.w and
    r1.y < r2.y + r2.h and
    r2.y < r1.y + r1.h 
end
PS: if you want to test the code, delete line 5 and 6.
Attachments
main.lua
(2.42 KiB) Downloaded 200 times
Post Reply

Who is online

Users browsing this forum: Bing [Bot], glitchapp and 5 guests