Automatically setting collisions on each tile

Hello I am using Tiled with tiled2unity for my 2d rpg game, and was wondering if there was a quicker way to set collisions on each tile much like how unity sets up colliders with a tilemap layer. as I am finding it very time consuming to setup each tile with collision in collision editor.

In Unity, by default, each tile in a tile palette is set as “sprite” collider type, and it will automatically add a collider depending on the part of the tile is the actual image (and not the transparent part) if i add a tilemap collider component to the tilemap layer.
In tiled, however, i only know how to set collision on 1 tile at a time. Is there a way to make it like unity “sprite” collider for multiple tiles without going through each tile and setting up colliders?

In the Tilesets panel you can click the Edit Tileset button after which you can switch to the Tile Collision Editor. Is that what you are looking for?

Also, welcome to the forums! :confetti_ball: :slightly_smiling_face:

thanks for the welcome…

I understand where tile collision editor is but problem is i have thousands of tiles and for me to go through each tile to set collision would take too much time. Is there a faster way?

In unity, it will read a tile and automatically set collider on image boundary (exclude transparent part), it is called “sprite collider type”. Is there a similar procedure with tiled?

Ah, so you’re looking the set up pixel-perfect collider within Tiled? :face_with_raised_eyebrow:

Something akin to Inkscape’s bitmap tracer? (Not a solution)

sorry, I am not aware about Inkscapes bitmap tracer…

To put more simply, if you had thousands of tiles how would you add collision to them? One by one using tiled collision editor?

I’d say having thousands of tiles is contrary to the power of using tiles. You may want to ask yourself if/why you really need that much different ones. Their convenience lies within their reusability.

But if you want a pixel-perfect collision shape that is not identical to the alpha channel of the tiles you could either create separate black-and-white tiles or hide the collision within one of the color channels, making uneven values of red block objects for instance.

For the latter approach I made this little tool years ago with which you can make all pixels in a PNG have even colors after which you can draw your collision shapes on a separate additive layer with #010101 in an image editor. The color differences are too small for the human eye to notice, but a % 2 will reveal this hidden layer to your program. This approach even allows for multiple collision layers (one on each color channel) to be embedded within your tiles. You could differentiate between platforms and impassable obstacles or block monsters where a player could pass. Note that this tool is somewhat project-specific and may need modifications to work best for you.

EvenPNG.zip (9.4 KB)

Would there be a way to make the whole tile is there a way to make the whole tile collidable without manually drawing a box for each one?

Generally I would say that in such a case, you’d just want to set a boolean like “collides: true” custom property on those tiles. You can do this after selecting all the tiles you want to set the property on, so it’s pretty quick. However, I don’t know if Tiled2Unity has such a shorthand way of saying declaring a colliding tile. Does it, @Seanba?

If that is not an option, then a little bit faster way to do this is to copy the rectangle you set on one tile and then Paste-In-Place (Ctrl+Shift+V) the rectangle to other tiles. This is a little bit faster than recreating each rectangle (even with “Snap to Grid” enabled).

One problem with the above solution is that I unfortunately broke the Cut/Copy actions in the Tile Collision Editor in Tiled 1.2.1. It has been fixed for the upcoming Tiled 1.2.3, but until then the copying would have to be done from a map instead of the Tile Collision Editor.

Eventually, I hope to enable editing of tile collisions for multiple tiles at once. This is covered by:

That is not supported but I think it could be added to SuperTiled2Unity. Isometric tiles may be a bit of a pain though :wink:

What about a property that can be used to enable the Unity “sprite” collider type?

Hi, I’m a brand new user and I’m also struggling to add collision shapes to a tileset I downloaded. I definitely DON’T want to draw 500* or so polygons.

A comment in the thread above seems to suggest that I can set collision boundaries automatically from an alpha channel (but maybe that was in reference to Unity’s tile editor). Can I use an alpha channel or [preferably] a separate b/w mask image to define the collision boundaries for a tileset?

If not, I might take a pass at writing a python script to act directly on a .tsx file. The format seems quite readable.

(* I’m actually too lazy to draw even a single polygon, but I’ll happily spend the same amount of time automating the process :slight_smile:

@Eric_Gradman Rather than writing a Python script for this, would you consider contributing such a feature to Tiled itself? I can help in the process.

Sure. My only reservation however is that I plan to use OpenCV to detect the contours of the individual masks, and I doubt you want to link that library. There’s probably a good lightweight alternative, but I’d have to do some digging.

I’m going to hack it together in Python today, because I need it now. But I’m happy to port the core algorithm to cpp. I don’t think I can deal with the qt stuff though.

1 Like

My first foray into tiled this week and I am wondering if any neat solution is now in place for this?

Essentially I have a tileset of around 50 tiles and want the collidebox to be exactly the size of the box of the png. Is there a way to even just set the collidebox to be equal to the bounds of the tile?

I will be loading the tileset into Phaser 3 where other images loaded as staticGroup have auto collision box equal to their bounds however, this doesn’t seem to be the case for tileMaps.

I thought this was a good use-case for a custom script, but it turned out I needed to extend the scripting API a little to make it possible:

Now (or, with the upcoming development snapshot), it will be possible to execute the following statement in the console to initialize all tile collision shapes to be boxes matching the size of each tile:

tiled.activeAsset.macro("Initialize Tile Collision Boxes", function() {
    tiled.activeAsset.tiles.forEach(function(tile) {
        // avoid overwriting existing collision info
        if (tile.objectGroup !== null && tile.objectGroup.objectCount > 0)
            return

        let o = new MapObject()
        o.width = tile.width
        o.height = tile.height

        let g = new ObjectGroup()
        g.addObject(o)

        tile.objectGroup = g
    })
})

In general I’m not sure about including this functionality by default, since it seems strange to me to fill the tileset with what is essentially superfluous information. But if people often need this we could include it somehow.

I was having a similar problem and after visiting the Unity manual I found exactly what we’re all looking for.(https://docs.unity3d.com/Manual/class-TilemapCollider2D.html) In case you don’t want to read through it yourself it explains that all you have to do is select the tilemap you want and add the component “tilemap collider 2D”, it’ll make every single tile on that tilemap to have a box collider 2D. I hope this helps you all out. :slight_smile:

Edit: sorry for late response and also I just realised bjorn posted on my birthday lol.

1 Like