I have a quite basic rectangle collision detection functon that i've been using for a while that you can possibly use
Code: Select all
function CheckCollision(x1, y1, w1, h1, x2, y2, w2, h2)
if x1 > (x2 + w2) or (x1 + w1) < x2 then return false end
if y1 > (y2 + h2) or (y1 + h1) < y2 then return false end
return true
end
Usage:
Code: Select all
if CheckCollision(xpos1, ypos1, width1, height1, xpos2, ypos2, width2, height2) then
--Whatever happens when collision is detected
end
You would replace the first 4 parameters with the player's position etc, and the other 4 with the tiles position etc or vice versa.
Simpler version for perfect squares only (you can use this if your player is also 16x16)
Code: Select all
function checkcollision(x1, y1, s1, x2, y2, s2)
if x1 > (x2 + s2) or (x1 + s1) < x2 then return false end
if y1 > (y2 + s2) or (y1 + s1) < y2 then return false end
return true
end
Edit: I'm too lazy to do the exact code, But basically you just do a loop in which you check for collisions, If a collision is detected you stop the player from moving.
edit2: You also might wanna do a less accurate collision detection to see if the player is close the the solid tile, And only if the player is close enough run the collision detecting loop, You could possibly use a distance formula for that
Code: Select all
function getdistance(x1, y1, x2, y2)
dx = x2 - x1
dy = y2 - y1
return = math.sqrt(math.pow(dx,2) + math.pow(dy,2))
end