Allowing getCollisionRects() to use invalid tileID

When setting up a tilemap, it is allowed at the moment to set a tileID to 0 (even if this is not a valid ID). It will be shown as fully transparent as expected.

The issue is that when passing the list of empty IDs in getCollisionRects(), it seems the tileIDs need to be a valid reference to the tileset even though the tileID might be used in the tilemap. As a result invalid entries in the tilemap will be always solid when we use addWallSprites().

It is quite typical in a level editor to not always set all the tiles in a tilemap so that would be nice to also be able to do that for playdate games using tilemap and still use the SDK collisions.

Here a little test case:

import("CoreLibs/graphics")
import("CoreLibs/sprites")

local tiles = {}
for y = 1, 24 do
	for x = 1, 40 do
		local tile = 2
		if x > 20 then tile = 0 end
		if x==1 or x==40 or y==1 or y==24 then
			tile = 1
		end

		table.insert( tiles, tile)
	end
end

local tilemap = playdate.graphics.tilemap.new()
tilemap:setImageTable( playdate.graphics.imagetable.new( "tiles" ) )
tilemap:setTiles( tiles, 40)

local level = playdate.graphics.sprite.new( )
level:setTilemap( tilemap )
level:moveTo( 0, 0 )
level:setCenter(0, 0)
level:add()

playdate.graphics.sprite.addWallSprites( tilemap, {2, 0} )

function playdate.update()
	playdate.graphics.clear()
	local collisionRects = tilemap:getCollisionRects( {2, 0} )
	playdate.graphics.setColor(playdate.graphics.kColorBlack)
	for index, rect in ipairs(collisionRects) do
		playdate.graphics.fillRect(rect.x*10, rect.y*10, rect.width*10, rect.height*10)
	end
end

And the tiny tileset:
tiles-table-10-10

2 Likes

Filed! Sounds pretty straightforward, should be an easy fix.

1 Like

Please fix this. I am also running into this issue.

1 Like

Hey @dave

Is it possible to get an update on this?

Dan says it looks like the fix for emptyIDs for tilemap get repeated - #6 by dan also fixes this. Update coming soon! :sweat_smile:

2 Likes