Looks like there's a bug in the multi-tile wall building.
Collisions are correct if the wall sprite is just 1 tile
Collisions are offset along the longer axis by 8 pixels if the wall sprite is 2 tiles, 16 pixels if the wall sprite is 3 tiles, 24 pixels if the wall sprite is 4 tiles....
A shorter demo:
import "CoreLibs/sprites"
import "CoreLibs/graphics"
pd = playdate
gfx = pd.graphics
-- ================== Load Resources
hero_img = gfx.image.new("hero")
map_img = gfx.imagetable.new("map")
map_tilemap = gfx.tilemap.new()
map_tilemap:setImageTable(map_img)
map = {2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 2, 2, 2, 2, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 5, 5, 5, 5, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 4, 4, 4, 5, 5, 5, 5, 4, 5, 5, 5, 5, 6, 5, 5, 5, 5, 4, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 5, 5, 5, 5, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7, 8, 5, 5, 8, 9, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5,
2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2,
}
map_tilemap:setTiles(map,25)
tile_sprites = gfx.sprite.addWallSprites(map_tilemap, {5})
-- =================== Initialize Global Data
touchy = {}
function DrawBackground()
map_tilemap:draw(0,0)
end
gfx.setColor(gfx.kColorWhite)
gfx.setBackgroundColor(gfx.kColorBlack)
gfx.setImageDrawMode(gfx.kDrawModeCopy)
gfx.sprite.setBackgroundDrawingCallback(DrawBackground)
hero = gfx.sprite.new(hero_img)
hero:moveTo(100,100)
hero:add()
hero:setCollideRect( 0,0,hero:getSize() )
function playdate.update()
local move = {x=0, y=0}
local speed = 4
if pd.buttonIsPressed(pd.kButtonUp) then
move.y -= speed
end
if pd.buttonIsPressed(pd.kButtonDown) then
move.y += speed
end
if pd.buttonIsPressed(pd.kButtonRight) then
move.x += speed
end
if pd.buttonIsPressed(pd.kButtonLeft) then
move.x -= speed
end
local actualX, actualY, collisions, length = hero:moveWithCollisions(hero.x + move.x, hero.y + move.y)
for i = 1, length do
print("hero at: " .. hero.x .. ", " .. hero.y)
print("collision at: " .. collisions[i].touch.x .. ", " .. collisions[i].touch.y)
table.insert(touchy, {collisions[i].touch.x, collisions[i].touch.y})
end
hero:moveTo(actualX, actualY)
gfx.sprite.update()
for i = 1, #touchy do
gfx.drawCircleAtPoint(touchy[i][1], touchy[i][2], 3)
end
end