Having problems with an AABB Collision Function
Posted: Mon Jun 17, 2019 9:30 pm
I'm trying to work with an AABB collision function for an RPG game, and I'm basically trying to make it so the player doesn't go through the block and just treats it as a solid object, the player is always at the center of the screen and the objects that exist within the game move when I press a key, so far I have this and when I tested it, the player just stops when it touches the block, I tried to change the true/false value in blockcollision to false whenever it goes the opposite direction, but it just doesn't seem to work.
Here's the code:
Here's the code:
Code: Select all
function love.update(dt)
if CheckCollision(player.x, player.y, player.w, player.h, blockx, blocky, blockw, blockh) then
blockcollision = true
end
if love.keyboard.isDown("a") then
if blockcollision == false then
textx = textx + player.spd * dt
blockx = blockx + player.spd * dt
end
elseif love.keyboard.isDown("d") then
if blockcollision == false then
textx = textx - player.spd * dt
blockx = blockx - player.spd * dt
end
end
if love.keyboard.isDown("w") then
if blockcollision == false then
texty = texty + player.spd * dt
blocky = blocky + player.spd * dt
end
elseif love.keyboard.isDown("s") then
if blockcollision == false then
texty = texty - player.spd * dt
blocky = blocky - player.spd * dt
end
end
end
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