How can I modify tilesource collision shapes via script?

The auto-generated shapes are creating some tunneling problems at acute angles; I need to modify the shapes to extend beyond the bounds of the tiles in some cases in order to prevent this.

I’ve read Defold tile source manual and API reference (Resource) and API reference (Tilemap) but none of them suggest any mechanism of editing the auto-generated collision shapes.

Editor-script at design time or regular script at runtime would both be fine.

1 Like

You should be able to set a specific collision image that will be used instead.
I agree that it’s not an idea solution, but at least it’s a way forward for you:

Thanks, yes I am using a custom collision image, but that still limits the collision shapes to the extents of each tile. So a tile like this for instance:

…when hit with a ray from the player to its right side who is moving left, is very likely to return a downward-facing normal due to the acute angle at the bottom-right corner. I can fix this geometrically pretty easily by detecting such cases and building an extra pair of vertices outside of the tile’s bounds (which doesn’t seem possible with just the collision image).

If editing the collision shapes per-tile isn’t possible, but reading them is, then I could also dynamically bake a convex hull. In many ways this would be preferable.

1 Like

Yeah, I’m afraid our format doesn’t currently support custom shapes, but we generate it by creating a convex hull from the tile texels.

Thanks, that at least keeps me from going down a dead-end!

It would be a bit of work, but I could read the tilemap png pixels and read the tilemap data and bake my own merged collision shapes from that.

Edit: nvm, although “hull” is documented as a valid shape type to get, it looks like it’s unimplemented:

Is there a special name for the collision shape used by tilemaps? I’m trying to use physics.get_shape:

pprint(physics.get_shape(msg.url("#tile_collisions"), "/main/levels/level1/level1_map.tilemap"))

on this:

I’m not entirely sure but I don’t think this is supported actually.

That refers to the convex hull shapes:

If Defold had support for adding multiple of those to a collision object (like you can do with the other shape types), you could write a script that generates all the shapes needed to cover a tilemap.

2 Likes

If Defold had support for adding multiple of those to a collision object (like you can do with the other shape types), you could write a script that generates all the shapes needed to cover a tilemap.

This would be very nice - my current workaround is to create the various hull collisionshape files in a directory, and to also create a “hulls” game object in which I nest collisionobjects that refer (1:1 sadly) to those collision shapes:

This would be a pain by hand but using an editor script to bake a tilemap into a set of hulls isn’t bad.