Question

I am encountering a problem in my tile collision method. For some reason the player is able to pass through some tiles when it shouldn't be able to. Also, I'm not entirely sure why but when it gets stuck it can move left through objects, but only left. I have posted some code below, and it would be nice if somebody could point me in the right direction. (Or even better if somebody could find a quick solution!) My player movement method and the tile collision method are both called in the update method.

MAP AND TILE COLLISIONS

    map = { {1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        }

function testTile(x,y)
    if map[y][x + 1] == 1 then
        canRight = true
    end

    if map[y][x + 1] ~= 1 then
        canRight = false
    end

    if map[y][x - 1] == 1 then
        canLeft = true
    end

    if map[y][x - 1] ~= 1 then
        canRight = false
    end

    if map[y + 1][x] == 1 then
        canDown = true
    end

    if map[y + 1][x] ~= 1 then
        canDown = false
    end

    if map[y - 1][x] == 1 then
        canUp = true
    end

    if map[y - 1][x] ~= 1 then
        canUp = false
    end
end

function movePlayer(dt)

    if love.keyboard.isDown("right") and canRight then
        playerX = playerX + 1 * dt
    end

    if love.keyboard.isDown("left") and canLeft then
        playerX = playerX - 1 * dt
    end

    if love.keyboard.isDown("down") and canDown then
        playerY = playerY + 1 * dt
    end

    if love.keyboard.isDown("up") and canUp then
        playerY = playerY - 1 * dt
    end
end
Was it helpful?

Solution

The reason left is working when it's stuck is likely because you have a typo in the 4th if statement of testTile(x,y).

You wrote

if map[y][x - 1] ~= 1 then
    canRight = false
end

and it should be

if map[y][x - 1] ~= 1 then
    canLeft = false
end

OTHER TIPS

The reason is a typing error:

if map[y][x - 1] ~= 1 then
    canRight = false;
end

You set canRight to false if you can't move left. It is probably a copy/paste error, so always take care, if you copy/paste code. This is one of the most common mistakes, and this mistakes are hard to detect :P

Another little suggestion: instead of using

if map[y][x - 1] == 1 then
    canLeft = true;
end
if map[y][x - 1] ~= 1 then
    canLeft = false;
end

you could use

else canLeft = false;

It would make the code shorter and in my opinion also cleaner.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top