Let's say I am approaching a wall horizontally. I stop moving when I collide with the wall. But, I can't move up if I am holding the right key, it only has to be the up and down key, which in many games that doesn't happen. I can't figure out how to fix it. Same thing with vertical collision
The player can move in 8 directions (WASD) , and only moves in integer increments. Here is my collision code:
Code: Select all
function collision:checkAABB(x, x2, ox, ox2)
if (x > ox and x < ox2) then
return true
end
if (x2 > ox and x2 < ox2) then
return true
end
return false
end
function sign(num)
local sign = 0
if (num > 0) then
sign = 1
end
if (num < 0) then
sign = -1
end
return sign
end
Code: Select all
function Player:collisions()
local xMeet = false
local yMeet = false
for _, o in pairs(objects) do
if (o ~= player) then
xMeet = collision:checkAABB(player.x + player.hsp, player.x + player.w + player.hsp, o.x, o.x + o.w)
yMeet = collision:checkAABB(player.y + player.vsp, player.y + player.h + player.vsp, o.y, o.y + o.h)
if (xMeet and yMeet) then
xMeet, yMeet = false, false
while (true) do
xMeet = collision:checkAABB(player.x + sign(player.hsp), player.x + player.w + sign(player.hsp), o.x, o.x + o.w)
if (xMeet == false) then
player.x = player.x + sign(player.hsp)
else
player.hsp = 0
break;
end
end
while (true) do
yMeet = collision:checkAABB(player.y + sign(player.vsp), player.y + player.h + sign(player.vsp), o.y, o.y + o.h)
if (yMeet == false) then
player.y = player.y + sign(player.vsp)
else
player.vsp = 0
break;
end
end
end
end
end
end
***The player's x is incremented my the hsp every frame, same with y and vsp****