Log In  

Cart #43142 | 2017-08-09 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

Hi! I just started using PICO-8 and I'm having a little trouble with my collision code.

It works fine when colliding against walls, but colliding against corners (the red tiles) breaks it.

Here is the collision part of the code:

function placeMeeting(x, y, f)
    local tilesize = 8
    local tilex = flr(x/8)
    local tiley = flr(y/8)
    local s = mget(tilex, tiley)
    return (fget(s, f))

And this is the movement part:

        if (btn(0)) then
            if (not placeMeeting(p.x-1, p.y, solidtile)) then
                p.x -= p.speed
            p.mirror = true
        elseif (btn(1)) then
            if (not placeMeeting(p.x+p.bbox_w+1, p.y, solidtile)) then
                p.x += p.speed
            p.mirror = false

        if (btn(2)) then
            if (not placeMeeting(p.x, p.y-1, solidtile)) then
                p.y -= p.speed
        elseif (btn(3)) then
            if (not placeMeeting(p.x, p.y+p.bbox_h+1, solidtile)) then
                p.y += p.speed

Thanks for the help :D

P#43143 2017-08-09 00:52 ( Edited 2017-08-09 19:02)

Your collision code probes one pixel next to one of the four corners to see if movement is possible. It looks like you probe from the top-left corner for leftward and upward movement, from the top-right corner for rightward movement and the bottom-left corner for downward movement.

The problem is that your character is more than one pixel wide, and might collide with either the left or right (or top or bottom) corner of a wall. An easy fix is to probe from both corners for the direction of movement, and only allow movement if both are clear. Since a wall can't be narrower than 8 pixels, this should work nicely.

P#43146 2017-08-09 08:15 ( Edited 2017-08-09 12:15)

Thanks! That should fix it :D

P#43153 2017-08-09 15:02 ( Edited 2017-08-09 19:02)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2023-03-22 22:05:46 | 0.006s | Q:12