Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Trick-or-Treat: Collision Code for Pico-8

Greetings, fellow Pico-8 people and coding wizards! 🧙‍♂️✨

Today, we're delving into the arcane world of collision detection. In the spirit of Halloween, I've prepared a special treat for you, a very special version of the collision code I commonly use with Pico-8. But be wary, for there lies a trick—a bug I've cunningly concealed within the incantations of code for you to uncover and vanquish. Watch the video above for a full breakdown and the secret to the fix!


--player defined variables
width  = 8
height = 8
function _move(deltax, deltay)
    --if we are moving on the x axis
    if deltax ~= 0 then
        xdir = sgn(deltax)
        --check the top and bottom corners of the block
        edgex = width/2 * xdir
        for yy = 0, 1, 1 do
            edgey = y - height/2 + height * yy
            --if the flag of the tile we are checking is larger than 0
            if _getposflag(x + edgex + deltax, edgey) > 0 then
                while _getposflag(x + edgex + xdir, edgey) <= 0 do
                    x += xdir
                    end
                --stop our velocity
                deltax = 0
                end
         end
        end
    --if we are moving on the x axis
    if deltay ~= 0 then
        ydir = sgn(deltay)
        --check the top and bottom corners of the block
        edgey = height/2 * ydir
        for xx = 0, 1, 1 do
            edgex = x - width/2 + width * xx
            --if the flag of the tile we are checking is larger than 0
            if _getposflag(edgex, y + edgey + deltay) > 0 then
                while _getposflag(edgex, y + edgey + ydir) <= 0 do
                    y += ydir
                    end
                --stop our velocity
                deltay = 0
                end
         end
        end
    x += deltax
    y += deltay
    end
function _getposflag(xpos, ypos)
    cellx = xpos/8
    celly = ypos/8
    return fget(mget(cellx, celly))
end

Until our paths cross again in the ethereal plane of game development, Happy Coding and Happy Halloween! 🎃

Support this post

Did you like this post? Tell us

Leave a comment

Log in with your itch.io account to leave a comment.