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)) end
And this is the movement part:
if (btn(0)) then if (not placeMeeting(p.x-1, p.y, solidtile)) then p.x -= p.speed end p.mirror = true elseif (btn(1)) then if (not placeMeeting(p.x+p.bbox_w+1, p.y, solidtile)) then p.x += p.speed end p.mirror = false end if (btn(2)) then if (not placeMeeting(p.x, p.y-1, solidtile)) then p.y -= p.speed end elseif (btn(3)) then if (not placeMeeting(p.x, p.y+p.bbox_h+1, solidtile)) then p.y += p.speed end end
Thanks for the help :D
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.
[Please log in to post a comment]